Slide 1

Slide 1 text

PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING Unleash your inner author How to Avoid the “Trigger Trap” @juneih #phpuk2013

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

The paradox of writing and reading code

Slide 4

Slide 4 text

The paradox of writing and reading code Me on the subway

Slide 5

Slide 5 text

The paradox of writing and reading code Everyone else on the subway

Slide 6

Slide 6 text

The paradox of writing and reading code

Slide 7

Slide 7 text

The paradox of writing and reading code

Slide 8

Slide 8 text

Four facts about the brain

Slide 9

Slide 9 text

Four facts about the brain

Slide 10

Slide 10 text

Four facts about the brain Which number sequence will be printed here?

Slide 11

Slide 11 text

Four facts about the brain

Slide 12

Slide 12 text

Four facts about the brain

Slide 13

Slide 13 text

Four facts about the brain

Slide 14

Slide 14 text

Four facts about the brain

Slide 15

Slide 15 text

Four facts about the brain

Slide 16

Slide 16 text

The Trigger Trap

Slide 17

Slide 17 text

The Trigger Trap

Slide 18

Slide 18 text

The Trigger Trap

Slide 19

Slide 19 text

Writing tips for the programmer

Slide 20

Slide 20 text

Shorter is better class DrugStrengthFormCountryManufacturerPharmacyCopayProductPriceFactory { public function getSalesOrderProductSupplierCopayDrugStrengthFormCountryManufacturerPharmacyCop ayProductPrice( SecurityContext $securityContext, SalesOrderProductSupplier $salesOrderProductSupplier, Organization $copay) { /* some code here */ } }

Slide 21

Slide 21 text

Shorter is better class ProductPriceFactory { public function getProductPrice ( SecurityContext $security, ProductSupplier $productSupplier, Organization $copay) { /* some code here */ } }

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Writing is rewriting ● DrugStrengthFormCountryManufacturerPharmacyCopayProductPriceFactory ● DrugStrengthFormCountryManufacturerPharmacyProductPriceFactory ● CountryManufacturerPharmacyProductPriceFactory ● ProductPriceFactory

Slide 24

Slide 24 text

Let structure control your line of thought public function execute( ) { switch ($this->getPrefix()) { case "STATUS": return $this->adminStatus(); default: return $this->subscribe(); } }

Slide 25

Slide 25 text

Writing tips for programmers ● Shorter is better ● Simpler is better ● Writing is rewriting ● Let the structure control your line of thought

Slide 26

Slide 26 text

Look up

Slide 27

Slide 27 text

Unleash your inner Author

Slide 28

Slide 28 text

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