Clean Code – Chapter 2: Meaningful Names
- Use Intention-Revealing Names
The name should tell you why it exists, what it does, and how it is used.
e.g.
Bad code:
public List<int[]> getThem(){
List<int[]> list1 = new ArrayList<int[]>();
for (int[] x : theList)
if (x[0] == 4)
list1.add(x);
return list1;
}Good code:
public List<int[]> getFlaggedCells(){
List<int[]> flaggedCells = new ArrayList<int[]>();
for (int[] cell : gameBoard)
if (cell[STATUS_VALUE] == FLAGGED)
flaggedCells.add(cell);
return flaggedCells;
}Better code:
public List<Cell> getFlaggedCells(){
List<Cell> flaggedCells = new ArrayList<Cell>();
for (Cell cell : gameBoard)
if (cell.isFlagged())
flaggedCells.add(cell);
return flaggedCells;
} - Avoid Disinformation
- Make Meaningful Distinctions
Bad code:
public static void copyChars(char a1[], char a2[]) {
for (int i = 0; i< a1.length; i++) {
a2[i] = a1[i];
}
}Good code:
Change the name
a1
,a2
tosource
anddestination
. - Use Pronounceable Names
这条对于我们中国人来说就不合适了,命名时还是不要用汉语拼音。
- Use Searchable Names
The length of a name should correspond to the size of its scope.
- Avoid Encodings
- Hungarian Notation
特定时代的产物,随着一些现代的集成开发环境的引入,已不建议使用了。(参考维基百科词条:匈牙利命名法)
- Member Prefix
- Interfaces and Implementations
leave interface unadorned.
prefer to encoding implementations than interfaces.
e.g. prefer to use the name
ShapeFactoryImp
rather thanIShapeFactory
.关于这一点,在上面提到的“匈牙利命名法”维基百科词条最后也有相关说明:
.NET Framework,微软新的软件开发平台,除了接口类型一般不适用匈牙利命名法。在 .NET 中,习惯在接口类型前放一个
I
(例如 Windows Forms 中的IButtonControl
接口。).NET Framework 指导方针建议程序员不要用匈牙利命名法,但是没有指明不要用系统匈牙利命名法还是匈牙利应用命名法,或者是两者都不要用。与此对比,Java 的标准库中连接口类型也不加前缀。
即 Java 的命名法则与作者的观点一致。
- Hungarian Notation
- Avoid Mental Mapping
Clarity is king.
- Class Names
noun or noun phrase names
- Method Names
verb or verb phrase names
- Don’t Be Cute
Say what you mean. Mean what you say.
- Pick One Word per Concept
- Don’t Pun
- Use Solution Domain Names
use computer science terms, algorithm names, pattern names, math terms and so forth.
- Use Problem Domain Names
When there is no “programmer-eese” for what you’re doing.
Separating solutions and problem domain concepts is part of the job of a good programmer and designer. The code has more to do with problem domain concepts should have names drawn from the problem domain.
- Add Meaningful Context
Bad code:
private void printGuessStatistics(char candidate, int count) {
String number;
String verb;
String pluralModifier;
if (count == 0) {
number = "no";
verb = "are";
pluralModifier = "s";
} else if (count == 1) {
number = "1";
verb = "is";
pluralModifier = "";
} else {
number = Integer.toString(count);
verb = "are";
pluralModifier = "s";
}
String guessMessage = String.format(
"There %s %s %s%s.", verb, number, candidate, pluralModifier);
print(guessMessage);
}Good code:
public class GuessStatisticsMessage {
private String number;
private String verb;
private String pluralModifier; public String make(char candidate, int count) {
createPluralDependentMessageParts(count);
return String.format(
"There %s %s %s%s.",
verb, number, candidate, pluralModifier);
} private void createPluralDependentMessageParts(int count) {
if (count == 0) {
thereAreNoLetters();
} else if (count == 1) {
thereIsOneLetter();
} else {
thereAreManyLetters(count);
}
} private void thereAreNoLetters() {
number = "no";
verb = "are";
pluralModifier = "s";
} private void thereIsOneLetter() {
number = "1";
verb = "is";
pluralModifier = "";
} private void thereAreManyLetters(int count) {
number = Integer.toString(count);
verb = "are";
pluralModifier = "s";
}
} - Don’t Add Gratuitous Context
Clean Code – Chapter 2: Meaningful Names的更多相关文章
- Clean Code – Chapter 3: Functions
Small Blocks and Indenting The blocks within if statements, else statements, while statements, and s ...
- Clean Code–Chapter 7 Error Handling
Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...
- Clean Code – Chapter 4: Comments
“Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments ...
- Clean Code – Chapter 6 Objects and Data Structures
Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...
- Clean Code – Chapter 5 Formatting
The Purpose of Formatting Code formatting is about communication, and communication is the professio ...
- “Clean Code” 读书笔记序
最近开始研读 Robert C.Martin 的 “Clean Code”,为了巩固学习,会把每一章的笔记整理到博客中.而这篇博文作为一个索引和总结,会陆续加入各章的笔记链接,以及全部读完后的心得体会 ...
- clean code meaningful names
---恢复内容开始--- Meaningful Names: use Intention-Revealing Names //nice,Everyone who reads your code (in ...
- [转]Clean Code Principles: Be a Better Programmer
原文:https://www.webcodegeeks.com/web-development/clean-code-principles-better-programmer/ ----------- ...
- 代码整洁之道Clean Code笔记
@ 目录 第 1 章 Clean Code 整洁代码(3星) ?为什么要整洁的代码 ?什么叫做整洁代码 第 2 章 Meaningful Names 有意义的命名(3星) 第 3 章 Function ...
随机推荐
- hadoop 错误处理机制
hadoop 错误处理机制 1.硬件故障 硬件故障是指jobtracker故障或TaskTracker 故障 jobtracker是单点,若发生故障,目前hadoop 还无法处理,唯有选择最牢靠的硬件 ...
- html css 如何将表头固定(转)
html css 如何将表头固定 position属性取值为fixed时,则元素的位置将不受滚动条的影响,而是直接依据窗口定位,这就是将表头固定的最直接方法,网上其他途径感觉都是在走弯路.但是与此同时 ...
- unity 3d 获取鼠标当前坐标
获取当前鼠标position:Input.mousePosition;
- 【网络】IP地址分配、端口号、分层
3.网络分层 OSI的七层网络结构图和TCP/IP的五层结构图 OSI七层模型OSI中的层 功能 ...
- struts2 标签库 介绍
struts2.0里的标签没有分类,只用在jsp头文件加上<%@ taglib prefix="s" uri="/struts-tags" %>就能 ...
- hdu 4577 X-Boxes 大数
java水过…… 代码如下: import java.math.*; import java.util.*; public class Main { public static void main(S ...
- 修改tomcat的默认编码
在tomcat的安装路径下查找conf文件下的server.xml文件,修改此文件即可,修改内容如下: <Connector port="8080" protocol=&qu ...
- HeadFirst设计模式之命令模式
一. 1.因为是操作经常变化,所以封装操作为command对象.You can do that by introducing “command objects” into your design. A ...
- Qt: 界面中使用中文(三种方法,QApplication::translate可指定编码)
界面中的字符串, 尽量的使用QObject::tr(text); 以便以后转换界面语言, 即使现在你还不考虑这个问题. 方法一: 每次设置时都使用: button->setText(QAppl ...
- Android2.3.7源码结构分析
对Andorid系统进行分析或者系统功能定制的时候,我们经常需要在众多文件中花费大量时间定位所需关注的部分.为了减轻这部分枯燥而不可避免的工作,本文对2.3.7版本的源码结构进行了简单分析.希望对刚加 ...