Unleash your inner author

C7be8063276a44ee68f83088d3e756e2?s=47 juneih
February 23, 2013

Unleash your inner author

Syntax is for machines, words are for humans, and code needs to satisfy both. It is easy to forget that once your carefully crafted code leaves your hands it isn't the problem you've been trying to solve any more, it has become a solution. The reason why you chose your solution over another is important and you have to make it clear to the next programmer who reads your code. This is when you have to unleash your inner author, and start communicating with your reader. That reader is the next person who needs to add to or fix your code, and more times that you'd like to admit - that reader is yourself. Think about that person when you code, and future you will thank you. This talk will cover why reading code is a dreaded task, we'll have a look at what "the trigger-trap" is and how you can avoid it, and we'll finish off with some writing tips to the programmer.

C7be8063276a44ee68f83088d3e756e2?s=128

juneih

February 23, 2013
Tweet

Transcript

  1. PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS •

    SUPPORT • TRAINING Unleash your inner author How to Avoid the “Trigger Trap” @juneih #phpuk2013
  2. Unleash your Inner Author Chapter 1 The paradox of writing

    and reading code Chapter 2 Four facts about the brain Chapter 3 The trigger trap Chapter 4 Writing tips for the programmer
  3. The paradox of writing and reading code

  4. The paradox of writing and reading code Me on the

    subway
  5. The paradox of writing and reading code Everyone else on

    the subway
  6. The paradox of writing and reading code

  7. The paradox of writing and reading code

  8. Four facts about the brain

  9. Four facts about the brain

  10. Four facts about the brain Which number sequence will be

    printed here? <?php echo "2" . print "3"; ?>
  11. Four facts about the brain

  12. Four facts about the brain

  13. Four facts about the brain

  14. Four facts about the brain

  15. Four facts about the brain

  16. The Trigger Trap

  17. The Trigger Trap

  18. The Trigger Trap

  19. Writing tips for the programmer

  20. Shorter is better class DrugStrengthFormCountryManufacturerPharmacyCopayProductPriceFactory { public function getSalesOrderProductSupplierCopayDrugStrengthFormCountryManufacturerPharmacyCop ayProductPrice(

    SecurityContext $securityContext, SalesOrderProductSupplier $salesOrderProductSupplier, Organization $copay) { /* some code here */ } }
  21. Shorter is better class ProductPriceFactory { public function getProductPrice (

    SecurityContext $security, ProductSupplier $productSupplier, Organization $copay) { /* some code here */ } }
  22. Simpler is better Nested ternaries – hard to read: $speed

    = isset($js_options['speed'])? is_numeric($js_options['speed'] )? $js_options['speed'] : "'". $js_options['speed'] ."'": "'normal'"; Better use of a ternary: $temperature = strpos($month, 'r') ? 'cold' : 'warm'; ?>
  23. Writing is rewriting • DrugStrengthFormCountryManufacturerPharmacyCopayProductPriceFactory • DrugStrengthFormCountryManufacturerPharmacyProductPriceFactory • CountryManufacturerPharmacyProductPriceFactory •

    ProductPriceFactory
  24. Let structure control your line of thought public function execute(

    ) { switch ($this->getPrefix()) { case "STATUS": return $this->adminStatus(); default: return $this->subscribe(); } }
  25. Writing tips for programmers • Shorter is better • Simpler

    is better • Writing is rewriting • Let the structure control your line of thought
  26. Look up

  27. Unleash your inner Author

  28. Sources Thinking, fast and slow - Daniel Kahnemann Brain rules

    – John Medina Back of the napkin – Dan Roam Elephant: http://files.phpclasses.org/graphics/phpclasses/elephpant.png Superhero: http://www.eschrade.com/wp-content/uploads/2011/12/superhero-white-bkg.png Writing Tips: 12 tips til skribenten, Gudmund Hernes, Morgenbladet The art of readable code: Dustin Boswell, Trevor Foucher http://thedailywtf.com/Comments/Really-Descriptive-Names.aspx I hate reading other people's code: http://abstrusegoose.com/432