Top Banner
Clean Code Tips September 2013 @JuanmaGomeR
34

Clean code tips

Jun 21, 2015

Download

Technology

Juanma Gómez

This presentation contains some tips and good practices about writing clean and maintainable code.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Clean code tips

Clean Code TipsSeptember 2013

@JuanmaGomeR

Page 2: Clean code tips

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

@JuanmaGomeR

Smells

Page 4: Clean code tips

Economic smells

● Over 80% of time spent reading code instead of writing it.

● High maintenance cost.● Unmotivated PEOPLE.

@JuanmaGomeR

Page 5: Clean code tips

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

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

Good Practices

@JuanmaGomeR

Page 8: Clean code tips

Intention-revealing names

Your classes, methods, functions and attributes

should tell you why they are there for

@JuanmaGomeR

Page 9: Clean code tips

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

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

Don’t Repeat Yourself principle

Don’t clone code, reuse it instead

@JuanmaGomeR

Page 12: Clean code tips

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

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

Single responsibility principle

They need to have just ONE RESPONSIBILITY each

@JuanmaGomeR

Page 15: Clean code tips

Small number of arguments

A small number of arguments per method or function indicates that we are...

@JuanmaGomeR

Page 16: Clean code tips

Small number of arguments

… sharing responsibilities

@JuanmaGomeR

Page 17: Clean code tips

Small number of arguments

… creating more reusable data structures

@JuanmaGomeR

Page 18: Clean code tips

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

Small artifacts

Small packages, classes, methods, functions are a signal of good code health

@JuanmaGomeR

Page 20: Clean code tips

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

Include comments in complex algorithms only

Your code must be readable and understandable by itself

@JuanmaGomeR

Page 22: Clean code tips

Tips to achieve this good practices

@JuanmaGomeR

Page 23: Clean code tips

… to Puppet@JuanmaGomeR

From G

odzilla...

Page 24: Clean code tips

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

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

Broken Phone

@JuanmaGomeR

Page 27: Clean code tips

Avoid comments

Every time you need to write a comment, read the code

you have written again

@JuanmaGomeR

Page 28: Clean code tips

Work in pairs

@JuanmaGomeR

Page 29: Clean code tips

Invite people to read your code

Before commiting a line of code, ask your partner for

reading it

@JuanmaGomeR

Page 30: Clean code tips

Invite people to read your code

Does he/she understand what it is intended to do?

@JuanmaGomeR

Page 31: Clean code tips

Code is free

@JuanmaGomeR

Page 32: Clean code tips

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

Lets start a refactoring exercise to put in practices

this principles

Now, do it

@JuanmaGomeR

Page 34: Clean code tips

Is there any volunteer in the room?

Now, do it

@JuanmaGomeR