本章主要讲解:
代码可理解性/可读性
Lines of code (LoC) – The longer a method gets, the harder it probably is to understand. 代码行越长,越难理解
Comment density (MCOMM%)
评论密度
MCOMM% = MCOMM / LOC
代码四种注释
Four types of comments in code
标题注释引入了类定义,重要函数,宏定义包,一些其他非平凡模块或整个源代码文件。 对于专有程序,标题注释通常包括版权声明
介绍性注释描述了类,函数或其他模块的用途和用法
Block comments describe the purpose and strategy of a group of related statements.
区域注释描述了一组相关声明的策略和目的。
Single-Line / Trailing / End-Of-Line comments explain an individual statement or even a part of a statement.
单行注释解释了一个个体声明或者一个声明的一部分
伪代码的六个基本操作
Six Basic Computer Operations
- Receive information from outside: Read, Get
- Put out information to outside: Print, Write, Output, Display, etc
- Perform arithmetic/computation: Compute, Calculate
- Assign value to a variable or memory location: Set, Save, Store,
- Compare and select alternate actions: If-Then-Else
- Repeat a group of actions: For, While, Do/Until
代码惯例
Use Intention-Revealing Names
用意图明显的名字
Packages should be lower case:
编码规范 :
▪ This: – package java.util; – package com.myapp.mypackage;
▪ Not This: – package javax.MyPackage – package Com.MyPackage
Classes and Interfaces should be nouns and upper case
名词小写
Methods should be verbs and start as lower case (Camel Case)
动词以小写字母开头
Constant naming: all upper case with underscores between words
全大写并伴随下划线
Ordering of Import Statements
包排序
1.标准包Standard packages (java.io, java.util, etc.)
– 2.第三方包Third party packages such as com.ibm.xml.parser – 3. 自己的包Your own packages
以上就是第四章的内容,主要讲解了代码的可读性,还有编码规范。理解即可。