一、命名規范
1、 項目名全部小寫
2、 包名全部小寫
3、 類名首字母大寫,如果類名由多個單詞組成,每個單詞的首字母都要大寫。
如:public class MyFirstClass{}
4、 變量名、方法名首字母小寫,如果名稱由多個單詞組成,每個單詞的首字母都要大寫。
如:int index=0;
5、 常量名全部大寫
如:public static final String GAME_COLOR=”RED”;
6、所有命名規則必須遵循以下規則:
1)、名稱只能由字母、數字、下划線、$符號組成
2)、不能以數字開頭
3)、名稱不能使用JAVA中的關鍵字。
4)、堅決不允許出現中文及拼音命名。
二、注釋規范
好的代碼規范是一個程序員的基本修煉,好的代碼注釋更能體現一個程序員的思維邏輯,雖然代碼是用來給機器運行的,我們只要能寫出能讓編譯器運行的代碼就行了,但是如果沒有好的編碼規范,到項目后期,加入開發的人員逐漸增多時,每個人的編碼風格都不一樣,這就會讓項目維護者很難維護,所以開始就要制定一些好的規范來讓大家遵守,這樣才能寫出可維護,健壯的項目,這就是接下來要做的事情。第一節從要從代碼注釋這一塊說起,包含: 版權注釋、類注釋(Class)、構造函數注釋(Constructor)、方法注釋(Methods)、代碼塊注釋(Block)、單句注釋、字段名注釋,然后分別為eclipse、IDEA創建注釋模塊等。
二、約定
下面就是一些常見的注釋示例:
其中 /** */注釋和 /* */注釋 的區別:
/** */注釋的話,你再調用類和方法的時候會出現提示,內容就是你寫的注釋。就好像文檔幫助一樣。類似"字符串".toString(),鼠標放在toString()上時出現的api說明。 而/* */就沒有了。 /* */就是//的多行版
1、版權注釋
版權注釋主要用來聲明公司的一些基本信息等:
-
/**
-
* projectName: xxx
-
* fileName: Tk.java
-
* packageName: xxxx
-
* date: 2017年12月18日下午12:28:39
-
* copyright(c) 2017-2020 xxx公司
-
*/
2、類注釋(Class)
類注釋(Class)主要用來聲明該類用來做什么,以及創建者、創建日期版本、包名等一些信息:
-
/**
-
* @version: V1.0
-
* @author: fendo
-
* @className: user
-
* @packageName: user
-
* @description: 這是用戶類
-
* @data: 2017-07-28 12:20
-
**/
3、構造函數注釋(Constructor)
構造函數注釋(Constructor)主要用來聲明該類的構造函數、入參等信息:
-
**
-
* @description: 構造函數
-
* @param: [sid, pid]
-
*/
4、方法注釋(Methods)
方法注釋(Methods)主要用來聲明該類的作用、入參、返回值、異常等信息:
-
/**
-
* @author: fendo
-
* @methodsName: addUser
-
* @description: 添加一個用戶
-
* @param: xxxx
-
* @return: String
-
* @throws:
-
*/
5、代碼塊注釋(Block)
-
/**
-
* 實例化一個用戶
-
* xxxxxxx
-
*/
-
User user=new User();
6、單句注釋
User user=new User(); //實例化一個用戶
7、字段名注釋
-
/**
-
* 用戶名
-
*/
-
public String name;
或者使用如下格式:
-
/**用戶名**/
-
public String name;
三、IDE模板
接下來就是分別在Eclipse和IDEA中實現上面的注釋,然后分別生成模板:
3.1、Eclipse代碼注釋
在Eclipse中可以通過CodeTemplates進行設置,具體步驟如下: Window->Preference->Java->Code Style->Code Template

其中有兩類一類是Comments、主要是類中的一些通用模板:

1.文件(Files)注釋標簽:
設置版權信息:
-
/**
-
* projectName: ${project_name}
-
* fileName: ${file_name}
-
* packageName: ${package_name}
-
* date: ${date}${time}
-
* copyright(c) 2017-2020 xxx公司
-
*/
注意: 要打上勾!!
2.類型(Types)注釋標簽(類的注釋):
-
/**
-
* @title: ${file_name}
-
* @package ${package_name}
-
* @description: ${todo}
-
* @author: fendo
-
* @date: ${date} ${time}
-
* @version: V1.0
-
*/

3.字段(Fields)注釋標簽:
-
/**
-
* @Fields ${field} : ${todo}(用一句話描述這個變量表示什么)
-
*/
4.構造函數(Constructors)標簽:
-
/**
-
* @title: ${enclosing_type}
-
* @description: ${todo}(這里用一句話描述這個方法的作用)
-
* @param: ${tags}
-
* @throws:
-
*/
5.方法(Methods)標簽:
-
/**
-
*@title: ${enclosing_method}
-
*@description: ${todo}
-
*@author: fendo
-
*@date: ${date} ${time}
-
*${tags}
-
*@throws:
-
*/
6.覆蓋方法(Overriding Methods)標簽:
-
/**
-
* @title: ${enclosing_method}
-
* @description: ${todo}
-
* ${tags}
-
* ${see_to_overridden}
-
*/
7.代表方法(Delegate Methods)標簽:
-
/**
-
* ${tags}
-
* ${see_to_target}
-
*/
8.Getter方法標簽:
-
/**
-
* @title: ${enclosing_method}
-
* @description: ${todo}
-
* @return: ${field_type}
-
*/
9.Setter方法標簽:
-
/**
-
* @title: ${enclosing_method}
-
* @description: ${todo}
-
* @return: ${field_type}
-
*/

由於基本的在上面的已經設置好了,所以這里也不需要設置什么,然后就是把這個模板導出來,分發給各開發人員,讓他們導進來就行了。

完整的模板如下:
-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
<template autoinsert="false" context="gettercomment_context" deleted="false" description="Comment for getter method" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.gettercomment" name="gettercomment">/**
-
* @title: ${enclosing_method}
-
* @description: ${todo}
-
* @return: ${field_type}
-
*/
</template>
<template autoinsert="false" context="constructorcomment_context" deleted="false" description="Comment for created constructors" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name="constructorcomment">/**
-
* @title: ${enclosing_type}
-
* @description: ${todo}
-
* @param: ${tags}
-
* @throws
-
*/
</template>
<template autoinsert="false" context="filecomment_context" deleted="false" description="Comment for created Java files" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.filecomment" name="filecomment">/**
-
* projectName:${project_name}
-
* fileName:${file_name}
-
* packageName:${package_name}
-
* date:${date}${time}
-
* copyright(c) 2017-2020 xxx公司
-
*/
</template>
<template autoinsert="false" context="typecomment_context" deleted="false" description="Comment for created types" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.typecomment" name="typecomment">/**
-
* @title: ${file_name}
-
* @package ${package_name}
-
* @description: ${todo}
-
* @author: fendo
-
* @date: ${date} ${time}
-
* @version: V1.0
-
*/
</template>
<template autoinsert="false" context="methodcomment_context" deleted="false" description="Comment for non-overriding methods" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.methodcomment" name="methodcomment">/**
-
*@title ${enclosing_method}
-
*@description: ${todo}
-
*@author: fendo
-
*@date: ${date} ${time}
-
*${tags}
-
*@throws
-
*/
</template>
<template autoinsert="false" context="overridecomment_context" deleted="false" description="Comment for overriding methods" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.overridecomment" name="overridecomment">/**
-
* @title: ${enclosing_method}
-
* @description: ${todo}
-
* ${tags}
-
* ${see_to_overridden}
-
*/
</template>
<template autoinsert="false" context="settercomment_context" deleted="false" description="Comment for setter method" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.settercomment" name="settercomment">/**
-
* @title: ${enclosing_method}
-
* @description: ${todo}
-
* @return: ${field_type}
-
*/
</template>
<template autoinsert="false" context="fieldcomment_context" deleted="false" description="Comment for fields" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name="fieldcomment">/**
-
* @Fields ${field} : ${todo}
-
*/
</template>
<template autoinsert="false" context="delegatecomment_context" deleted="false" description="Comment for delegate methods" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name="delegatecomment">/**
-
* ${tags}
-
* ${see_to_target}
-
*/
</template>
</templates>
3.2、IDEA代碼注釋
idea有兩種快捷方式,一個是live templates,一個是file and code templates。
3.2.1、file and code templates
IDEA的code templates僅限於類文件頭和所有文件頭。配置如下圖:
File -- Settings -- Editor -- Code Style -- File and Code Templates

模板如下,只能實現類注釋,方法注釋只能用live templates
-
/**
-
* projectName: ${PROJECT_NAME}
-
* fileName: ${NAME}.java
-
* packageName: ${PACKAGE_NAME}
-
* date: ${YEAR}-${MONTH}-${DAY} ${TIME}
-
* copyright(c) 2017-2020 xxx公司
-
*/
-
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
-
-
/**
-
* @version: V1.0
-
* @author: fendo
-
* @className: ${NAME}
-
* @packageName: ${PACKAGE_NAME}
-
* @description: ${DESCRIPTION}
-
* @data: ${YEAR}-${MONTH}-${DAY} ${TIME}
-
**/
-
public class ${NAME} {
-
}
3.2.1、live templates
Live Template用中文應該叫做熱加載模板。它的原理就是配置一些常用代碼字母縮寫,在輸入簡寫時可以出現你預制的模板內容,使得開發效率大大提高。
在配置當中找到Live Template,右邊加號先添加一個TemplateGroup

選中該分組再點擊加號添加一個Live Template.Abbreviation中填寫命令,Description填寫描述,Template text填寫你的配置模板。

代碼注釋模板如下:
-
/**
-
* @title: $file_name$
-
* @package $package_name$
-
* @description:
-
* @author: $author$
-
* @date: $date$ $time$
-
* @version: V1.0
-
*/
這里的變量是$$括起來的!!
然后點擊

選擇Everywhere

然后選擇JAVA

最后點擊右下角的Edit variables 按鈕,然后彈出一個窗口,如下:

注意:
默認值需要用""括起來!!
內置函數詳細請參考:https://www.jetbrains.com/help/idea/live-template-variables.html
方法注釋如下:
-
/**
-
*@title: $enclosing_method$
-
*@description: TODO
-
*@author: $author$
-
*@date: $date$ $time$
-
*@param: $param$
-
*@return: $return$
-
*@throws:
-
*/
其中的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\\b' : '')}; return result", methodParameters())
-
這種生成的會換行。
注意:
有個很坑的地方就是,使用這個注釋的時候,必須在方法內使用,如果在方法外使用有些參數就會獲取不到。。。

不足之處:
1、live template中的函數方法是讀取當前函數體的屬性,所以只有在該方法內使用該命令才能獲取,如果想獲取其他一些信息,如項目名,字段名,根本獲取不到,這是個比較雞肋的地方。
2、Template variables的Expression不能疊加方法。定制化程度不夠好。
IntelliJ IDEA 的實時代碼模板保存在 /templates 目錄下,其他系統目錄位置如下:
-
Windows: C:\Users\xxxx\.IntelliJIdea2017.2\config
-
Linux: ~/.
<product name>
<version number>/config/templates
-
OS X: ~/Library/Preferences/IdeaIC2017.2/templates
一些常用的模板:
1.logger
private static final Logger logger = LoggerFactory.getLogger($CLASS_NAME$.class);
2.loggerout
logger.info("op=start_$METHOD_NAME$, $PARAMS_FORMAT$", $PARAMS$);
3.test
-
@Test
-
public void test() {
-
-
}
