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

Unleash your inner author

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.


February 23, 2013

More Decks by juneih

Other Decks in Programming



    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. Four facts about the brain Which number sequence will be

    printed here? <?php echo "2" . print "3"; ?>
  4. Shorter is better class DrugStrengthFormCountryManufacturerPharmacyCopayProductPriceFactory { public function getSalesOrderProductSupplierCopayDrugStrengthFormCountryManufacturerPharmacyCop ayProductPrice(

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

    SecurityContext $security, ProductSupplier $productSupplier, Organization $copay) { /* some code here */ } }
  6. 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'; ?>
  7. Let structure control your line of thought public function execute(

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

    is better • Writing is rewriting • Let the structure control your line of thought
  9. 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