The style of coding of good Java programmers

Reprinted from the  posture | The style of writing code of excellent Java programmers

Today, I came across a whim, and I am very interested in coding habits and programming styles, so I found some information on programming styles (Java articles), hoping to help students who love coding or start learning coding!


From "The Elements of Java Style" - "The Java Programming Style", a book worth reading, it will save you a lot of time on the details, and the collaboration is more enjoyable!


Not much to say about the benefits, but a few principles are as follows 

1. Keep the original style

2. Adhere to the principle of least surprise

3. Do it right the first time

4. Document all non-normative behavior

format specification


1. Indent nested code: Add indentation to each code block and nesting, indent the code, and enhance readability. These places include [class definitions, inner class definitions, method definitions, static blocks, for loop statements, if-else statements, try, catch and finally blocks, anonymous inner classes, while statements, do-while statements]


2. Break long sentences: First, if commas are included, start a new line after each comma, aligning each expression after the comma with the first letter of the expression before the comma. Second, the line should be broken before the operator with the lowest precedence.


3. Use white space: between the keyword and the left parenthesis, between the right parenthesis and the following keyword, and between operators other than "." and the expressions before and after it are separated by spaces. A blank line should be added between each logically independent method and code segment, between members of a defined class or interface, and between each class and interface.


4. Don't use the Tab controller directly: Different environments interpret the Tab controller differently.


naming convention


1. The name should have practical meaning

2. Use familiar names

3. Be careful with long names and use concise and generic abbreviations

4. Keep the vowels as much as possible

5. Capitalize the first letter of abbreviations

6. Don't use case to distinguish names

package naming


1. Use the reverse lowercase version of your organization's domain name as the root qualifier for the package

2. Use a separate lowercase word for the root name of each package

3. Only when the old and new versions are binary compatible, the package can use the same name, otherwise, please use the new name


type naming


1. Capitalize the first letter of each word in class and interface names


class naming


1. Name classes with nouns

2. Class names with groups of related properties, static services or constants use plural form


接口命名


1.用名词或者形容词命名接口


方法命名


1.方法名中的第一个单词小写,其后每个单词的第一个字母大写

2.用动词命名方法

3.遵循JavaBean中命名属性访问函数方法:set,get,is


变量命名


1.变量命中的第一个单词小写,其后的每个单词的第一个字母大写

2.用名词命名变量

3.集合引用名要用复数形式

4.为不重要的临时变量简历并使用一套标准名字


字段命名


1.使用this字段变量可以区分开局部变量


参数命名


1.构造函数或者”set”方法给字段分配参数赋值,参数名应该和字段名相同


常量命名


1.常量的每个单词均大写,单词之间使用下划线连接


文档约定


1.为使用和维护你的代码的人编写文档

2.注释和代码要同步

3.使用积极的语气,省略无用的词语


注释类型


1.用文档注释来描述编程接口

2.用标准注释格式隐藏代码而不必删除它们

3.用单行注释解释实现细节


文档注释


1.在编写代码前描述编程接口

2.为公用,受保护,包,私有成员建立文档

3.为每个包编写总结和概述

4.为包的每个应用程序或组编写概述


注释风格


1.对所有文档注释使用统一的格式和组织结构

2.关键字,标识符和常量应放到<code>…</code>标签中

3.将代码放入<pre>…</pre>标签中

4.在标识符第一次出现的时候用{@link}标签

5.为Javadoc标签简历并使用一套固定的顺序

6.使用第三人称叙述的形式

7.编写独立的概述

8.省略概述中动作和服务的主语

9.省略事物概述中的对象和动词

10.使用this而不是the来指代当前类中的实例

11.方法名或者构造函数名不需圆括号,除非你想突出一个特殊的签名


注释内容


1.每个类、接口、字段和方法都编写概述

2.完整描述每个方法的签名

3.包含示例

4.为前置、后置、不变条件编写文档

5.为已知的缺陷和不足编写文档

6.为同步语法编写文档


内部注释


1.仅添加有助于理解你的代码的内部注释

2.描写代码为什么这样做,而不是在做什么

3.避免使用行尾注释

4.用行尾注释解释局部变量声明

5.建立并使用一套关键词来标识尚未解决的问题

6.在嵌套程度高的控制结构中标记出嵌套结束位置

7.如果两个case标记之间没有break语句,就在中间加入“fall-through”注释

8.标记空语句


编程约定


1.将表示基础数据类型的类声明为final类型

2.通过本地类型和其他具体类型建立具体类型

3.定义小的类和小的方法

4.定义子类,以便任何使用超类的地方都可以使用子类

5.使所有字段私有

6.使用多态来替代instanceof


类型安全


1.以java.lang.Object包装通用类,提供静态类型检查

2.以类的形式封装枚举类型

3.尽量使用泛型


语句和表达式


1.用等价的方法替换重复的、复杂的表达式

2.使用块语句代替控制流结构的表达式

3.使用括号明确操作顺序

4.在switch语句中的最后一个case体使用break语句

5.使用equals(),而不是==来检测对象的对等关系


构造


1.构造状态有效的对象

2.不要从构造函数中调用非final方法

3.用嵌套的构造函数消除冗余代码


异常处理


1.使用不受检查、运行时的异常来报告可能在程序逻辑中出错的严重未查明错误

2.使用检查异常来报告可能发生,而在正常的程序运行时极少发生的错误

3.用返回代码报告可预知的状态改变

4.仅转化异常来添加信息

5.不要私自处置运行时或者错误异常

6.用finally语句块释放资源


断言


1.按照约定编程

2.用无用代码消除机制实现断言

3.用断言捕捉代码中的逻辑错误

4.用断言检测方法的前置条件和后置条件


并发


1.仅在适当的地方使用线程


同步


1.避免同步

2.用同步的包装器,提供同步接口

3.如果方法包含几个不需要同步的重要操作,那么不要同步整个方法

4.读写实例变量的时候避免不必要的同步

5.使用notify()而不是notifyAll()

6.为同步初始化使用双重检查模式


效率


1.使用懒惰初始化

2.避免创建不必要的对象

3.重新初始化并重新使用对象,尽量不要新建对象

4.把优化工作留在日后


打包约定


1.将经常使用、更改、同时发布或者互相依存的类型,放在同一个包里

2.共同封闭原则

3.重用/发布等价原则

4.无环依赖原则

5.将不稳定的类和接口隔离在单独的包中

6.易于修改的包不要依赖于难以修改的包

7.最大化抽象最大化稳定性

8.将高层设计和架构作为稳定的抽象,组织为稳定的包


以上仅是一些规则简单的罗列,推荐阅读此书。




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324685298&siteId=291194637