文章目录
  1. 1. Character 2 命名相关
  2. 2. Character 3 函数
  3. 3. Character 4 注释

Character 2 命名相关

  • 接口实现不要 I 打头了
  • 类名:名词 or 名词短语
  • 方法名:动词 or 动词短语
  • 名词长短应与其作用域大小相对应
  • 作有意义的区分

    不要废话,例如 data/string/info/ 表名中的 table 等后缀
    a/an/the 前缀

  • 避免误导

    即赋予 list 等保留字语义

  • 不要添加没用的语境

Character 3 函数

  • 函数就该短短短短短

    呃,这个暂时无法认同(做不到)

  • 函数应该做一件事.做好这件事.只做件事.
  • 判断函数是否不止做了一件事,可以看看是否可以再拆分,拆分出来的函数不仅只是单纯地诠释其实现
  • 理清抽象层级并且隔离开

    不让细节混杂

  • switch “将switch语句埋到抽象工厂底下”

    理解吃力

  • 使用描述性的名称
  • 函数参数少少少
  • 尽量返回参数,不要通过更改入参的方式达到目的
  • 不要传 boolean
  • 没有自然联系的不要二元入参
  • 三元入参更是杀杀杀
  • 可以把入参的语义加到函数命名中有利于解释函数的意图/参数的顺序和意图
  • 分隔指令和询问>即一个函数只做一件事
  • 使用异常替代返回错误码>把错误处理码从主路径代码中分离出来
  • 抽离 try/catch 代码块>简单来说即把 try 中的东西和 catch 中的东西抽出来(例如 logger.log()变为 logError(e)函数)
  • 附一下该章fitnesse 整改后的代码和原代码;回头再读的时候进行比较
    原代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public static String testableHtml( PageData pageData,
boolean includeSuiteSetup
) throws Exception {
WikiPage wikiPage = pageData.getWikiPage(); StringBuffer buffer = new StringBuffer(); if (pageData.hasAttribute("Test")) {
if (includeSuiteSetup) {
WikiPage suiteSetup =
PageCrawlerImpl.getInheritedPage( SuiteResponder.SUITE_SETUP_NAME, wikiPage
);
if (suiteSetup != null) {
WikiPagePath pagePath = suiteSetup.getPageCrawler().getFullPath(suiteSetup);
String pagePathName = PathParser.render(pagePath); buffer.append("!include -setup .")
.append(pagePathName) .append("\n");
}
}
WikiPage setup = PageCrawlerImpl.getInheritedPage("SetUp", wikiPage);
if (setup != null) {
WikiPagePath setupPath =
wikiPage.getPageCrawler().getFullPath(setup); String setupPathName = PathParser.render(setupPath); buffer.append("!include -setup .")
.append(setupPathName) .append("\n");
}
}
buffer.append(pageData.getContent()); if (pageData.hasAttribute("Test")) {
WikiPage teardown = PageCrawlerImpl.getInheritedPage("TearDown", wikiPage);
if (teardown != null) {
WikiPagePath tearDownPath =
wikiPage.getPageCrawler().getFullPath(teardown);
String tearDownPathName = PathParser.render(tearDownPath); buffer.append("\n")
.append("!include -teardown .") .append(tearDownPathName) .append("\n");
}
if (includeSuiteSetup) {
WikiPage suiteTeardown =
PageCrawlerImpl.getInheritedPage( SuiteResponder.SUITE_TEARDOWN_NAME, wikiPage
);
if (suiteTeardown != null) {
WikiPagePath pagePath = suiteTeardown.getPageCrawler().getFullPath (suiteTeardown);
String pagePathName = PathParser.render(pagePath); buffer.append("!include -teardown .")
.append(pagePathName) .append("\n");
}
}
} pageData.setContent(buffer.toString()); return pageData.getHtml();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package fitnesse.html;
import fitnesse.responders.run.SuiteResponder;
import fitnesse.wiki.*;
public class SetupTeardownIncluder {
private PageData pageData;
private boolean isSuite;
private WikiPage testPage;
private StringBuffer newPageContent;
private PageCrawler pageCrawler;
public static String render(PageData pageData) throws Exception {
return render(pageData, false);
}
public static String render(PageData pageData, boolean isSuite)
throws Exception {
return new SetupTeardownIncluder(pageData).render(isSuite);
}
private SetupTeardownIncluder(PageData pageData) {
this.pageData = pageData;
testPage = pageData.getWikiPage();
pageCrawler = testPage.getPageCrawler();
newnewPageContent = new StringBuffer();
}
private String render(boolean isSuite) throws Exception {
this.isSuite = isSuite;
if (isTestPage())
includeSetupAndTeardownPages();
return pageData.getHtml();
}
private boolean isTestPage() throws Exception {
return pageData.hasAttribute("Test");
}
private void includeSetupAndTeardownPages() throws Exception {
includeSetupPages();
includePageContent();
includeTeardownPages();
updatePageContent();
}
private void includeSetupPages() throws Exception {
if (isSuite)
includeSuiteSetupPage();
includeSetupPage();
}
private void includeSuiteSetupPage() throws Exception {
include(SuiteResponder.SUITE_SETUP_NAME, "-setup");
}
private void includeSetupPage() throws Exception {
include("SetUp", "-setup");
}
private void includePageContent() throws Exception {
newPageContent.append(pageData.getContent());
}
private void includeTeardownPages() throws Exception {
includeTeardownPage();
if (isSuite)
includeSuiteTeardownPage();
}
private void includeTeardownPage() throws Exception {
include("TearDown", "-teardown");
}
private void includeSuiteTeardownPage() throws Exception {
include(SuiteResponder.SUITE_TEARDOWN_NAME, "-teardown");
}
private void updatePageContent() throws Exception {
pageData.setContent(newPageContent.toString());
}
private void include(String pageName, String arg) throws Exception {
WikiPage inheritedPage = findInheritedPage(pageName);
if (inheritedPage != null) {
String pagePathName = getPathNameForPage(inheritedPage);
buildIncludeDirective(pagePathName, arg);
}
}
private WikiPage findInheritedPage(String pageName) throws Exception {
return PageCrawlerImpl.getInheritedPage(pageName, testPage);
}
private String getPathNameForPage(WikiPage page) throws Exception {
WikiPagePath pagePath = pageCrawler.getFullPath(page);
return PathParser.render(pagePath);
}
private void buildIncludeDirective(String pagePathName, String arg) {
newPageContent
.append("\n!include ")
.append(arg)
.append(" .")
.append(pagePathName)
.append("\n");
}
}

Character 4 注释

  • 少量注释的整洁而有表达力的代码,要比带有大量注释的零碎而复杂的代码好
  • 用代码来阐述, eg:
1
better than

//check to see if the employee is eligible for full benefits
if(employee.flags&HOURLY_FLAG)&&(employee.age > 65))```

  • TODO
  • 放大=>放大某种看起来不合理之物的重要性
  • 坏注释:
    1. 注释掉的代码
    2. 非本地信息
  • 别让注释本身还要被解释
文章目录
  1. 1. Character 2 命名相关
  2. 2. Character 3 函数
  3. 3. Character 4 注释