Clean code tips
-
Upload
juanma-gomez -
Category
Technology
-
view
273 -
download
1
description
Transcript of Clean code tips
![Page 1: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/1.jpg)
Clean Code TipsSeptember 2013
@JuanmaGomeR
![Page 2: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/2.jpg)
How do we code?
It is done when (we think) it is working.
We invest the minimum amount time to get the best
results. Are you sure?
@JuanmaGomeR
![Page 3: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/3.jpg)
@JuanmaGomeR
Smells
![Page 4: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/4.jpg)
Economic smells
● Over 80% of time spent reading code instead of writing it.
● High maintenance cost.● Unmotivated PEOPLE.
@JuanmaGomeR
![Page 5: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/5.jpg)
Technical smells
● Classes with 2.000 lines of code● Methods with over 200 lines of
code● Lots of comments trying to
explain what the code is supposed to do
● Duplicated lines of code (Copy & Paste everywhere) @JuanmaGomeR
![Page 6: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/6.jpg)
Why do I have to take care of it?
Because you write it
Every time you write bad code you are being part of
the problem
@JuanmaGomeR
![Page 7: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/7.jpg)
Good Practices
@JuanmaGomeR
![Page 8: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/8.jpg)
Intention-revealing names
Your classes, methods, functions and attributes
should tell you why they are there for
@JuanmaGomeR
![Page 9: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/9.jpg)
Do what you are supposed to do
Each function, method or class only has to contain the
business logic that they declare
@JuanmaGomeR
![Page 10: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/10.jpg)
Do what you are supposed to do
If you are writing an user-and-password-checking
method, don’t start a new session in the system, just
check if the user and password are correct
@JuanmaGomeR
![Page 11: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/11.jpg)
Don’t Repeat Yourself principle
Don’t clone code, reuse it instead
@JuanmaGomeR
![Page 12: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/12.jpg)
Don’t Repeat Yourself principle
Every time you copy & paste code, you are exponentially
rising the probability of having problems in your next
change
@JuanmaGomeR
![Page 13: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/13.jpg)
Single responsibility principle
There should be only one possible reason to modify a class, method or whatever
artifact in your project
@JuanmaGomeR
![Page 14: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/14.jpg)
Single responsibility principle
They need to have just ONE RESPONSIBILITY each
@JuanmaGomeR
![Page 15: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/15.jpg)
Small number of arguments
A small number of arguments per method or function indicates that we are...
@JuanmaGomeR
![Page 16: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/16.jpg)
Small number of arguments
… sharing responsibilities
@JuanmaGomeR
![Page 17: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/17.jpg)
Small number of arguments
… creating more reusable data structures
@JuanmaGomeR
![Page 18: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/18.jpg)
Small number of arguments
@JuanmaGomeR
… decreasing the amount of time spent on understanding the purpose of this piece of code
![Page 19: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/19.jpg)
Small artifacts
Small packages, classes, methods, functions are a signal of good code health
@JuanmaGomeR
![Page 20: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/20.jpg)
Include comments in complex algorithms only
This way we reduce the “I have found comments that say a different thing that
the code” risk
@JuanmaGomeR
![Page 21: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/21.jpg)
Include comments in complex algorithms only
Your code must be readable and understandable by itself
@JuanmaGomeR
![Page 22: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/22.jpg)
Tips to achieve this good practices
@JuanmaGomeR
![Page 23: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/23.jpg)
… to Puppet@JuanmaGomeR
From G
odzilla...
![Page 24: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/24.jpg)
Set a maximum number of lines of code
Limit the amount of allowed code lines per class, method
and function
@JuanmaGomeR
![Page 25: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/25.jpg)
Set a maximum number of lines of code
I.E. A maximum of 10 lines for methods and functions,
100 for classes
@JuanmaGomeR
![Page 26: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/26.jpg)
Broken Phone
@JuanmaGomeR
![Page 27: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/27.jpg)
Avoid comments
Every time you need to write a comment, read the code
you have written again
@JuanmaGomeR
![Page 28: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/28.jpg)
Work in pairs
@JuanmaGomeR
![Page 29: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/29.jpg)
Invite people to read your code
Before commiting a line of code, ask your partner for
reading it
@JuanmaGomeR
![Page 30: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/30.jpg)
Invite people to read your code
Does he/she understand what it is intended to do?
@JuanmaGomeR
![Page 31: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/31.jpg)
Code is free
@JuanmaGomeR
![Page 32: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/32.jpg)
If you need to write more classes in order to reuse your
code, DO IT, THEY ARE FREE!
Use more classes
@JuanmaGomeR
![Page 33: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/33.jpg)
Lets start a refactoring exercise to put in practices
this principles
Now, do it
@JuanmaGomeR
![Page 34: Clean code tips](https://reader034.fdocuments.us/reader034/viewer/2022052600/5585bc2fd8b42a5e5a8b4da2/html5/thumbnails/34.jpg)
Is there any volunteer in the room?
Now, do it
@JuanmaGomeR