“Writing code that lasts” … or writing code you won’t hate tomorrow.

77
Writing code that lasts. Rafael Dohms @rdohms photo: djandyw.com #ocforphp

Transcript of “Writing code that lasts” … or writing code you won’t hate tomorrow.

Page 1: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Writing code that lasts.

Rafael Dohms@rdohms

photo: djandyw.com

#ocforphp

Page 2: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Writing code you won’t hate tomorrow.

Rafael Dohms@rdohms

photo: djandyw.com

#ocforphp

Page 3: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Rafael Dohmsor @rdohms on twitter

Page 4: “Writing code that lasts” … or writing code you won’t hate tomorrow.

DeveloperHerder

Page 5: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Lead BackendDeveloper

Page 6: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Let me introduce you to someone…

Page 7: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 8: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 9: “Writing code that lasts” … or writing code you won’t hate tomorrow.

No Tests.

Wrote his own framework.

500 line methods.

Used Singletons!

shhh.. don’t tell Grumpy.

.. and used it.

.. and every other anti-pattern out there.

… not even methods, they were functions

Page 10: “Writing code that lasts” … or writing code you won’t hate tomorrow.

photo: Rob Allen (@akrabat)

Page 11: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 12: “Writing code that lasts” … or writing code you won’t hate tomorrow.

REWRITEALL THE CODE!

image: hyperboleandahalf

Page 13: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Real Developers, SHIP STUFF.

photo: Glyn Lowe Photoworks

Page 14: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 15: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 16: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 17: “Writing code that lasts” … or writing code you won’t hate tomorrow.

How do we find balance?

photo: Kalexanderson

Page 18: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Code has an expiration date

photo: CarbonNYC

Page 19: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Code is perishable, it rots.

photo by: massdistraction

Page 20: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Code Evolves

photo by: kevin dooley

Page 21: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Languages evolve.photo by: raneko

Page 22: “Writing code that lasts” … or writing code you won’t hate tomorrow.

You evolve.

photo by: Kaptain Kobold

Page 23: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Complexity kills Comprehension

photo: osbornb

Page 24: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Bad Design

photo: miskan

Page 25: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Bad specs

Page 26: “Writing code that lasts” … or writing code you won’t hate tomorrow.

NIH

source: Urban dictionary

photo by: John 'K'

Page 27: “Writing code that lasts” … or writing code you won’t hate tomorrow.

NIH| nɒt ɪnˈventɪd hɪə |

Not Invented Here —The German art of humiliating any technology, agricultural product, or (medical) scientific work that has not been

invented or produced in Germany, Switzerland, or Austria.source: Urban dictionary

photo by: John 'K'

Page 28: “Writing code that lasts” … or writing code you won’t hate tomorrow.

The Solution™photo: Ian Sane

Page 29: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Improve code

Page 30: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Improve codemake it easier to comprehend

Page 31: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Improve codemake it easier to comprehend

make it flexible

Page 32: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Improve codemake it easier to comprehend

make it flexible

make it tested

Page 33: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Improve codemake it easier to comprehend

make it flexible

make it tested

make it easier to replace, refactor

Page 34: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Improve codemake it easier to comprehend

make it flexible

make it tested

make it easier to replace, refactor

make it not exist

Page 35: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Testing

photo by: jeffkrause

Page 36: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Good Design Concepts

Page 37: “Writing code that lasts” … or writing code you won’t hate tomorrow.

SOLID

Single ResponsibilityOpen and Close

Liskov substitutionInterface SegregationDependency Inversion

photo by j_regan

Page 38: “Writing code that lasts” … or writing code you won’t hate tomorrow.

photo by lofink

STUPIDSingleton

Tight CouplingUntestability

Premature OptimizationIndescriptive Naming

Duplication

Page 39: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Design Patterns

photo by halloweenstock

Page 40: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Package Managers

Page 41: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Composer

Page 42: “Writing code that lasts” … or writing code you won’t hate tomorrow.

PIEProudly

InventedElsewhere

Page 43: “Writing code that lasts” … or writing code you won’t hate tomorrow.

guzzlehttp/guzzleHTTP client for RESTful API clients

photo by j_regan

Page 44: “Writing code that lasts” … or writing code you won’t hate tomorrow.

league/tacticianA flexible CommandBus implementation

Page 45: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Readability

photo: Morgenland

Page 46: “Writing code that lasts” … or writing code you won’t hate tomorrow.

paragraphs

photo: Trossachs Photography

Page 47: “Writing code that lasts” … or writing code you won’t hate tomorrow.

whitespace

Page 48: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 49: “Writing code that lasts” … or writing code you won’t hate tomorrow.

R&D Team for MIH/Naspers

São PauloCape Town

Page 50: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Object Calisthenics

photo by: boston_public_library

Page 51: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Calisthenics/ˌkaləsˈTHeniks/

Calisthenics are a form of dynamic exercise consisting of a variety of simple, often rhythmical, movements, generally using minimal

equipment or apparatus.

photo by: boston_public_library

Page 52: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Its about perception, not rules or standards

photo by: david_a_l

Page 53: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#1 Only one indentation level per method.

photo by: justinliew

Page 54: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#2 Do not use else

photo by: justinliew

Page 55: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 56: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 57: “Writing code that lasts” … or writing code you won’t hate tomorrow.
Page 58: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#3 Wrap primitive types, if they contain behavior

photo by: justinliew

Page 59: “Writing code that lasts” … or writing code you won’t hate tomorrow.

$component->repaint(false);

Page 60: “Writing code that lasts” … or writing code you won’t hate tomorrow.

$component->repaint( new Animate(false) );

Page 61: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#4 Only one -> per line

photo by: justinliew

Page 62: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#5 Do not abbreviate.

photo by: justinliew

Page 63: “Writing code that lasts” … or writing code you won’t hate tomorrow.

if($sx >= $sy) { if ($sx > $strSysMatImgW) { $ny = $strSysMatImgW * $sy / $sx; $nx = $strSysMatImgW; } if ($ny > $strSysMatImgH) { $nx = $strSysMatImgH * $sx / $sy; $ny = $strSysMatImgH; }

}

Page 64: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#6 Keep your classes small.

photo by: justinliew

Page 65: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#7 Limit your instance variables to less then 5

photo by: justinliew

Page 66: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#8 Use first class collections

photo by: justinliew

Page 67: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#9 Don’t use getters and setters.

photo by: justinliew

Page 68: “Writing code that lasts” … or writing code you won’t hate tomorrow.

#10 Document your code.

photo by: justinliew

Page 69: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Your turn.

Page 70: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Improve yourself!

Page 71: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Read lots of code!

Page 72: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Write simple code.

Page 73: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Try Object Calisthenics for a month.

Page 74: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Use someone else’s code.

Page 75: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Share bite size libraries.

Page 76: “Writing code that lasts” … or writing code you won’t hate tomorrow.

Thank you.

http://slides.doh.ms

http://doh.ms

@rdohms

we are hiring!

http://careers.symbid.com/

https://joind.in/talk/d3749