The document discusses principles of clean code, including DRY (Don't Repeat Yourself), KISS (Keep It Simple Stupid), and YAGNI (You Ain't Gonna Need It). It emphasizes writing meaningful code through use of meaningful names, small stylish functions, comments when needed, and well-formatted code. Following clean code principles helps produce high-quality code and avoid issues like low performance, bugs, and project failure.
2. PRINCIPLES
• DRY -> Don’t Repeat Yourself
• KISS -> Keep it Simple Stupid
• YAGNI -> You “Ain’t Gonna Need It
GARIK KALASHYAN
3. DON’T REPEAT YOURSELF
GARIK KALASHYAN
A basic strategy for reducing complexity to managable units is to
divide a system into pieces.
4. KEEP IT SINGLE STUPID
The simplest explanation tends to be the right one.
GARIK KALASHYAN
5. YOU “AIN’T GONNA NEED IT
Do the Simplest Thing That Could Possibly Work
GARIK KALASHYAN
6. THE BOY SCOUT RULE
Leave the campground cleaner than you
found it.
GARIK KALASHYAN
7. BAD CODE
• Forget about normal refactoring
• Good performance ! – Hoho, are you sure?
• Demotivation at work
• Failure of product
GARIK KALASHYAN
8. LET’S THINK ABOUT
• Meaningful names
• Stylish functions
• Adequate comments
• Format your code
GARIK KALASHYAN
• Think a lot about objects
• Handle errors
• Have and Run tests
• Minimalism
9. MEANINGFUL NAMES
public List<int[]> getThem() {
List<int[]> list1 = new
ArrayList<int[]>();
for (int[] x : theList)
if (x[0] == 4)
list1.add(x);
return list1;
}
GARIK KALASHYAN
public List<Cell> getFlaggedCells() {
List<Cell> flaggedCells = new
ArrayList<Cell>();
for (Cell cell : gameBoard)
if (cell.isFlagged())
flaggedCells.add(cell);
return flaggedCells;
}
12. STYLISH FUNCTIONS
• Small !
• Do One Thing !
• Function is an action !
• Think a lot about arguments !
• Prefer Exceptions to Returning Error Codes
GARIK KALASHYAN
14. BAD COMMENTS
• Explain Yourself in Code
• Comment codes
• Noise comments
/**
* Returns the day of the month.
* @return the day of the month.
*/
GARIK KALASHYAN
15. GOOD COMMENTS
• Informative comments
// format matched kk:mm:ss EEE, MMM dd, yyyy
Pattern timeMatcher = Pattern.compile("d*:d*:d* w*,w* d*, d*");
• Warning of Consequences
// Don't run unless you have some time to kill.
public void _testWithReallyBigFile(){…}
• TODO Comments
GARIK KALASHYAN