Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing Good Code

Writing Good Code

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

Nate Abele

April 30, 2012
Tweet

More Decks by Nate Abele

Other Decks in Programming

Transcript

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

    to CakePHP, lead developer for ~1.5 years ! Co-founded Lithium ! Just scratching the surface
  2. “ ” Ward Cunningham Clean code is when you look

    at a routine and it's pretty much what you expected.
  3. 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(); } }
  4. 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
  5. 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
  6. 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'); }
  7. Refactoring ! All code is duplicate code ! Find the

    breaking point ! Focus on flexibility, not capability
  8. Techniques ! Recap: Avoid large if blocks ! Recap: Else

    blocks generally also bad ! Switch blocks rarely needed ! Data vs. logic: more defaults, fewer decisions
  9. 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'; }
  10. $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';
  11. class Session { switch ($storage) { case 'php': // ...

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

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

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

    $defaults = array('include' => array(), 'background' => false); $options += $defaults; if ($options['background']) { // ... } // ... }
  15. Revolution ! Software is ethereal, no limitations ! Boundless potential

    for innovation ! Always remember: everything is amazing
  16. Why...? ! ...code for a living? ! ...work at your

    company? ! ...use <insert language/tool/framework>?
  17. “ ” 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."
  18. “ ” 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.