This commit is contained in:
周伟
2022-05-11 19:04:14 +08:00
parent 9440ac7291
commit d9c5ffd627
826 changed files with 0 additions and 481675 deletions

View File

@@ -12,9 +12,7 @@
<!-- theme css & js -->
<meta name="generator" content="Hexo 4.2.0">
</head>
<body>
<div class="book-container">
<div class="book-sidebar">
<div class="book-brand">
@@ -27,86 +25,61 @@
<ul class="uncollapsible">
<li><a href="/" class="current-tab">首页</a></li>
</ul>
<ul class="uncollapsible">
<li><a href="../">上一级</a></li>
</ul>
<ul class="uncollapsible">
<li>
<a href="/极客时间/Java基础36讲.md.html">Java基础36讲.md.html</a>
</li>
<li>
<a href="/极客时间/Java错误示例100讲.md.html">Java错误示例100讲.md.html</a>
</li>
<li>
<a href="/极客时间/Linux性能优化.md.html">Linux性能优化.md.html</a>
</li>
<li>
<a href="/极客时间/MySQL实战45讲.md.html">MySQL实战45讲.md.html</a>
</li>
<li>
<a href="/极客时间/从0开始学微服务.md.html">从0开始学微服务.md.html</a>
</li>
<li>
<a class="current-tab" href="/极客时间/代码精进之路.md.html">代码精进之路.md.html</a>
</li>
<li>
<a href="/极客时间/持续交付36讲.md.html">持续交付36讲.md.html</a>
</li>
<li>
<a href="/极客时间/程序员进阶攻略.md.html">程序员进阶攻略.md.html</a>
</li>
<li>
<a href="/极客时间/趣谈网络协议.md.html">趣谈网络协议.md.html</a>
</li>
</ul>
</div>
</div>
<div class="sidebar-toggle" onclick="sidebar_toggle()" onmouseover="add_inner()" onmouseleave="remove_inner()">
<div class="sidebar-toggle-inner"></div>
</div>
<script>
function add_inner() {
let inner = document.querySelector('.sidebar-toggle-inner')
inner.classList.add('show')
}
function remove_inner() {
let inner = document.querySelector('.sidebar-toggle-inner')
inner.classList.remove('show')
}
function sidebar_toggle() {
let sidebar_toggle = document.querySelector('.sidebar-toggle')
let sidebar = document.querySelector('.book-sidebar')
@@ -122,7 +95,6 @@
}
}
function open_sidebar() {
let sidebar = document.querySelector('.book-sidebar')
let overlay = document.querySelector('.off-canvas-overlay')
@@ -135,9 +107,7 @@ function hide_canvas() {
sidebar.classList.remove('show')
overlay.classList.remove('show')
}
</script>
<div class="off-canvas-content">
<div class="columns">
<div class="column col-12 col-lg-12">
@@ -489,14 +459,11 @@ fail:
<h2>一起来动手</h2>
<p>下面的这段代码,我们前面用过一次,我稍微做了点修改。我们这次重点来看编码的规范,有哪些地方你看着不顺眼,你会怎么改进?</p>
<pre><code>package com.example;
import java.util.Collections;
import java.util.List;
import javax.net.ssl.SNIServerName;
class ServerNameSpec {
final List&lt;SNIServerName&gt; serverNames;
ServerNameSpec(List&lt;SNIServerName&gt; serverNames) {
this.serverNames = Collections.&lt;SNIServerName&gt;unmodifiableList(serverNames);
}
@@ -616,7 +583,6 @@ static final String[] names = { &quot;Alice&quot;, &quot;Bob&quot;, &quot;Tom&qu
<p>所以为了让你更好地实践,我找了一段 Java 代码。你来试试,这段代码中有哪些名字可以优化? 欢迎你把优化的代码发在评论里,我们亲自感受下如何优化代码名字。</p>
<pre><code>import java.util.HashMap;
import java.util.Map;
class Solution {
/**
* Given an array of integers, return indices of the two numbers
@@ -666,14 +632,12 @@ if ((firstName != null) &amp;&amp; (lastName != null))
<p>下面这个简图,可以直观地表示这种代码块的布局方式。</p>
<p><img src="assets/4d810b8acbdc3a5413f53a432dc08eb7.png" alt="img" />或者,也可以参考下面的代码示例。 这段代码,也包含了使用空格区分同一行代码内部的信息块。</p>
<pre><code>package coding;
public class CodingFormat {
public static void main(String[] args) {
System.out.println(
&quot;German say hello with &quot;
+ Greeting.GERMAN.getGreeting());
}
private static enum Greeting {
ENGLISH (&quot;English&quot;, &quot;Hello&quot;),
SPANISH (&quot;Spanish&quot;, &quot;Hola&quot;),
@@ -716,19 +680,16 @@ public class CodingFormat {
<p>上面的 CodingFormat 例子中,我们使用了四个空格作为一个缩进单元。下面,我们看看两个空格的缩进,以及八个空格的缩进效果。</p>
<p>两个空格的缩进:</p>
<pre><code>package coding;
public class CodingFormat {
public static void main(String[] args) {
System.out.println(
&quot;German say hello with &quot; + Greeting.GERMAN.getGreeting());
}
private static enum Greeting {
ENGLISH (&quot;English&quot;, &quot;Hello&quot;),
SPANISH (&quot;Spanish&quot;, &quot;Hola&quot;),
GERMAN (&quot;German&quot;, &quot;Hallo&quot;),
MANDARIN (&quot;Mandarin&quot;, &quot;Ni Hao&quot;);
private final String language;
private final String greeting;
@@ -751,14 +712,12 @@ public class CodingFormat {
<p>两个空格的缩进,视觉上,缩进线靠得太近,相对而言,更容易混淆代码的分块级别。这是两个空格缩进的一个小缺点。</p>
<p>我们再来看下八个空格的缩进:</p>
<pre><code>package coding;
public class CodingFormat {
public static void main(String[] args) {
System.out.println(
&quot;German say hello with &quot;
+ Greeting.GERMAN.getGreeting());
}
private static enum Greeting {
ENGLISH (&quot;English&quot;, &quot;Hello&quot;),
SPANISH (&quot;Spanish&quot;, &quot;Hola&quot;),
@@ -794,7 +753,6 @@ public class CodingFormat {
</code></pre>
<p>双目运算符或者多目运算符,运算符前后都要使用空格:</p>
<pre><code>firstName != null
(firstName != null) &amp;&amp; (lastName != null)
</code></pre>
<h2>一行一个行为</h2>
@@ -803,7 +761,6 @@ public class CodingFormat {
<p>一个重要的原则是,每一行代码仅仅表示一个行为。这样每一行的代码才是一个常规大小的、可以识别的基础信息块。</p>
<p>比如说,下面的这行代码就包含了两个行为,一个是判断行为,一个是执行行为。 两个行为放在一行,这样的代码不仅看起来有些乱,我们的大脑处理起来也有些懵。</p>
<pre><code>if (variable != null) variable.doSomething();
</code></pre>
<p>如果分隔开这两个行为,信息块的区隔会更明显,代码会更清晰:</p>
<pre><code>if (variable != null) {
@@ -836,11 +793,9 @@ public class CodingFormat {
</ul>
<pre><code>anObject.methodOne(parameterForMethodOne,
anObject.methodTwo(parameterForMethodTwo));
/ conventional indentation
int runningMiles = runningSpeedOne * runningTimeOne
+ runningSpeedTwo * runningTimeTwo;
// confusing indentation
int runningMiles = runningSpeedOne
* runningTimeOne + runningSpeedTwo
@@ -860,13 +815,11 @@ int runningMiles = runningSpeedOne
anObject.methodTwo(parameterOneForMethodTwo,
parameterTwoForMethodTwo,
parameterThreeForMethodTwo));
// bad indentation
if ((conditionOne &amp;&amp; conditionTwo)
|| (conditionThree &amp;&amp; conditionFour)) {
doSomething();
}
// a better indentation, using 8 spaces for the indentation
if ((conditionOne &amp;&amp; conditionTwo)
|| (conditionThree &amp;&amp; conditionFour)) {
@@ -880,7 +833,6 @@ if ((conditionOne &amp;&amp; conditionTwo)
<p>还记得我们上一节的练习题吗?上次我们改名字,这次我们来修改一下代码的编排。欢迎你把优化的代码公布在讨论区,也可以写下你的优化思路,我们一起来看看编排优化后的代码是不是更好阅读了呢?</p>
<pre><code>import java.util.HashMap;
import java.util.Map;
class Solution {
/**
* Given an array of integers, return indices of the two numbers
@@ -963,9 +915,7 @@ private static void verifyLength(
ByteBuffer buffer, int requiredLength) {
...
}
String myString; // using end-to-line comment
// This is a multiple line comment. This is a multiple
// line comment.
if (!myString.isEmpty()) {
@@ -977,7 +927,6 @@ if (!myString.isEmpty()) {
<pre><code>/*
* This is a multiple line comment. This is a multiple
* line comment.
if (programingLanguage.equals(&quot;Java&quot;)) {
...
} */
@@ -1014,7 +963,6 @@ if (!myString.isEmpty()) {
<p>还记得我们上一节的练习题吗?前面,我们改了名字,改了编排。这一次,我们来修改注释。认真读一下这段代码,看看有需要增加或者修改注释的地方吗?欢迎你把优化的代码公布在讨论区,我们一起来感受修改后的代码是不是更好阅读,更好维护。</p>
<pre><code>import java.util.HashMap;
import java.util.Map;
class Solution {
/**
* Given an array of integers, return indices of the two numbers
@@ -1117,7 +1065,6 @@ class Solution {
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Solution {
/**
* Return a list of the words in words that match the given pattern.
@@ -1145,7 +1092,6 @@ class Solution {
ans.add(word);
return ans;
}
public boolean match(String word, String pattern) {
Map&lt;Character, Character&gt; M = new HashMap();
for (int i = 0; i &lt; word.length(); ++i) {
@@ -1235,7 +1181,6 @@ public String(byte ascii[], int hibyte) {
<p>做好这件事情,需要我们使用 Deprecated 注解,并且用一切可以使用的办法,广而告之。对于代码而言,要在声明中使用 Deprecated 注解;在规范描述中,说明废弃的原因以及替代的办法;对于有计划要删除的接口,要注明计划删除的版本号。</p>
<p>下面是两个可以参照的 Java 代码废弃接口的例子:</p>
<pre><code>java/lang/String.java:
/**
* Counts the number of stack frames in this thread. The thread must
* be suspended.
@@ -1253,7 +1198,6 @@ public String(byte ascii[], int hibyte) {
@Deprecated(since=&quot;1.2&quot;, forRemoval=true)
public native int countStackFrames();
java.security.Certificate.java:
/**
* &lt;p&gt;This is an interface of abstract methods for managing a
* variety of identity certificates.
@@ -1445,7 +1389,6 @@ boolean isAuthenticatedUser(String userName, String password) {
<p>欢迎你把优化的代码公布在讨论区,我们一起来讨论,看哪些地方我们可以做得更好。也欢迎你把今天的内容分享给协作的小伙伴,和他一起进步。</p>
<pre><code>import java.util.HashMap;
import java.util.Map;
class Solution {
/**
* Given an array of integers, return indices of the two numbers
@@ -1570,7 +1513,6 @@ class Solution {
<p>也欢迎点击“请朋友读”,把这篇文章分享给你的朋友或者同事,一起来体验修改代码的快感。</p>
<pre><code>import java.util.HashMap;
import java.util.Map;
class Solution {
/**
* Given an array of integers, return indices of the two numbers
@@ -2032,7 +1974,6 @@ class Solution {
<p>欢迎你把优化的代码公布在讨论区,我们一起来看看性能优化后的代码可以是什么样的?</p>
<pre><code>import java.util.HashMap;
import java.util.Map;
class Solution {
/**
* Given an array of integers, return indices of the two numbers
@@ -2115,7 +2056,6 @@ class Solution {
<p>欢迎你在留言区讨论上面的问题,我们一起来看看这一小段代码,是不是可以做的更好?</p>
<pre><code>import java.util.HashMap;
import java.util.Map;
class Solution {
/**
* Given an array of integers, return indices of the two numbers
@@ -2334,12 +2274,10 @@ boolean isRegisteredUser(String userName) {
<a href="/极客时间/持续交付36讲.md.html">下一页</a>
</div>
</div>
</div>
</div>
</div>
</div>
<a class="off-canvas-overlay" onclick="hide_canvas()"></a>
</div>
<script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script><script defer src="https://static.cloudflareinsights.com/beacon.min.js/v652eace1692a40cfa3763df669d7439c1639079717194" integrity="sha512-Gi7xpJR8tSkrpF7aordPZQlW2DLtzUlZcumS8dMQjwDHEnw9I7ZLyiOj/6tZStRBGtGgN6ceN6cMH8z7etPGlw==" data-cf-beacon='{"rayId":"709980b46d648b66","version":"2021.12.0","r":1,"token":"1f5d475227ce4f0089a7cff1ab17c0f5","si":100}' crossorigin="anonymous"></script>
@@ -2348,11 +2286,9 @@ boolean isRegisteredUser(String userName) {
<script async src="https://www.googletagmanager.com/gtag/js?id=G-NPSEEVD756"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-NPSEEVD756');
var path = window.location.pathname
@@ -2366,14 +2302,12 @@ boolean isRegisteredUser(String userName) {
} else {
setCookie("lastPath", path)
}
function setCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (180 * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires + ";path = /";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
@@ -2383,7 +2317,5 @@ boolean isRegisteredUser(String userName) {
}
return "";
}
</script>
</html>