Writing Good Code

Writing Good Code

This is a very early version of a talk I gave at MNPHP a couple of weeks ago.

7fca546408cc6d46ab158f06baed2535?s=128

Nate Abele

April 30, 2012
Tweet

Transcript

  1. The 18th _ of April, 2012 Writing Good Code v0.1a

  2. Biography ! Started programming at 12 ! 4-year core contributor

    to CakePHP, lead developer for ~1.5 years ! Co-founded Lithium ! Just scratching the surface
  3. What...?

  4. Compared to what?

  5. “ ” Ward Cunningham Clean code is when you look

    at a routine and it's pretty much what you expected.
  6. Bad == Stream-of-consciousness

  7. public function edit() { $post = Posts::first($this->request->id); if ($post) {

    if (!$this->request->is('get')) { $post->set($this->request->data); if ($post->validates()) { if ($post->save()) { Messages::add('Post was saved successfully.', 'ok'); } else { Messages::add('Saved failed due to DB error', 'error'); } } else { Messages::add('Saved failed due to validation error', 'error'); } } else { $this->set(array('post' => $post)); } } else { throw new NotFoundException(); } }
  8. Post exists? 404 GET? Show edit Valid? Show errors Dead

    end Branch DB error Save? Success Yes Yes Yes Yes N o N o N o N o
  9. Return Branch Post exists? 404 G ET? Show edit Valid?

    Show errors D B error Save? Success Yes Yes Yes Yes No No No No
  10. public function edit() { $post = Posts::first($this->request->id); if (!$post) {

    throw new NotFoundException(); } if (!$this->request->is('get')) { return compact('post'); } $post->set($this->request->data); if (!$post->validates()) { Messages::add('Saved failed due to validation error', 'error'); return compact('post'); } if ($post->save()) { Messages::add('Post was saved successfully.', 'ok'); return compact('post'); } Messages::add('Saved failed due to DB error', 'error'); return compact('post'); }
  11. “ ” Felix Geisendörfer Return home early. http://bit.ly/returnhomeearly #ProTip

  12. Refactoring

  13. Refactoring ! A process of understanding ! A process of

    finding balance
  14. Refactoring Perfectionism Sloppiness The Spectrum

  15. Refactoring ! All code is duplicate code ! Find the

    breaking point ! Focus on flexibility, not capability
  16. Separation of Concerns

  17. Separation of Concerns ! Modularity ! Stratification ! Probably the

    most important concept
  18. Modularity ! Encapsulation ! Extensibility ! Replaceability

  19. Modularity ! Assumptions ! … == trade-offs

  20. Techniques

  21. Techniques ! Recap: Avoid large if blocks ! Recap: Else

    blocks generally also bad ! Switch blocks rarely needed ! Data vs. logic: more defaults, fewer decisions
  22. switch ($request->get('http:host')) { case 'localhost': case '127.0.0.1': case 'myapp.local': case

    'myapp.local.dev': case 'myapp.dev.local': case 'local.myapp.com': return 'development'; case '10.183.1.63': case '10.183.1.57': case '50.56.80.111': case '50.56.80.107': case 'dev.myapp.com': return 'staging'; case 'myapp.com': case 'www.myapp.com': return 'production'; case 'test.myapp.com': case 'myapp.test.local': return 'test'; }
  23. $hosts = array( 'local' => array( 'localhost', '127.0.0.1', 'myapp.local', 'myapp.local.dev',

    'myapp.dev.local', 'local.myapp.com' ), 'production' => array('myapp.com', 'www.myapp.com'), 'test' => array('test.myapp.com', 'myapp.test.local'), 'development' => array( '10.183.1.63', '10.183.1.57', '50.56.80.111', '50.56.80.107', 'dev.myapp.com' ) ); $current = $request->get('http:host'); foreach ($hosts as $env => $list) { if (in_array($current, $list)) { return $env; } } return 'production';
  24. class Session { switch ($storage) { case 'php': // ...

    case 'file': // ... case 'database': // ... } }
  25. switch (true) { case $a && $b: // ... case

    $a [&& !$b]: // ... case $a && $c: // ... }
  26. public function index($class, array $keys, array $options = array()) {

    if (isset($options['background'])) { if ($options['background'] == true) { // ... } } // ... }
  27. public function index($class, array $keys, array $options = array()) {

    $defaults = array('include' => array(), 'background' => false); $options += $defaults; if ($options['background']) { // ... } // ... }
  28. Data > Logic #ProTip

  29. Kaizen

  30. Code you wrote two years ago should disgust you today.

  31. Revolution

  32. Revolution ! Software is ethereal, no limitations ! Boundless potential

    for innovation ! Always remember: everything is amazing
  33. Why...?

  34. Why...? ! ...code for a living? ! ...work at your

    company? ! ...use <insert language/tool/framework>?
  35. Action <= Value <= Belief

  36. “ ” Christopher Alexander In my life as an architect,

    I find that the single thing which inhibits young professionals, new students most severely, is their acceptance of standards that are too low. If I ask a student whether her design is as good as Chartres, she often smiles tolerantly at me as if to say, "Of course not, that isn't what I am trying to do.... I could never do that."
  37. “ ” Steve Jobs Your work is going to fill

    a large part of your life, and the only way to be truly satisfied is to do what you believe is great work; and the only way to do great work is to love what you do.
  38. Thanks. Also, Feedback: ! @nateabele ! nate.abele@gmail.com ! http://nateabele.com/ Check

    out http://phpwizards.com/