Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Writing code you won’t hate tomorrow - PHPCE18

122 views

Published on

As developers we write code everyday, only to frown at it a week after that. Why do we have such a hard time with code written by others and ourselves, this raging desire to rewrite everything we see? Writing code that survives the test of time and self judgment is a matter of clarity and simplicity. Let's talk about growing, learning and improving our code with calisthenics, readability and good design.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Writing code you won’t hate tomorrow - PHPCE18

  1. 1. Writing code 
 you won’t hate tomorrow. Rafael Dohms
 @rdohms photo: djandyw.com
  2. 2. Staff Engineer Rafael Dohms jobs.usabilla.com @rdohms on twitter
  3. 3. Staff Engineer Rafael Dohms jobs.usabilla.com FeedbackFeedback @rdohms on twitter
  4. 4. photo: Rob Allen (@akrabat)
  5. 5. REWRITE ALL THE CODE! image: hyperboleandahalf
  6. 6. Real Developers, SHIP STUFF. photo: Glyn Lowe Photoworks
  7. 7. How do we find balance? photo: Kalexanderson
  8. 8. Code has an expiration date photo: CarbonNYC
  9. 9. Code is perishable, it rots. photo by: massdistraction
  10. 10. Code Evolves photo by: kevin dooley
  11. 11. Languages evolve. photo by: raneko
  12. 12. You evolve. photo by: Kaptain Kobold
  13. 13. Complexity 
 kills Comprehension photo: osbornb
  14. 14. Bad Design photo: miskan
  15. 15. Bad specs
  16. 16. NIH source: Urban dictionary photo by: John 'K'
  17. 17. 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'
  18. 18. The Solution™ photo: Ian Sane
  19. 19. Improve code
  20. 20. Improve code make it easier to comprehend
  21. 21. Improve code make it easier to comprehend make it flexible
  22. 22. Improve code make it easier to comprehend make it flexible make it tested
  23. 23. Improve code make it easier to comprehend make it flexible make it tested make it easier to replace, refactor
  24. 24. Improve code make it easier to comprehend make it flexible make it tested make it easier to replace, refactor make it not exist
  25. 25. Testing photo by: jeffkrause
  26. 26. "Refactoring without tests is just changing stuff."
  27. 27. Good Design Concepts
  28. 28. Single Responsibility
 Open and Close
 Liskov substitution
 Interface Segregation
 Dependency Inversion Singleton
 Tight Coupling
 Untestability
 Premature Optimization
 Indescriptive Naming
 Duplication Solid Stupid ○"
  29. 29. Design Patterns photo by halloweenstock
  30. 30. Domain Driven Design with Ubiquitous Language photo by lwr
  31. 31. Modular Architecture CQRS, Event Sourcing, Micro Services photo by medialoog
  32. 32. Strict Programming
  33. 33. Package Managers
  34. 34. PIE Proudly
 Invented
 Elsewhere
  35. 35. Readability photo: Morgenland
  36. 36. paragraphs photo: Trossachs Photography whitespace
  37. 37. photo by: boston_public_library Object Calisthenics Jeff Bay
  38. 38. 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
  39. 39. These are exercises, not rules. photo by: david_a_l
  40. 40. #1 Only one indentation level per method. photo by: justinliew
  41. 41. #2 Do not 
 use else photo by: justinliew
  42. 42. 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); } }
  43. 43. 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"
  44. 44. 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
  45. 45. 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); } }
  46. 46. 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'); }
  47. 47. 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
  48. 48. 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
  49. 49. 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
  50. 50. public function createPost($request) { $repository = $this->getRepository('MyBundle:Post'); $entity = new Post(); $repository->save($entity); return $this->redirect('create_ok'); }
  51. 51. public function createPost($request) { $repository = $this->getRepository('MyBundle:Post'); $entity = new Post(); $repository->save($entity); return $this->redirect('create_ok'); } FormValidationMiddleware UniqueEntityMiddleware
  52. 52. 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
  53. 53. #3 Wrap primitive types, if they contain behavior photo by: justinliew
  54. 54. $component->repaint(false);
  55. 55. $component->repaint( new Animate(false) );
  56. 56. use MoneyMoney; $fiveEur = Money::EUR(500); $tenEur = $fiveEur->add($fiveEur);
  57. 57. #4 Only one 
 -> per line photo by: justinliew
  58. 58. $user = $this->get('security.token_storage')->getToken()->getUser();
  59. 59. $user = $this->get('security.token_storage')->getToken()->getUser(); null
  60. 60. #5 Do not abbreviate. photo by: justinliew
  61. 61. if($sx >= $sy) { if ($sx > $strSysMatImgW) { $ny = $strSysMatImgW * $sy / $sx; $nx = $strSysMatImgW; } if ($ny > $strSysMatImgH) { $nx = $strSysMatImgH * $sx / $sy; $ny = $strSysMatImgH; } }
  62. 62. #6 Keep your classes small. photo by: justinliew
  63. 63. #7 Limit your instance variables to 2 photo by: justinliew
  64. 64. class MyRegistrationService { protected $userService; protected $passwordService; protected $logger; protected $translator; protected $entityManager; protected $imageCropper; // ... }
  65. 65. #8 Use first class collections photo by: justinliew
  66. 66. #9 Don’t use getters and setters. photo by: justinliew
  67. 67. public function getScore($request) { return $this->score; } public function setScore($score) { $this->score = $score; } $game->setScore($game->getScore() += 1);
  68. 68. public function collectedCoin() { $this->score += 1; } $game->collectedCoin();
  69. 69. #10 Document your code. photo by: justinliew
  70. 70. Your turn.
  71. 71. Improve yourself!
  72. 72. Read lots of code!
  73. 73. Write simpler code.
  74. 74. Try Object Calisthenics for a month.
  75. 75. Be part of this community!
 
 Embrace open source, and add to it!
  76. 76. DDD, Auto-wire, contribute to PHP, write secure code, use flex, use docker, do static analysis, phpStan CQRS, use fixtures, be strict, communicate, document, collect elephpants… and all the other amazing topics you saw this weekend. #phpce18
  77. 77. DDD, Auto-wire, contribute to PHP, write secure code, use flex, use docker, do static analysis, phpStan CQRS, use fixtures, be strict, communicate, document, collect elephpants… and all the other amazing topics you saw this weekend. #phpce18
  78. 78. Thank you. http://slides.doh.ms http://doh.ms @rdohms jobs.usabilla.com looking for a job in Amsterdam? https://joind.in/talk/18880
  79. 79. http://l.doh.ms/object-calisthenics-links Books

×