What is 'good code'?
'Good code' is code that works, is bug free, and is readable and maintainable. Some organizations have coding 'standards' that all developers are supposed to adhere to, but everyone has different ideas about what's best, or what is too many or too few rules. There are also various theories and metrics, such as McCabe Complexity metrics. It should be kept in mind that excessive use of standards and rules can stifle productivity and creativity. 'Peer reviews', 'buddy checks' code analysis tools, etc. can be used to check for problems and enforce standards.
For C and C++ coding, here are some typical ideas to consider in setting rules/standards; these may or may not apply to a particular situation:
- minimize or eliminate use of global variables.
- use descriptive function and method names - use both upper and lower case, avoid abbreviations, use as many characters as necessary to be adequately descriptive (use of more than 20 characters is not out of line); be consistent in naming conventions.
- use descriptive variable names - use both upper and lower case, avoid abbreviations, use as many characters as necessary to be adequately descriptive (use of more than 20 characters is not out of line); be consistent in naming conventions.
- function and method sizes should be minimized; less than 100 lines of code is good, less than 50 lines is preferable.
- function descriptions should be clearly spelled out in comments preceding a function's code.
- organize code for readability.
- use whitespace generously - vertically and horizontally
- each line of code should contain 70 characters max.
- one code statement per line.
- coding style should be consistent throught a program (eg, use of brackets, indentations, naming conventions, etc.)
- in adding comments, err on the side of too many rather than too few comments; a common rule of thumb is that there should be at least as many lines of comments (including header blocks) as lines of code.
- no matter how small, an application should include documentaion of the overall program function and flow (even a few paragraphs is better than nothing); or if possible a separate flow chart and detailed program documentation.
- make extensive use of error handling procedures and status and error logging.
- for C++, to minimize complexity and increase maintainability, avoid too many levels of inheritance in class heirarchies (relative to the size and complexity of the application). Minimize use of multiple inheritance, and minimize use of operator overloading (note that the Java programming language eliminates multiple inheritance and operator overloading.)
- for C++, keep class methods small, less than 50 lines of code per method is preferable.
- for C++, make liberal use of exception handlers
好的代码是代码运行正常、bug很少、并且具有可读性和可维护性。
一些企业自己有所有开发人员都必需遵守的编码规范,但是对于什么样的代码是最好的每个人的
都有自己的标准、或者有太多的或太少的编码规则。这有多种原则和标准,例如,McCable 的
复杂度度量。的确使用过多的编码标准和规则可能降低生产率和创造性。“同行评审”或“同事检查”
代码分析工具等,都能用来检查问题或坚持标准。
对于C和C++编码,这有一些典型的规则/标准;它们可能运用或不能运用到某些特殊的情况。
1、减少或不使用全局变量
2、使用特定的功能和方法名称-都使用大写或小写字母,避免使用缩写,为了表达信息可以使用
多个字符(最好不超过20个);使用约定的命名规则。
3、使用特定的变量名称-都使用大写或小写字母,避免使用缩写,为了表达信息可以使用
多个字符(最好不超过20个);使用约定的命名规则。
4、函数和方法的规模尽量小,少于100行比较好,最好少于50行。
5、在函数前面的注释中清晰的给出函数的功能。
6、使代码具有可读性.
7、使用空格。每一行最多包括70个字符。
8、每一行只有一条语句。
9、整个程序代码的风格一致(例如,括号的使用,缩进、命名规则等)。
10、在注释中有一些错误也比几乎没有注释好;一个通常的规则是注释的数量至少与代码行的数量
差不多(包括注释头)。
11、不管一个应用程序多么小,都应该包括全部的文档和流程图(至少有一段也比没有好)。如果有
可能单独有一个流程图和详细的程序文档最好。
12、广泛使用错误处理程序、状态和错误日志。对于C++,减少复杂度和提高可维护性,避免使用过多的
类继承(相对于应用程序的复杂度和规模),尽量少使用重载(注意,Java语言去掉了多重继承和操作符
重载。
13、对于C++,尽量使方法代码减少,最好每个方法少于50行代码。
14、对于C++、采用异常处理