1. 程式人生 > >程式碼註釋規範-IDEA 配置 Java 類方法註釋模板

程式碼註釋規範-IDEA 配置 Java 類方法註釋模板

1. 引言

    團隊開發時,業務模組分配的越清晰,程式碼註釋管理越完善,越有利於後面維護,後面再管理也方便不少。另外也起著"文字磚"的作用,你懂的。註釋不需要很詳細,把程式碼塊方法塊功能簡述一下就行。不然三月後回頭看就要罵人了,罵完發現是自己寫的,嘖嘖嘖...

    三種常用的 Java 註釋方式

    // 宣告常量
    int number;
    /*
     * 類主函式
     */
    public static void main(String[] args) {
    }
    /**
     * @param masterId  品牌商Id
     * @param shopId    店鋪Id
     * @param skuId     商品skuId
     * @description: 校驗商品標識碼與店鋪的所屬關係
     * @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 下添加註釋模板,配置模板後點擊 Apply OK 應用。

    自定義註釋模板

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

    新建介面檔案自動生成註釋,效果如下

/**
 * @className: CrowdService
 * @description: 人群物件業務
 * @author: niaonao
 * @date: 2020/1/13
 **/
public interface CrowdService {
}

2.1.2 自定義註釋模板不完全變數參考表

預定義變數 描述資訊
${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 下新增一個新模板組,名字自定義 JavaTemplateGroup。選中模板組,右側點選新增按鈕,建立新模板。

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

    Edit variables 編輯變數

  • 變數 return 表示式為 methodReturnType()
  • 變數 date 表示式為 date()
  • 變數 param 表示式為
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()) 

    若有警告資訊 No Applicable contexts,點選 Define 選中 Java 即可。

    此處 Expend With 配置為 Enter 回車鍵,註釋生成快捷方式,看個人習慣,也可以時 Tab 鍵。

    點選 Apply OK 應用配置即可。效果如下

    /**
     * @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 補充說明

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

    類註釋模板在檔案建立時生成,已建立檔案不會觸發該模板,會觸發方法註釋模板。

Power By niaonao, The End, Thanks