一、Java代码约定
语法告诉我们what code it is possible to write—what the machine will understand,而约定(style、conventions)告诉我们what you ought to write—what the humans reading the code will understand.
按照一致、简单的约定写出的代码,将是maintainable, robust, and contain fewer bugs.
为什么需要约定,Sun给出的原因有:
80% of the lifetime cost of a piece of software goes to maintenance.
Hardly any software is maintained for its whole life by the original author.
Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
编写Java代码常用的约定有两个:
Sun公司的《The Code Conventions for the Java Programming Language》(revised and updated on April 20, 1999.)Download HTML (zip, ~62K)
Cambridge University Press, 2000 出版的《The Elements of Java Style》http://www.ambysoft.com/elementsJavaStyle.html(只有介绍)
二、TIOBE Coding Standard Methodology和Jacobe
see:http://www.tiobe.com/tpci.htm
代码美化工具:Jacobe,缺省包含了一个完全按照Sun的代码规范的格式化配置文件。