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

Writing code that speaks

Writing code that speaks

Code is read many more times than it is written. In this talk I'm going to walk through simple, practical examples of how you can give your code a voice. We're going to explore the power of word choice, and how you can develop naming habits that lead to beneficial refactorings.

Caleb Porzio

June 16, 2017
Tweet

More Decks by Caleb Porzio

Other Decks in Programming

Transcript

  1. "Any fool can write code that a computer can understand.

    Good programmers write code that humans can understand." Martin Fowler
  2. "Indeed, the ratio of time spent reading versus writing is

    well over 10 to 1. We are constantly reading old code as part of the effort to write new code. ... [Therefore,] making it easy to read makes it easier to write." Uncle Bob (Robert C Martin)
  3. 1. Avoid meaningless words 2. Strive for consistency 3. Don't

    make my brain hurt 4. Name according to scope 5. Don't make me look things up 6. Leverage metaphors
  4. ! The more abstracted your code is, the more abstract

    your words will be (1. Avoid meaningless words)
  5. Hungarian notation ! $aNames = ['bob', 'carol', 'jim']; $sName =

    'jack'; $iAge = 23; (1. Avoid meaningless words)
  6. Hungarian notation ! $names = ['bob', 'carol', 'jim']; $name =

    'jack'; $age = 23; (1. Avoid meaningless words)
  7. Pick a word and stick with it echo $blog->body; echo

    $comment->text; echo $testimonial->content; (2. Strive for consistency)
  8. Pick a word and stick with it echo $blog->body; echo

    $comment->body; echo $testimonial->body; (2. Strive for consistency)
  9. Pick a word and stick with it service.api_key service.key service.public_key

    service.secret_key service.api_secret service.api_public service.secret ... (2. Strive for consistency)
  10. Abbreviations I'm ok with ! auth authentication // TODO admin

    administrator id identifier db database i iteration int, bool integer / boolean e, z exception / zonda (3. Don't make my brain hurt)
  11. Abbreviations I'm NOT ok with ! addr address attr attribute

    err error arr array opt option msg message img image (3. Don't make my brain hurt)
  12. 1. Avoid meaningless words 2. Strive for consistency 3. Don't

    make my brain hurt 4. Name according to scope
  13. Compound method names class Cart { public function addProduct() {};

    public function removeProduct() {}; public function changeProduct() {}; } $cart->addProduct(); (4. Name according to scope)
  14. Compound method names class Cart { public products; } class

    Products { public function add() {}; public function remove() {}; public function change() {}; } $cart->products->add($product); (4. Name according to scope)
  15. 1. Avoid meaningless words 2. Strive for consistency 3. Don't

    make my brain hurt 4. Name according to scope 5. Don't make me look things up
  16. Bare integer class HttpStatus { const CREATED = 201; }

    $response = $request->post('/users', $payload); $this->assertEquals(HttpStatus::CREATED, $response->statusCode()); (5. Don't make me look things up)
  17. Implicit method name Post::getFirst(); // creates a new post if

    can't find one (5. Don't make me look things up)
  18. 1. Avoid meaningless words 2. Strive for consistency 3. Don't

    make my brain hurt 4. Name according to scope 5. Don't make me look things up 6. Leverage metaphors
  19. "This transfer of some quality of the known to the

    unknown is like a beam of light; we "see" (that is, we understand) something about the unknown in the light of the known." Mary Oliver (A Poetry Handbook)
  20. 1. Avoid meaningless words 2. Strive for consistency 3. Don't

    make my brain hurt 4. Name according to scope 5. Don't make me look things up 6. Leverage metaphors