my five mantras to write better software

86
My five mantras to write better Software Raúl Ávila

Upload: raul-avila

Post on 23-Jan-2017

185 views

Category:

Software


0 download

TRANSCRIPT

Page 1: My five mantras to write better Software

My five mantras to write better Software

Raúl Ávila

Page 2: My five mantras to write better Software

Mantra

• “a sound, word, or phrase that is repeated by someone who is praying or meditating

• “a word or phrase that is repeated often or that expresses someone’s basic beliefs”

(Merrian-Webster)

Page 3: My five mantras to write better Software

Two questions

Page 4: My five mantras to write better Software

We have to pay maximum attention to our code

Page 5: My five mantras to write better Software

Improving the way we write code

1. Pair programming

2. Code reviews

3. Reading books

Page 6: My five mantras to write better Software
Page 7: My five mantras to write better Software

• Embrace main ideas as mantras

• Apply them consistently

• On a daily basis

Page 8: My five mantras to write better Software

1

Page 9: My five mantras to write better Software

Broken window theory

• “When a community tolerates minor examples of disorder (…) such as broken windows (…) people are more likely to commit more serious crimes”

Source: http://bit.ly/2ebQEgR

Page 10: My five mantras to write better Software
Page 11: My five mantras to write better Software
Page 12: My five mantras to write better Software
Page 13: My five mantras to write better Software

1. “Don’t leave broken windows”

Page 14: My five mantras to write better Software

As soon as you see a broken window…fix it!

But we have to deliver!

Page 15: My five mantras to write better Software

Two levels

1. Stop the world!

2. Next iteration

Page 16: My five mantras to write better Software

Examples

Page 17: My five mantras to write better Software
Page 18: My five mantras to write better Software

Inconsistent naming conventions

Page 19: My five mantras to write better Software

TODO comments

Page 20: My five mantras to write better Software

Unclear code formatting style

Page 21: My five mantras to write better Software

Commented out code

Page 22: My five mantras to write better Software
Page 23: My five mantras to write better Software

• Dead code

• Unused imports

• Ignored tests

• Unused dependencies

• Several dependencies to do the same thing

Page 24: My five mantras to write better Software

• No README file

• Unclear Git strategy

• Manual steps in deployments

• Unstable CI

Delivery practices

Page 25: My five mantras to write better Software

2

Page 26: My five mantras to write better Software
Page 27: My five mantras to write better Software
Page 28: My five mantras to write better Software
Page 29: My five mantras to write better Software

2. “Extract till you drop”

Page 30: My five mantras to write better Software

Short term memory

Between 5 and 9 items

Page 31: My five mantras to write better Software
Page 32: My five mantras to write better Software

Extract variables

Page 33: My five mantras to write better Software
Page 34: My five mantras to write better Software
Page 35: My five mantras to write better Software
Page 36: My five mantras to write better Software
Page 37: My five mantras to write better Software

Extract functions

Page 38: My five mantras to write better Software
Page 39: My five mantras to write better Software
Page 40: My five mantras to write better Software
Page 41: My five mantras to write better Software
Page 42: My five mantras to write better Software
Page 43: My five mantras to write better Software

Benefits• Extracting increases readability

• Indentation is reduced drastically

• Makes responsibilities arise

• Avoids big balls of mud

• Facilitates benchmarking

Page 44: My five mantras to write better Software
Page 45: My five mantras to write better Software

Not extracting

Page 46: My five mantras to write better Software
Page 47: My five mantras to write better Software

Extract classes

• When a function is hard to split (i.e. it has many parameters)…maybe it should be a class

• Or maybe two classes!

• More on this later

Page 48: My five mantras to write better Software

Extract packages

Page 49: My five mantras to write better Software
Page 50: My five mantras to write better Software

3

Page 51: My five mantras to write better Software
Page 52: My five mantras to write better Software

3. “Do one thing, do it well, and do it only”

(Single Responsibility Principle)

Page 53: My five mantras to write better Software
Page 54: My five mantras to write better Software

What is (or what defines) a responsibility?

It depends…

Page 55: My five mantras to write better Software

Functions

1. Its name describes accurately what it does

2. Such name does not have conjunctions (And / Or)

3. It does not have side effects

Page 56: My five mantras to write better Software
Page 57: My five mantras to write better Software
Page 58: My five mantras to write better Software

Classes

• Definition of class: “family of functions with a clear and well defined objective”

• This objective is the responsibility of the class

• Responsibility as a bunch of solutions to an actor

Page 59: My five mantras to write better Software

Actors / Roles

• Final user

• External systems

• Accountants / Stakeholders

• Software Architects

Page 60: My five mantras to write better Software

Classes and actors

A class must satisfy the needs of one actor, and that actor only

Page 61: My five mantras to write better Software
Page 62: My five mantras to write better Software
Page 63: My five mantras to write better Software
Page 64: My five mantras to write better Software
Page 65: My five mantras to write better Software
Page 66: My five mantras to write better Software
Page 67: My five mantras to write better Software

If we do more than one thing

• Lines of code will grow without control

• Share code between different responsibilities

• Heterogeneous collaborators

• Classes are difficult to test

• Our code will be very hard to change

Page 68: My five mantras to write better Software

How to do one thing

1. Testing (TDD)

2. Extracting

Page 69: My five mantras to write better Software

4

Page 70: My five mantras to write better Software
Page 71: My five mantras to write better Software
Page 72: My five mantras to write better Software

4. “Abstractions in code, details in data”

Page 73: My five mantras to write better Software

0 2 20 39

Id Name Address

Parsing a message

Page 74: My five mantras to write better Software
Page 75: My five mantras to write better Software
Page 76: My five mantras to write better Software
Page 77: My five mantras to write better Software
Page 78: My five mantras to write better Software

Benefits• More robust design, as there are more immutable

parts

• We can code without knowing all the details (defer details)

• Facilitates testing, as we can test the abstraction, not specifics

• Details that are likely to change are not spread out in the code, so it’s faster to find them

Page 79: My five mantras to write better Software

Get details out of your code!• Data structures (OK, still code :))

• Properties files / YAML

• Environment variables

• Datastores

• Configuration server

Page 80: My five mantras to write better Software

One exception

Tests

Page 81: My five mantras to write better Software

5

Page 82: My five mantras to write better Software

Failing test

Make it passRefactor

TDD

Page 83: My five mantras to write better Software

Failing test

Make it better

Make it work

Make it pretty

Page 84: My five mantras to write better Software

5. “Make it work, make it better, make it pretty”

Page 85: My five mantras to write better Software

My five mantras

1. Don’t leave broken windows

2. Extract till you drop

3. Do one thing, do it well, and do it only

4. Abstractions in code, details in data

5. Make it work, make it better, make it pretty

Page 86: My five mantras to write better Software

Questions?

Other mantras?

@_Raul_Avila

[email protected]