Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Biography ! Started programming at 12 ! 4-year core contributor to CakePHP, lead developer for ~1.5 years ! Co-founded Lithium ! Just scratching the surface

Slide 3

Slide 3 text

What...?

Slide 4

Slide 4 text

Compared to what?

Slide 5

Slide 5 text

“ ” Ward Cunningham Clean code is when you look at a routine and it's pretty much what you expected.

Slide 6

Slide 6 text

Bad == Stream-of-consciousness

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

“ ” Felix Geisendörfer Return home early. http://bit.ly/returnhomeearly #ProTip

Slide 12

Slide 12 text

Refactoring

Slide 13

Slide 13 text

Refactoring ! A process of understanding ! A process of finding balance

Slide 14

Slide 14 text

Refactoring Perfectionism Sloppiness The Spectrum

Slide 15

Slide 15 text

Refactoring ! All code is duplicate code ! Find the breaking point ! Focus on flexibility, not capability

Slide 16

Slide 16 text

Separation of Concerns

Slide 17

Slide 17 text

Separation of Concerns ! Modularity ! Stratification ! Probably the most important concept

Slide 18

Slide 18 text

Modularity ! Encapsulation ! Extensibility ! Replaceability

Slide 19

Slide 19 text

Modularity ! Assumptions ! … == trade-offs

Slide 20

Slide 20 text

Techniques

Slide 21

Slide 21 text

Techniques ! Recap: Avoid large if blocks ! Recap: Else blocks generally also bad ! Switch blocks rarely needed ! Data vs. logic: more defaults, fewer decisions

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

$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';

Slide 24

Slide 24 text

class Session { switch ($storage) { case 'php': // ... case 'file': // ... case 'database': // ... } }

Slide 25

Slide 25 text

switch (true) { case $a && $b: // ... case $a [&& !$b]: // ... case $a && $c: // ... }

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

public function index($class, array $keys, array $options = array()) { $defaults = array('include' => array(), 'background' => false); $options += $defaults; if ($options['background']) { // ... } // ... }

Slide 28

Slide 28 text

Data > Logic #ProTip

Slide 29

Slide 29 text

Kaizen

Slide 30

Slide 30 text

Code you wrote two years ago should disgust you today.

Slide 31

Slide 31 text

Revolution

Slide 32

Slide 32 text

Revolution ! Software is ethereal, no limitations ! Boundless potential for innovation ! Always remember: everything is amazing

Slide 33

Slide 33 text

Why...?

Slide 34

Slide 34 text

Why...? ! ...code for a living? ! ...work at your company? ! ...use ?

Slide 35

Slide 35 text

Action <= Value <= Belief

Slide 36

Slide 36 text

“ ” 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."

Slide 37

Slide 37 text

“ ” 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.

Slide 38

Slide 38 text

Thanks. Also, Feedback: ! @nateabele ! [email protected] ! http://nateabele.com/ Check out http://phpwizards.com/