05 Clean Code Trang 1 45
05 Clean Code Trang 1 45
Content
1. Introduction Clean Code
Keep it Clean, Your Mother Doesn’t Work Here!
2. Meaningful Names
3. Clean Function/Method
4. Clean Class
5. Clean Test
3
… by anyone
Going fast
”The only way to go fast, is to go well”
Robert C. Martin
• Don’t hack and skip testing just to finish at
the end of an iteration
• Write unit tests
• Fix the code
• Refactor
8
• Poor reusability
• It breaks when you change it
• Changes to component require inspecting/ modifying
more code
D.R.Y Principle
• Don’t Repeat Yourself
• Applicable whenever we copy / paste a
piece of code
• Just use a method or a class
16
K.I.S.S Principle
• Keep It Simple and Stupid
• Whenever we want to implement a method to do
all things
17
Y.A.G.N.I principle
• You Ain’t Gonna Need It
• Don’t write methods which are not yet
necessary (may be you will never need
them)
• Unused classes, methods, parameters,
variables à remove them
• Somehow related with KISS
18
Law of Demeter
Karl Lieberherr i and colleagues
• Law of Demeter: An object should know as little as possible
about the internal structure of other objects with which it
interacts – a question of coupling
• Or… “only talk to your immediate friends”
• Closely related to representation exposure and
(im)mutability
• Bad example – too-tight chain of coupling between classes
general.getColonel().getMajor(m).getCaptain(cap)
.getSergeant(ser).getPrivate(name).digFoxHole();
• Better example
general.superviseFoxHole(m, cap, ser, name);
19
S.O.L.I.D Principles
• SRP: The Single Responsibility Principle
• OCP: The Open Closed Principle
• LSP: The Liskov Substitution Principle
• ISP: The Interface Segregation Principle
• DIP: The Dependency Inversion Principle
21
Content
1. Introduction Clean Code
Keep it Clean, Your Mother Doesn’t Work Here!
2. Meaningful Names
3. Clean Function/Method
4. Clean Class
5. Clean Test
22
Example
What is the purpose of this code?
Solution
• The simplicity of this code is not changed, but much more
meaningful
VS
29
VS
30
Content
1. Introduction Clean Code
Keep it Clean, Your Mother Doesn’t Work Here!
2. Meaningful Names
3. Clean Function/Method
4. Clean Class
5. Clean Test
35
HtmlUtil.java (v2)
public static String renderPageWithSetupsAndTeardowns(
PageData pageData, boolean isSuite) throws Exception{
boolean isTestPage = pageData.hasAttribute("Test"); //low
if (isTestPage){
WikiPage testPage = pageData.getWikiPage(); // high
StringBuffer newPageContent = new StringBuffer();
includeSetupPages(testPage, newPageContent, isSuite);
newPageContent.append(pageData.getContent();
includeTeardownPages(testPage, newPageContent, isSuite);
pageData.setContent(newPageContent.toString());
}
return pageData.getHtml(); Two levels of
}
abstraction
39
HtmlUtil.java (v3)
public static String renderPageWithSetupsAndTeardowns(
PageData pageData, boolean isSuite) throws Exception{
if (isTestPage(pageData))
includeSetupAndTeardownPages(pageData, isSuite);
return pageData.getHtml();
}
4
0
41
3.2. (cont.)
• Extract Try/Catch Blocks
• Try/Catch mixes error processing with normal processing
Error Processing
• Extract bodies of try/catch out into separated functions
3.2. (cont.)
• Prefer Exceptions to Returning Error Codes
(Control Coupling)
3.2. (cont.)
• Prefer Exceptions to Returning Error Codes
(Control Coupling) – Benefits:
1. No more code for checking various error codes
2. Exception classes can implement their own method,
i.e., error handling functionality
3. Error codes can’t be used in a constructor, since a
constructor must return only a new object