clean code - esercitazioni ingegneria software · clean code @mariosangiorgio. why? goals readable,...
TRANSCRIPT
![Page 1: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/1.jpg)
Clean Code@mariosangiorgio
![Page 2: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/2.jpg)
Why?
![Page 3: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/3.jpg)
Goals
Readable, maintainable and extendable code
![Page 4: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/4.jpg)
Meaningful names
![Page 5: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/5.jpg)
A simple example
public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list1.add(x); return list1;}
![Page 6: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/6.jpg)
A simple example
public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list1.add(x); return list1;}
This code is quite simple but what does it do?
![Page 7: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/7.jpg)
A simple example
public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list1.add(x); return list1;}
This code is quite simple but what does it do?
Looking at it we can’t tell what it is actually doing!
![Page 8: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/8.jpg)
A simple example
public List<int[]> getFlaggedCells() { List<int[]> flaggedCells = new ArrayList<int[]>(); for (int[] cell : gameBoard) if (cell[STATUS_VALUE] == FLAGGED) flaggedCells.add(x); return flaggedCells;}
Is this code any better?
![Page 9: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/9.jpg)
A simple example
public List<Cell> getFlaggedCells() { List<Cell> flaggedCells = new ArrayList<Cell>(); for (Cell cell : gameBoard) if (cell.isFlagged()) flaggedCells.add(x); return flaggedCells;}
What about this?
![Page 10: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/10.jpg)
A simple exampleWhat we have done:
![Page 11: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/11.jpg)
A simple exampleWhat we have done:
used intention revealing names
flaggedCells rather than list1
![Page 12: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/12.jpg)
A simple exampleWhat we have done:
used intention revealing names
flaggedCells rather than list1
replaced magic numbers with constants
cell[STATUS_VALUE] rather than x[0]
![Page 13: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/13.jpg)
A simple exampleWhat we have done:
used intention revealing names
flaggedCells rather than list1
replaced magic numbers with constants
cell[STATUS_VALUE] rather than x[0]
created an appropriate abstract data type
Cell cell rather than int[] cell
![Page 14: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/14.jpg)
Another example
int d; What does it mean?Days? Diameter? ...
![Page 15: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/15.jpg)
Another example
int d; What does it mean?Days? Diameter? ...
int d; //elapsed time in days
Is this any better?
![Page 16: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/16.jpg)
Another example
int d; What does it mean?Days? Diameter? ...
int d; //elapsed time in days
Is this any better?
int elapsedTimeInDays;
What about this?
![Page 17: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/17.jpg)
Functions
![Page 18: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/18.jpg)
Do one thing
public bool isEdible() { if (this.ExpirationDate > Date.Now && this.ApprovedForConsumption == true && this.InspectorId != null) { return true; } else { return false; }}
How many things is the function doing?
![Page 19: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/19.jpg)
Do one thing
public bool isEdible() { if (this.ExpirationDate > Date.Now && this.ApprovedForConsumption == true && this.InspectorId != null) { return true; } else { return false; }}
How many things is the function doing?
1.check expiration2.check approval3.check inspection4.answer the request
![Page 20: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/20.jpg)
Do one thing
public bool isEdible() { return isFresh() &&
isApproved() && isInspected();
}
Now the function is doing one thing!
![Page 21: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/21.jpg)
Do one thing
public bool isEdible() { return isFresh() &&
isApproved() && isInspected();
}
Now the function is doing one thing!
A change in the specifications turns into a single change in the code!
![Page 22: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/22.jpg)
Don’t mix levels of abstraction
public void doTheDomesticThings() { takeOutTheTrash(); walkTheDog(); for (Dish dish : dirtyDishStack) { sink.washDish(dish); teaTowel.dryDish(dish); }}
public void doTheDomesticThings() { takeOutTheTrash(); walkTheDog(); doTheDishes();}
Which one is easier to read and understand?
![Page 23: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/23.jpg)
Separate commands and queries
AVOID SIDE EFFECTS!
Commands should only do something
(One thing)
Queries should only answer
somethingpublic class Car{ private boolean isOn
public void turnOn(){ isOn = true; }
public boolean isOn(){ return isOn; }}
![Page 24: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/24.jpg)
Use exceptions
public int foo(){ ...}
public void bar(){ if(foo() == OK) ... else // error handling}
![Page 25: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/25.jpg)
Use exceptions
public int foo(){ ...}
public void bar(){ if(foo() == OK) ... else // error handling}
Errors have to be encoded
![Page 26: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/26.jpg)
Use exceptions
public int foo(){ ...}
public void bar(){ if(foo() == OK) ... else // error handling}
Errors have to be encoded
Checks (when performed) require a lot of code
![Page 27: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/27.jpg)
Use exceptions
public int foo(){ ...}
public void bar(){ if(foo() == OK) ... else // error handling}
Errors have to be encoded
Checks (when performed) require a lot of code
It’s harder to extend such programs
![Page 28: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/28.jpg)
Use exceptionspublic void foo() throws FooException{ ...}
public void bar(){ try{ foo(); ... } catch(FooException){ // error handling }}
![Page 29: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/29.jpg)
Use exceptionspublic void foo() throws FooException{ ...}
public void bar(){ try{ foo(); ... } catch(FooException){ // error handling }}
No need to mix return values and control values
![Page 30: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/30.jpg)
Use exceptionspublic void foo() throws FooException{ ...}
public void bar(){ try{ foo(); ... } catch(FooException){ // error handling }}
No need to mix return values and control values
Cleaner syntax
![Page 31: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/31.jpg)
Use exceptionspublic void foo() throws FooException{ ...}
public void bar(){ try{ foo(); ... } catch(FooException){ // error handling }}
No need to mix return values and control values
Easier to extend
Cleaner syntax
![Page 32: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/32.jpg)
Don’t Repeat Yourself
DO NOT EVER COPY AND PASTE CODE
public void bar(){ foo(“A”); foo(“B”); foo(“C”);}
public void bar(){ String [] elements = {“A”, “B”, “C”};
for(String element : elements){ foo(element); }
}
![Page 33: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/33.jpg)
Don’t Repeat Yourself
DO NOT EVER COPY AND PASTE CODE
public void bar(){ foo(“A”); foo(“B”); foo(“C”);}
public void bar(){ String [] elements = {“A”, “B”, “C”};
for(String element : elements){ foo(element); }
}
Logic to handle the elements it’s written once for all
![Page 34: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/34.jpg)
Comments
![Page 35: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/35.jpg)
Explain yourself in the code
Which one is clearer?
// Check to see if the employee is eligible for full benefitsif ((employee.flags & HOURLY_FLAG) && (employee.age > 65))
if (employee.isEligibleForFullBenefits())
![Page 36: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/36.jpg)
Comments
API Documentation
Explanation of intent
Clarification
GOOD BAD
Redundant
Obsolete
Code commented-out
![Page 37: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/37.jpg)
Other code smellsWhat we don’t want to see in your code
![Page 38: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/38.jpg)
The bloatersSomething in your code grow too large
Long methods and large classes
Single responsibility principle violated
Primitive obsession and too much parameters
It is a symptom of bad design
![Page 39: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/39.jpg)
Primitive obsessionpublic Class Car{ private int red, green, blue;
public void paint(int red, int green, int blue){ this.red = red; this.green = green; this.blue = blue; }}
public Class Car{ private Color color;
public void paint(Color color){ this.color = color; }}
![Page 40: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/40.jpg)
The OO abusersObject orientation is not fully exploited
Switch statements on objects
It is better to use polymorphism
Alternative classes with different interfaces
Poor class hierarchy design
Refused bequest
![Page 41: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/41.jpg)
Switch vs polymorphismpublic Money calculatePay(Employee e) throws InvalidEmployeeType{ switch(e.type){ case COMMISSIONED: return calculateCommissionedPay(e); case HOURLY: return calculateHourlyPay(e); case SALARIED: return calculateSalariedPay(e); default: throw new InvalidEmployeeType(e.type); } }
public abstract class Employee{ public abstract Money calculatePay();}
![Page 42: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/42.jpg)
Refused bequestSubclass doesn’t use superclass methods and attributes
public abstract class Employee{ private int quota; public int getQuota(); ...}
public class Salesman extends Employee{ ... }
public class Engineer extends Employee{ ... public int getQuota(){ throw new NotSupportedException(); }}
Engineer does not use quota. It should be pushed down to Salesman
![Page 43: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/43.jpg)
The change preventersSomething is making hard to change the code
Divergent changeA class has to be changed
in several parts
Shotgun surgery A single change requires changes in several classes
![Page 44: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/44.jpg)
The dispensablesThe code contains something unnecessary
A class is not doing enough
Unused or redundant code
Class not providing logic
It isn’t something useful
![Page 45: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/45.jpg)
The couplersSome classes are too tightly coupled
Feature Envy
Inappropriate Intimacy
Message Chains
Misplaced responsibility
Classes should know as little as possible about each other
Too complex data access
![Page 46: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/46.jpg)
Feature Envypublic class Customer{ private PhoneNumber mobilePhone; ...
public String getMobilePhoneNumber(){ return “(” + mobilePhone.getAreaCode() + “)” + mobilePhone.getPrefix() + “-” + mobilePhone.getNumber(); }}
![Page 47: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/47.jpg)
Feature Envypublic class Customer{ private PhoneNumber mobilePhone; ...
public String getMobilePhoneNumber(){ return “(” + mobilePhone.getAreaCode() + “)” + mobilePhone.getPrefix() + “-” + mobilePhone.getNumber(); }}
public String getMobilePhoneNumber(){ return mobilePhone.toFormattedString(); }
![Page 48: Clean Code - Esercitazioni ingegneria software · Clean Code @mariosangiorgio. Why? Goals Readable, maintainable and extendable code. Meaningful names. A simple example ... This code](https://reader033.vdocuments.site/reader033/viewer/2022042914/5f4f2e6ccbd2e0003768edeb/html5/thumbnails/48.jpg)
Message chains
a.getB().getC().getD().getTheNeededData()
Law of Demeter: Each unit should only talk with friends
a.getTheNeededData()