Top Banner
Writing code that lasts. Rafael Dohms @rdohms photo: djandyw.com
81

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

Apr 13, 2017

Download

Technology

Rafael Dohms
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: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

Writing code that lasts.

Rafael Dohms@rdohms

photo: djandyw.com

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

Writing code you won’t hate tomorrow.

Rafael Dohms@rdohms

photo: djandyw.com

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

Lead Backend EngineerRafael Dohms

jobs.usabilla.com

@rdohms on twitter

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

Lead Backend EngineerRafael Dohms

jobs.usabilla.com

Feedback

Feedback

@rdohms on twitter

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

photo: Rob Allen (@akrabat)

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

REWRITEALL THE CODE!

image: hyperboleandahalf

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

Real Developers, SHIP STUFF.

photo: Glyn Lowe Photoworks

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

How do we find balance?

photo: Kalexanderson

Page 14: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

Code has an expiration date

photo: CarbonNYC

Page 15: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

Code is perishable, it rots.

photo by: massdistraction

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

Code Evolves

photo by: kevin dooley

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

Languages evolve.photo by: raneko

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

You evolve.

photo by: Kaptain Kobold

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

Complexity kills Comprehension

photo: osbornb

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

Bad Design

photo: miskan

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

Bad specs

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

NIH

source: Urban dictionary

photo by: John 'K'

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

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

The Solution™photo: Ian Sane

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

Improve code

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

Improve codemake it easier to comprehend

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

Improve codemake it easier to comprehend

make it flexible

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

Improve codemake it easier to comprehend

make it flexible

make it tested

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

Improve codemake it easier to comprehend

make it flexible

make it tested

make it easier to replace, refactor

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

Improve codemake it easier to comprehend

make it flexible

make it tested

make it easier to replace, refactor

make it not exist

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

Testing

photo by: jeffkrause

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

"Refactoring without tests is just changing stuff."

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

Good Design Concepts

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

Single Responsibility Open and Close

Liskov substitution Interface Segregation Dependency Inversion

SingletonTight CouplingUntestability

Premature Optimization Indescriptive Naming

Duplication

Solid Stupid

○"

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

Design Patterns

photo by halloweenstock

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

Domain Driven Designwith Ubiquitous Language

photo by lwr

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

Modular ArchitectureCQRS, Event Sourcing, Micro Services

photo by medialoog

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

Package Managers

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

PIEProudly

InventedElsewhere

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

Readability

photo: Morgenland

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

photo by: boston_public_library

Object Calisthenics

Jeff Bay

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

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

These are exercises, not rules.

photo by: david_a_l

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

#1 Only one indentation level per method.

photo by: justinliew

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

#2 Do not use else

photo by: justinliew

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

public function createPost($request) { $entity = new Post(); $form = new MyForm($entity); $form->bind($request); if ($form->isValid()){ $repository = $this->getRepository('MyBundle:Post'); if (!$repository->exists($entity) ) { $repository->save($entity); return $this->redirect('create_ok'); } else { $error = "Post Title already exists"; return array('form' => $form, 'error' => $error); } } else { $error = "Invalid fields"; return array('form' => $form, 'error' => $error); } }

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

public function createPost($request) { $entity = new Post(); $form = new MyForm($entity); $form->bind($request); if ($form->isValid()){ $repository = $this->getRepository('MyBundle:Post'); if (!$repository->exists($entity) ) { $repository->save($entity); return $this->redirect('create_ok'); } else { $error = "Post Title already exists"; return array('form' => $form, 'error' => $error); } } else { $error = "Invalid fields"; return array('form' => $form, 'error' => $error); } }

actual goal of function “Create Post"

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

public function createPost($request) { $entity = new Post(); $form = new MyForm($entity); $form->bind($request); if ($form->isValid()){ $repository = $this->getRepository('MyBundle:Post'); if (!$repository->exists($entity) ) { $repository->save($entity); return $this->redirect('create_ok'); } else { $error = "Post Title already exists"; return array('form' => $form, 'error' => $error); } } else { $error = "Invalid fields"; return array('form' => $form, 'error' => $error); } }

this is all error

handling

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

public function createPost($request) { $entity = new Post(); $form = new MyForm($entity); $form->bind($request); if ($form->isValid()){ $repository = $this->getRepository('MyBundle:Post'); if (!$repository->exists($entity) ) { $repository->save($entity); return $this->redirect('create_ok'); } else { $error = "Post Title already exists"; return array('form' => $form, 'error' => $error); } } else { $error = "Invalid fields"; return array('form' => $form, 'error' => $error); } }

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

public function createPost($request) { $entity = new Post(); $repository = $this->getRepository('MyBundle:Post'); $form = new MyForm($entity); $form->bind($request); if ($form->isValid() === false){ return array('form' => $form, 'error' => 'Invalid fields'); } if ($repository->exists($entity)){ return array('form' => $form, 'error' => 'Duplicate post title'); } $repository->save($entity); return $this->redirect('create_ok'); }

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

public function createPost($request) { $entity = new Post(); $repository = $this->getRepository('MyBundle:Post'); $form = new MyForm($entity); $form->bind($request); if ($form->isValid() === false){ return array('form' => $form, 'error' => 'Invalid fields'); } if ($repository->exists($entity)){ return array('form' => $form, 'error' => 'Duplicate post title'); } $repository->save($entity); return $this->redirect('create_ok'); }

exit condition 1: invalid form

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

public function createPost($request) { $entity = new Post(); $repository = $this->getRepository('MyBundle:Post'); $form = new MyForm($entity); $form->bind($request); if ($form->isValid() === false){ return array('form' => $form, 'error' => 'Invalid fields'); } if ($repository->exists($entity)){ return array('form' => $form, 'error' => 'Duplicate post title'); } $repository->save($entity); return $this->redirect('create_ok'); }

exit condition 2: duplicated post

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

public function createPost($request) { $entity = new Post(); $repository = $this->getRepository('MyBundle:Post'); $form = new MyForm($entity); $form->bind($request); if ($form->isValid() === false){ return array('form' => $form, 'error' => 'Invalid fields'); } if ($repository->exists($entity)){ return array('form' => $form, 'error' => 'Duplicate post title'); } $repository->save($entity); return $this->redirect('create_ok'); }

this is what the method actually

does

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

public function createPost($request) { $repository = $this->getRepository('MyBundle:Post'); $entity = new Post();

$repository->save($entity); return $this->redirect('create_ok'); }

Page 55: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

public function createPost($request) { $repository = $this->getRepository('MyBundle:Post'); $entity = new Post();

$repository->save($entity); return $this->redirect('create_ok'); }

FormValidationMiddleware

UniqueEntityMiddleware

Page 56: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

public function createPost($request) { $repository = $this->getRepository('MyBundle:Post'); $entity = new Post();

$repository->save($entity); return $this->redirect('create_ok'); }

The power of PSR-7 and middleware

FormValidationMiddleware

UniqueEntityMiddleware

Page 57: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire
Page 58: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire
Page 59: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

#3 Wrap primitive types, if they contain behavior

photo by: justinliew

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

$component->repaint(false);

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

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

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

#4 Only one -> per line

photo by: justinliew

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

#5 Do not abbreviate.

photo by: justinliew

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

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

}

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

#6 Keep your classes small.

photo by: justinliew

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

#7 Limit your instance variables to 2

photo by: justinliew

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

#8 Use first class collections

photo by: justinliew

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

#9 Don’t use getters and setters.

photo by: justinliew

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

public function getScore($request) { return $this->score;}

public function setScore($score) { $this->score = $score;}

$game->setScore($game->getScore() += 1);

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

public function collectedCoin() { $this->score += 1;}

$game->collectedCoin();

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

#10 Document your code.

photo by: justinliew

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

Your turn.

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

Improve yourself!

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

Read lots of code!

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

Write simpler code.

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

Try Object Calisthenics for a month.

Page 77: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

Use someone else’s code,

and share yours!

Page 78: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

Improve your naming, secure your API, grow

awesome teams, handle errors, flip out, check out

MySQL 8, Drupal 8 and HTML 5, go PSR-7, git you guts and finally, survive.

#phpyorkshire

Page 79: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

Improve your naming, secure your API, grow

awesome teams, handle errors, flip out, check out

MySQL 8, Drupal 8 and HTML 5, go PSR-7, git you guts and finally, survive.

#phpyorkshire

Page 80: “Writing code that lasts” … or writing code you won’t hate tomorrow. - PHP Yorkshire

Thank you.

http://slides.doh.ms

http://doh.ms

@rdohms

https://joind.in/talk/89356

jobs.usabilla.comlooking for a job in Amsterdam?