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

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

Embed Size (px)

Citation preview

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

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. - #PHPSRB16

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. - #PHPSRB16

Rafael Dohms

or @rdohms on twitter

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

DeveloperHerder

Page 5: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16
Page 6: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16
Page 7: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

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 8: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

photo: Rob Allen (@akrabat)

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

REWRITEALL THE CODE!

image: hyperboleandahalf

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

Real Developers, SHIP STUFF.

photo: Glyn Lowe Photoworks

Page 12: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16
Page 13: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16
Page 14: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16
Page 15: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

How do we find balance?

photo: Kalexanderson

Page 16: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

Code has an expiration date

photo: CarbonNYC

Page 17: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

Code is perishable, it rots.

photo by: massdistraction

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

Code Evolves

photo by: kevin dooley

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

Languages evolve.photo by: raneko

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

You evolve.

photo by: Kaptain Kobold

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

Complexity kills Comprehension

photo: osbornb

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

Bad Design

photo: miskan

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

Bad specs

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

NIH

source: Urban dictionary

photo by: John 'K'

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

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 26: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

The Solution™photo: Ian Sane

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

Improve code

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

Improve codemake it easier to comprehend

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

Improve codemake it easier to comprehend

make it flexible

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

Improve codemake it easier to comprehend

make it flexible

make it tested

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

Improve codemake it easier to comprehend

make it flexible

make it tested

make it easier to replace, refactor

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

Improve codemake it easier to comprehend

make it flexible

make it tested

make it easier to replace, refactor

make it not exist

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

Testing

photo by: jeffkrause

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

Good Design Concepts

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

SOLID

Single ResponsibilityOpen and Close

Liskov substitutionInterface SegregationDependency Inversion

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

STUPIDSingleton

Tight CouplingUntestability

Premature OptimizationIndescriptive Naming

Duplication

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

Design Patterns

photo by halloweenstock

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

Package Managers

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

Composer

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

PIEProudly

InventedElsewhere

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

guzzlehttp/guzzleHTTP client for RESTful API clients

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

league/tacticianA flexible CommandBus implementation

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

Readability

photo: Morgenland

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

paragraphs

photo: Trossachs Photography

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

whitespace

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

R&D Team for MIH/Naspers

São PauloCape Town

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

Object Calisthenics

photo by: boston_public_library

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

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 50: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

Its about perception, not rules or standards

photo by: david_a_l

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

#1 Only one indentation level per method.

photo by: justinliew

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

#2 Do not use else

photo by: justinliew

Page 53: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16
Page 54: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16
Page 55: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16
Page 56: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

#3 Wrap primitive types, if they contain behavior

photo by: justinliew

Page 57: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

$component->repaint(false);

Page 58: “Writing code that lasts” … or writing code you won’t hate tomorrow. - #PHPSRB16

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

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

#4 Only one -> per line

photo by: justinliew

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

#5 Do not abbreviate.

photo by: justinliew

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

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

}

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

#6 Keep your classes small.

photo by: justinliew

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

#7 Limit your instance variables to less then 5

photo by: justinliew

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

#8 Use first class collections

photo by: justinliew

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

#9 Don’t use getters and setters.

photo by: justinliew

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

#10 Document your code.

photo by: justinliew

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

Your turn.

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

Improve yourself!

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

Read lots of code!

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

Write simple code.

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

Try Object Calisthenics for a month.

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

Use someone else’s code.

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

Share bite size libraries.

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

Thank you.

http://slides.doh.ms

http://doh.ms

@rdohms

https://joind.in/talk/6236e