Clean Code Tips September 2013 @JuanmaGomeR
Jun 21, 2015
Clean Code TipsSeptember 2013
@JuanmaGomeR
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
@JuanmaGomeR
Smells
Economic smells
● Over 80% of time spent reading code instead of writing it.
● High maintenance cost.● Unmotivated PEOPLE.
@JuanmaGomeR
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
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
Good Practices
@JuanmaGomeR
Intention-revealing names
Your classes, methods, functions and attributes
should tell you why they are there for
@JuanmaGomeR
Do what you are supposed to do
Each function, method or class only has to contain the
business logic that they declare
@JuanmaGomeR
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
Don’t Repeat Yourself principle
Don’t clone code, reuse it instead
@JuanmaGomeR
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
Single responsibility principle
There should be only one possible reason to modify a class, method or whatever
artifact in your project
@JuanmaGomeR
Single responsibility principle
They need to have just ONE RESPONSIBILITY each
@JuanmaGomeR
Small number of arguments
A small number of arguments per method or function indicates that we are...
@JuanmaGomeR
Small number of arguments
… sharing responsibilities
@JuanmaGomeR
Small number of arguments
… creating more reusable data structures
@JuanmaGomeR
Small number of arguments
@JuanmaGomeR
… decreasing the amount of time spent on understanding the purpose of this piece of code
Small artifacts
Small packages, classes, methods, functions are a signal of good code health
@JuanmaGomeR
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
Include comments in complex algorithms only
Your code must be readable and understandable by itself
@JuanmaGomeR
Tips to achieve this good practices
@JuanmaGomeR
… to Puppet@JuanmaGomeR
From G
odzilla...
Set a maximum number of lines of code
Limit the amount of allowed code lines per class, method
and function
@JuanmaGomeR
Set a maximum number of lines of code
I.E. A maximum of 10 lines for methods and functions,
100 for classes
@JuanmaGomeR
Broken Phone
@JuanmaGomeR
Avoid comments
Every time you need to write a comment, read the code
you have written again
@JuanmaGomeR
Work in pairs
@JuanmaGomeR
Invite people to read your code
Before commiting a line of code, ask your partner for
reading it
@JuanmaGomeR
Invite people to read your code
Does he/she understand what it is intended to do?
@JuanmaGomeR
Code is free
@JuanmaGomeR
If you need to write more classes in order to reuse your
code, DO IT, THEY ARE FREE!
Use more classes
@JuanmaGomeR
Lets start a refactoring exercise to put in practices
this principles
Now, do it
@JuanmaGomeR
Is there any volunteer in the room?
Now, do it
@JuanmaGomeR