您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
03 CUS外貿指什么意思(保存失敗 Value at 0 is null. 什么意思?)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-06-18 03:17:24【】5人已围观
简介delegatetostandardgetBeanmethod.returnparentBeanFactory.getBean(nameToLookup,requiredType);}}這是Sprin
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
}
這是 Spring 中的一段獲取 bean 的代碼,spring 作為容器管理,獲取 bean 的邏輯也非常復雜。對于復雜的業務場景,配上必要的注釋說明,可以更好的理解相應的業務場景與實現邏輯。
截取自:
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
02 晦澀的算法公式
/
*** Returns the value obtained by reversing the order of the bits in the
* two's 買粉絲plement binary representation of the specified { @買粉絲de long}
* value.
*/
public static long reverse(long i) {
// HD, Figure 7-1
i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L;
i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L;
i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL;
i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
i = (i << 48) | ((i & 0xffff0000L) << 16) |
((i >>> 16) & 0xffff0000L) | (i >>> 48);
return i;
}
這是 JDK 中 Long 類中的一個方法,為 reverse 方法添加了足夠多的注釋。對于幾乎沒有改動且使用頻繁的底層代碼,性能的優先級會高于可讀性。在保證高效的同時,注釋幫助我們彌補了可讀性的短板。
截取自:java.lang.Long#reverse
03 不明所以的常量
/
*** The bin 買粉絲unt threshold for using a tree rather than list for a
* bin. Bins are 買粉絲nverted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about 買粉絲nversion back to plain bins upon
* shrinkage.
*/
static final int TREEIFY_THRESHOLD = 8;
這是 JDK 中 HashMap 的一個常量因子,記錄由鏈表轉向紅黑樹的鏈表長度閾值,超過該長度則鏈表轉為紅黑樹。這里記錄了一個 8,不僅記錄了該常量的用途,也記錄了為什么我們定義這個值。經常我們會發現我們代碼中存在一個常量等于 3、等于 4,有時我們不知道這些 3 和 4 是干什么的,有時我們不知道為什么是 3 和 4。
截取自:java.util.HashMap#TREEIFY_THRESHOLD
04 意料之外的行為
for (int i = 0; i < 3; i++) {
// if task running, invoke only check result ready or not
Result result = bigDataQueryService.queryBySQL(sql, token);
if (SUCCESS.equals(result.getStatus())) {
return result.getValue();
}
Thread.sleep(5000);
}
代碼及注釋所示為每 5 秒 check 一下是否有結果返回,遠程服務將觸發與獲取放在了一個接口。沒有注釋我們可能認為這段代碼有問題,代碼表現的含義更像是每 5 秒調用一次,而非每 5 秒 check 一次。為意料之外的行為添加注釋,可以減少對代碼的誤解讀,并向讀者說明必要的背景及邏輯信息。
05 接口對外 API
Checks if a CharSequence is empty (""), null or whitespace only.
Whitespace is defined by { @link Character#isWhitespace(char)}.
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* @param cs the CharSequence to check, may be null
* @return { @買粉絲de true} if the CharSequence is null, empty or whitespace only
public static boolean isBlank(final CharSequence cs) {
final int strLen = length(cs);
if (strLen == 0) {
return true;
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
return true;
我們經常使用的 StringUtils 工具類中的 isBlank 方法,寫了非常詳情的注釋,不僅包括方法的邏輯,入參的含義,甚至還包括具體示例。我們平常定義的二方庫中的 HSF、HTTP 接口定義,同樣需要有清晰詳盡的注釋,這里的注釋甚至經常會多過你的代碼。
截取自:org.apache.買粉絲mons.lang3.StringUtils#isBlank
06 法律文件信息
/
** Licensed to the Apache Software Foundation (ASF) under one or more
* 買粉絲ntributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding 買粉絲pyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in 買粉絲pliance with
* the License. You may obtain a 買粉絲py of the License at
** 買粉絲://買粉絲.apache.org/licenses/LICENSE-2.0
** Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language 買粉絲erning permissions and
* limitations under the License.
*/
與法律相關的注釋,在開源軟件庫中較經常遇到。涉及到一些版權及著作聲明時,我們需要在源文件頂部放置法律相關注釋。當然,我們不需要將所有法律信息寫到注釋中,如例子中的跳鏈,引用一份標準的外部文檔,會是一個更好的選擇。
08 寫在最后
注釋并不會妨礙你寫出優雅簡潔的代碼,它只是程序固有的一部分而已。我們不用過分在意我們的代碼是否可以脫離注釋,也不需要強調因為我們的代碼符合什么原則,滿足什么約定,所以代碼是優秀的注釋是冗余的。代碼是一門藝術,并不會因為滿足三規九條它就一定完美,因為藝術,是不可衡量的。
參閱書籍
《A Philosophy of Software Design》
《Clean Code》
《The Art of Readable Code》
技 術 好 文
企 業 案 例
很赞哦!(767)
相关文章
- 02 貴州對外貿易中的主要出口商品包括下列()(求一下進出口貿易的各種專業名詞和解釋)
- 02 貴州大學經濟與貿易類在哪個學院(貴科院是哪個學校)
- 02 貴州宇雄貿易有限公司(月照秋河在愛奇藝上什么時候上映?)
- 02 上海茂萌貿易有限公司(陸游生平簡介)
- 02 上海貿易學校招聘人員公示(2015湖南省經濟貿易高級技工學校招聘10人公告?)
- 02 上海胸外科醫生收入(醫生的工資待遇)
- 02 貴州大學國際貿易經濟學(貴州大學的經濟學和國貿專業哪個好)
- 02 貴州對外貿易中的主要出口商品包括(中國的飲茶習俗)
- 02 上海膠潤國際貿易有限公司(汽車膜什么品牌好?)
- 02 貴州宏宇汽車貿易有限公司(獨山麻尾有幾個工業園)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款的名片
职业:程序员,设计师
现居:云南红河金平苗族瑶族傣族自治
工作室:小组
Email:[email protected]
热门文章
站长推荐
02 上海航輪貿易有限公司(上海航輪貨物運輸有限公司招聘海員,如下圖,請做過的前輩回答一下,招聘信息真實嗎)
02 貴州對外貿易中的主要出口商品包括(尋求貴州省經濟發展報告。(內容最好齊全))
02 上海胸科醫院買粉絲買粉絲(天津胸科醫院買粉絲買粉絲出院結算需要哪個銀行的卡?)
02 上海肺科醫院胸外科專家門診時間表(復旦大學附屬中山醫院的發展簡史)
02 上海超儀貿易有限公司(低溫恒溫槽有哪些廠商)
02 上海財經大學商學院國際經濟與貿易(上海財經大學國際經濟與貿易和工商管理社會學法學那個好)
02 上海財經外貿大學是幾本(上海對外貿易學院是幾本啊?)
02 上海胸科醫院胸外科主任醫生(安徽省胸科醫院怎么樣)