Java命名、注釋規范


一、命名規范

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、版權注釋

版權注釋主要用來聲明公司的一些基本信息等:


   
   
   
           
  1. /**
  2. * projectName: xxx
  3. * fileName: Tk.java
  4. * packageName: xxxx
  5. * date: 2017年12月18日下午12:28:39
  6. * copyright(c) 2017-2020 xxx公司
  7. */


2、類注釋(Class)

類注釋(Class)主要用來聲明該類用來做什么,以及創建者、創建日期版本、包名等一些信息:


   
   
   
           
  1. /**
  2. * @version: V1.0
  3. * @author: fendo
  4. * @className: user
  5. * @packageName: user
  6. * @description: 這是用戶類
  7. * @data: 2017-07-28 12:20
  8. **/


3、構造函數注釋(Constructor)

構造函數注釋(Constructor)主要用來聲明該類的構造函數、入參等信息:


   
   
   
           
  1. **
  2. * @description: 構造函數
  3. * @param: [sid, pid]
  4. */


4、方法注釋(Methods)

方法注釋(Methods)主要用來聲明該類的作用、入參、返回值、異常等信息:


   
   
   
           
  1. /**
  2. * @author: fendo
  3. * @methodsName: addUser
  4. * @description: 添加一個用戶
  5. * @param: xxxx
  6. * @return: String
  7. * @throws:
  8. */

5、代碼塊注釋(Block)


   
   
   
           
  1. /**
  2. * 實例化一個用戶
  3. * xxxxxxx
  4. */
  5. User user=new User();


6、單句注釋

User user=new User();  //實例化一個用戶
  
  
  
          

7、字段名注釋


   
   
   
           
  1. /**
  2. * 用戶名
  3. */
  4. public String name;

或者使用如下格式:


   
   
   
           
  1. /**用戶名**/
  2. public String name;


三、IDE模板


接下來就是分別在Eclipse和IDEA中實現上面的注釋,然后分別生成模板:


3.1、Eclipse代碼注釋


在Eclipse中可以通過CodeTemplates進行設置,具體步驟如下: Window->Preference->Java->Code Style->Code Template 



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




1.文件(Files)注釋標簽:


設置版權信息:


   
   
   
           
  1. /**
  2. * projectName: ${project_name}
  3. * fileName: ${file_name}
  4. * packageName: ${package_name}
  5. * date: ${date}${time}
  6. * copyright(c) 2017-2020 xxx公司
  7. */



注意: 要打上勾!!


2.類型(Types)注釋標簽(類的注釋):


   
   
   
           
  1. /**
  2. * @title: ${file_name}
  3. * @package ${package_name}
  4. * @description: ${todo}
  5. * @author: fendo
  6. * @date: ${date} ${time}
  7. * @version: V1.0
  8. */



3.字段(Fields)注釋標簽:


   
   
   
           
  1. /**
  2. * @Fields ${field} : ${todo}(用一句話描述這個變量表示什么)
  3. */

4.構造函數(Constructors)標簽:


   
   
   
           
  1. /**
  2. * @title: ${enclosing_type}
  3. * @description: ${todo}(這里用一句話描述這個方法的作用)
  4. * @param: ${tags}
  5. * @throws:
  6. */


5.方法(Methods)標簽:


   
   
   
           
  1. /**
  2. *@title: ${enclosing_method}
  3. *@description: ${todo}
  4. *@author: fendo
  5. *@date: ${date} ${time}
  6. *${tags}
  7. *@throws:
  8. */

6.覆蓋方法(Overriding Methods)標簽:


   
   
   
           
  1. /**
  2. * @title: ${enclosing_method}
  3. * @description: ${todo}
  4. * ${tags}
  5. * ${see_to_overridden}
  6. */

7.代表方法(Delegate Methods)標簽:


   
   
   
           
  1. /**
  2. * ${tags}
  3. * ${see_to_target}
  4. */

8.Getter方法標簽:


   
   
   
           
  1. /**
  2. * @title: ${enclosing_method}
  3. * @description: ${todo}
  4. * @return: ${field_type}
  5. */

9.Setter方法標簽:


   
   
   
           
  1. /**
  2. * @title: ${enclosing_method}
  3. * @description: ${todo}
  4. * @return: ${field_type}
  5. */

另一類是代碼模板如下:



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




完整的模板如下:


   
   
   
           
  1. <?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">/**
  2. * @title: ${enclosing_method}
  3. * @description: ${todo}
  4. * @return: ${field_type}
  5. */ </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">/**
  6. * @title: ${enclosing_type}
  7. * @description: ${todo}
  8. * @param: ${tags}
  9. * @throws
  10. */ </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">/**
  11. * projectName:${project_name}
  12. * fileName:${file_name}
  13. * packageName:${package_name}
  14. * date:${date}${time}
  15. * copyright(c) 2017-2020 xxx公司
  16. */ </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">/**
  17. * @title: ${file_name}
  18. * @package ${package_name}
  19. * @description: ${todo}
  20. * @author: fendo
  21. * @date: ${date} ${time}
  22. * @version: V1.0
  23. */ </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">/**
  24. *@title ${enclosing_method}
  25. *@description: ${todo}
  26. *@author: fendo
  27. *@date: ${date} ${time}
  28. *${tags}
  29. *@throws
  30. */ </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">/**
  31. * @title: ${enclosing_method}
  32. * @description: ${todo}
  33. * ${tags}
  34. * ${see_to_overridden}
  35. */ </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">/**
  36. * @title: ${enclosing_method}
  37. * @description: ${todo}
  38. * @return: ${field_type}
  39. */ </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">/**
  40. * @Fields ${field} : ${todo}
  41. */ </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">/**
  42. * ${tags}
  43. * ${see_to_target}
  44. */ </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


   
   
   
           
  1. /**
  2. * projectName: ${PROJECT_NAME}
  3. * fileName: ${NAME}.java
  4. * packageName: ${PACKAGE_NAME}
  5. * date: ${YEAR}-${MONTH}-${DAY} ${TIME}
  6. * copyright(c) 2017-2020 xxx公司
  7. */
  8. #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
  9. /**
  10. * @version: V1.0
  11. * @author: fendo
  12. * @className: ${NAME}
  13. * @packageName: ${PACKAGE_NAME}
  14. * @description: ${DESCRIPTION}
  15. * @data: ${YEAR}-${MONTH}-${DAY} ${TIME}
  16. **/
  17. public class ${NAME} {
  18. }


3.2.1、live templates


Live Template用中文應該叫做熱加載模板。它的原理就是配置一些常用代碼字母縮寫,在輸入簡寫時可以出現你預制的模板內容,使得開發效率大大提高。


在配置當中找到Live Template,右邊加號先添加一個TemplateGroup



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




代碼注釋模板如下:


   
   
   
           
  1. /**
  2. * @title: $file_name$
  3. * @package $package_name$
  4. * @description:
  5. * @author: $author$
  6. * @date: $date$ $time$
  7. * @version: V1.0
  8. */


注意:

這里的變量是$$括起來的!!

然后點擊




選擇Everywhere




然后選擇JAVA




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




注意:

默認值需要用""括起來!!


內置函數詳細請參考:https://www.jetbrains.com/help/idea/live-template-variables.html


方法注釋如下:


   
   
   
           
  1. /**
  2. *@title: $enclosing_method$
  3. *@description: TODO
  4. *@author: $author$
  5. *@date: $date$ $time$
  6. *@param: $param$
  7. *@return: $return$
  8. *@throws:
  9. */



其中的param也可以使用:


   
   
   
           
  1. 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 目錄下,其他系統目錄位置如下:


   
   
   
           
  1. Windows: C:\Users\xxxx\.IntelliJIdea2017.2\config
  2. Linux: ~/. <product name> <version number>/config/templates
  3. 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


   
   
   
           
  1. @Test
  2. public void test() {
  3. }



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM