Clean Code Tips
September 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
Smells

@JuanmaGomeR
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 userand-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

… decreasing the amount of
time spent on understanding
the purpose of this piece of
code
@JuanmaGomeR
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
From Godzilla...

… to Puppet
@JuanmaGomeR
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
Use more classes

If you need to write more
classes in order to reuse your
code, DO IT, THEY ARE
FREE!
@JuanmaGomeR
Now, do it

Lets start a refactoring
exercise to put in practices
this principles

@JuanmaGomeR
Now, do it

Is there any volunteer in the
room?

@JuanmaGomeR

Clean code tips

  • 1.
    Clean Code Tips September2013 @JuanmaGomeR
  • 2.
    How do wecode? It is done when (we think) it is working. We invest the minimum amount time to get the best results. Are you sure? @JuanmaGomeR
  • 3.
  • 4.
    Economic smells ● Over80% of time spent reading code instead of writing it. ● High maintenance cost. ● Unmotivated PEOPLE. @JuanmaGomeR
  • 5.
    Technical smells ● Classeswith 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
  • 6.
    Why do Ihave to take care of it? Because you write it Every time you write bad code you are being part of the problem @JuanmaGomeR
  • 7.
  • 8.
    Intention-revealing names Your classes,methods, functions and attributes should tell you why they are there for @JuanmaGomeR
  • 9.
    Do what youare supposed to do Each function, method or class only has to contain the business logic that they declare @JuanmaGomeR
  • 10.
    Do what youare supposed to do If you are writing an userand-password-checking method, don’t start a new session in the system, just check if the user and password are correct @JuanmaGomeR
  • 11.
    Don’t Repeat Yourselfprinciple Don’t clone code, reuse it instead @JuanmaGomeR
  • 12.
    Don’t Repeat Yourselfprinciple Every time you copy & paste code, you are exponentially rising the probability of having problems in your next change @JuanmaGomeR
  • 13.
    Single responsibility principle Thereshould be only one possible reason to modify a class, method or whatever artifact in your project @JuanmaGomeR
  • 14.
    Single responsibility principle Theyneed to have just ONE RESPONSIBILITY each @JuanmaGomeR
  • 15.
    Small number ofarguments A small number of arguments per method or function indicates that we are... @JuanmaGomeR
  • 16.
    Small number ofarguments … sharing responsibilities @JuanmaGomeR
  • 17.
    Small number ofarguments … creating more reusable data structures @JuanmaGomeR
  • 18.
    Small number ofarguments … decreasing the amount of time spent on understanding the purpose of this piece of code @JuanmaGomeR
  • 19.
    Small artifacts Small packages,classes, methods, functions are a signal of good code health @JuanmaGomeR
  • 20.
    Include comments incomplex algorithms only This way we reduce the “I have found comments that say a different thing that the code” risk @JuanmaGomeR
  • 21.
    Include comments incomplex algorithms only Your code must be readable and understandable by itself @JuanmaGomeR
  • 22.
    Tips to achievethis good practices @JuanmaGomeR
  • 23.
    From Godzilla... … toPuppet @JuanmaGomeR
  • 24.
    Set a maximumnumber of lines of code Limit the amount of allowed code lines per class, method and function @JuanmaGomeR
  • 25.
    Set a maximumnumber of lines of code I.E. A maximum of 10 lines for methods and functions, 100 for classes @JuanmaGomeR
  • 26.
  • 27.
    Avoid comments Every timeyou need to write a comment, read the code you have written again @JuanmaGomeR
  • 28.
  • 29.
    Invite people toread your code Before commiting a line of code, ask your partner for reading it @JuanmaGomeR
  • 30.
    Invite people toread your code Does he/she understand what it is intended to do? @JuanmaGomeR
  • 31.
  • 32.
    Use more classes Ifyou need to write more classes in order to reuse your code, DO IT, THEY ARE FREE! @JuanmaGomeR
  • 33.
    Now, do it Letsstart a refactoring exercise to put in practices this principles @JuanmaGomeR
  • 34.
    Now, do it Isthere any volunteer in the room? @JuanmaGomeR