代碼注釋規(guī)范-IDEA 配置 Java 類方法注釋模板

1. 引言

????團(tuán)隊(duì)開發(fā)時(shí),業(yè)務(wù)模塊分配的越清晰,代碼注釋管理越完善,越有利于后面維護(hù),后面再管理也方便不少。另外也起著"文字磚"的作用,你懂的。注釋不需要很詳細(xì),把代碼塊方法塊功能簡述一下就行。不然三月后回頭看就要罵人了,罵完發(fā)現(xiàn)是自己寫的,嘖嘖嘖...

????三種常用的 Java 注釋方式

    // 聲明常量
    int number;
    /*
     * 類主函數(shù)
     */
    public static void main(String[] args) {
    }
    /**
     * @param masterId  品牌商Id
     * @param shopId    店鋪Id
     * @param skuId     商品skuId
     * @description: 校驗(yàn)商品標(biāo)識(shí)碼與店鋪的所屬關(guān)系
     * @return: net.jdcloud.APIECRM.model.ValidateSkuUsingGETResponse
     * @author: niaonao
     * @date: 2020/01/13
     */
    public static ValidateSkuUsingGETResponse validateSkuUsing(String masterId, String shopId, String skuId){
        return null;
    }

2. 自定義注釋模板

2.1 類注釋模板

2.1.1 配置模板

????菜單路徑 File-Settings-Editor-File and Code Templates-Incudes-File Header 下添加注釋模板,配置模板后點(diǎn)擊 Apply OK 應(yīng)用。

image

????自定義注釋模板

/**
 * @className: ${NAME}
 * @description: TODO 類描述 
 * @author: niaonao
 * @date:  ${DATE}
 **/

????新建接口文件自動(dòng)生成注釋,效果如下

/**
 * @className: CrowdService
 * @description: 人群對(duì)象業(yè)務(wù)
 * @author: niaonao
 * @date: 2020/1/13
 **/
public interface CrowdService {
}

2.1.2 自定義注釋模板不完全變量參考表

預(yù)定義變量 描述信息
${NAME} the name of the current file
${PACKAGE_NAME} name of the package in which the new file is created
${USER} current user system login name
${DATE} current system date
${TIME} current system time
${YEAR} current year
${MONTH} current month
${MONTH_NAME_SHORT} first 3 letters of the current month name. Example: Jan, Feb, etc.
${MONTH_NAME_FULL} full name of the current month. Example: January, February, etc.
${DAY} current day of the month
${DAY_NAME_SHORT} first 3 letters of the current day name. Example: Mon, Tue, etc.
${DAY_NAME_FULL} full name of the current day. Example: Monday, Tuesday, etc.
${HOUR} current hour
${MINUTE} current minute
${PROJECT_NAME} the name of the current project

2.2 方法注釋模板

2.2.1 配置模板

????菜單路徑 File-Settings-Editor-Live Templates 下添加一個(gè)新模板組,名字自定義 JavaTemplateGroup。選中模板組,右側(cè)點(diǎn)擊新增按鈕,創(chuàng)建新模板。

  • Abbreviation 配置為* 。
  • Description 自定義描述信息。
  • Template Text 自定義模板
*
 $param$
 * @description: TODO
 * @return: $return$
 * @author: niaonao
 * @date: $date$
 */
image
image

????Edit variables 編輯變量

  • 變量 return 表達(dá)式為 methodReturnType()
  • 變量 date 表達(dá)式為 date()
  • 變量 param 表達(dá)式為
groovyScript("def result=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {result+='* @param: ' + params[i] + ((i < params.size() - 1) ? '\\n ' : '')};return result", methodParameters()) 
image

????若有警告信息 No Applicable contexts,點(diǎn)擊 Define 選中 Java 即可。

image

????此處 Expend With 配置為 Enter 回車鍵,注釋生成快捷方式,看個(gè)人習(xí)慣,也可以時(shí) Tab 鍵。

????點(diǎn)擊 Apply OK 應(yīng)用配置即可。效果如下

    /**
     * @param: masterId
     * @param: shopId
     * @param: skuId
     * @description: TODO
     * @return: net.jdcloud.APIECRM.model.ValidateSkuUsingGETResponse
     * @author: niaonao
     * @date: 2020/1/13
     */
    public static ValidateSkuUsingGETResponse validateSkuUsing(String masterId, String shopId, String skuId) {
        return null;
    }

2.2.2 補(bǔ)充說明

????方法注釋模板不可用在,方法外,若用在方法外 @param 獲取不到,注釋為 @param null;

????類注釋模板在文件創(chuàng)建時(shí)生成,已創(chuàng)建文件不會(huì)觸發(fā)該模板,會(huì)觸發(fā)方法注釋模板。

Power By niaonao, The End, Thanks

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,504評(píng)論 0 13
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 2,719評(píng)論 0 3
  • 文件代碼模板的使用 文件代碼模板的介紹 文件代碼模板可以簡單理解為:我們?cè)陧?xiàng)目中創(chuàng)建某些類型文件時(shí),就已經(jīng)在對(duì)應(yīng)這...
    01f271d45f32閱讀 218評(píng)論 0 0
  • 每一份經(jīng)典 都在服飾中體現(xiàn) 經(jīng)典的服飾 傳承百年千年 仍舊是經(jīng)典 對(duì)經(jīng)典的服飾 我從來愛不釋手 誰動(dòng)我衣服 我砍他...
    狂野云豹閱讀 93評(píng)論 0 1
  • 在當(dāng)下“辣媽時(shí)代”女士對(duì)妊娠紋的重視度越來越高了。很多女士在孕育寶寶時(shí)有了妊娠紋,產(chǎn)后妊娠紋遲遲沒有消退的跡象,這...
    xindi1095閱讀 391評(píng)論 0 0