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

Post on 13-Apr-2017

40 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

Transcript

Writing code that lasts.

Rafael Dohms@rdohms

photo: djandyw.com

Writing code you won’t hate tomorrow.

Rafael Dohms@rdohms

photo: djandyw.com

Lead Backend EngineerRafael Dohms

jobs.usabilla.com

@rdohms on twitter

Lead Backend EngineerRafael Dohms

jobs.usabilla.com

Feedback

Feedback

@rdohms on twitter

photo: Rob Allen (@akrabat)

REWRITEALL THE CODE!

image: hyperboleandahalf

Real Developers, SHIP STUFF.

photo: Glyn Lowe Photoworks

How do we find balance?

photo: Kalexanderson

Code has an expiration date

photo: CarbonNYC

Code is perishable, it rots.

photo by: massdistraction

Code Evolves

photo by: kevin dooley

Languages evolve.photo by: raneko

You evolve.

photo by: Kaptain Kobold

Complexity kills Comprehension

photo: osbornb

Bad Design

photo: miskan

Bad specs

NIH

source: Urban dictionary

photo by: John 'K'

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'

The Solution™photo: Ian Sane

Improve code

Improve codemake it easier to comprehend

Improve codemake it easier to comprehend

make it flexible

Improve codemake it easier to comprehend

make it flexible

make it tested

Improve codemake it easier to comprehend

make it flexible

make it tested

make it easier to replace, refactor

Improve codemake it easier to comprehend

make it flexible

make it tested

make it easier to replace, refactor

make it not exist

Testing

photo by: jeffkrause

"Refactoring without tests is just changing stuff."

Good Design Concepts

Single Responsibility Open and Close

Liskov substitution Interface Segregation Dependency Inversion

SingletonTight CouplingUntestability

Premature Optimization Indescriptive Naming

Duplication

Solid Stupid

○"

Design Patterns

photo by halloweenstock

Domain Driven Designwith Ubiquitous Language

photo by lwr

Modular ArchitectureCQRS, Event Sourcing, Micro Services

photo by medialoog

Package Managers

PIEProudly

InventedElsewhere

Readability

photo: Morgenland

photo by: boston_public_library

Object Calisthenics

Jeff Bay

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

These are exercises, not rules.

photo by: david_a_l

#1 Only one indentation level per method.

photo by: justinliew

#2 Do not use else

photo by: justinliew

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); } }

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"

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

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); } }

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'); }

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

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

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

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

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

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

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

FormValidationMiddleware

UniqueEntityMiddleware

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

#3 Wrap primitive types, if they contain behavior

photo by: justinliew

$component->repaint(false);

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

#4 Only one -> per line

photo by: justinliew

#5 Do not abbreviate.

photo by: justinliew

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

}

#6 Keep your classes small.

photo by: justinliew

#7 Limit your instance variables to 2

photo by: justinliew

#8 Use first class collections

photo by: justinliew

#9 Don’t use getters and setters.

photo by: justinliew

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

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

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

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

$game->collectedCoin();

#10 Document your code.

photo by: justinliew

Your turn.

Improve yourself!

Read lots of code!

Write simpler code.

Try Object Calisthenics for a month.

Use someone else’s code,

and share yours!

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

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

Thank you.

http://slides.doh.ms

http://doh.ms

@rdohms

https://joind.in/talk/89356

jobs.usabilla.comlooking for a job in Amsterdam?

top related