Code Deodorant 2014

Eba4cc68bfbc2b59c3c3a3cf789075f0?s=47 Tom J Nowell
September 28, 2014

Code Deodorant 2014

Given at WordCamp Europe Sophia 2014

Eba4cc68bfbc2b59c3c3a3cf789075f0?s=128

Tom J Nowell

September 28, 2014
Tweet

Transcript

  1. Code Deodorant Prevention > Cure

  2. I’m Tom J Nowell Code For The People

  3. Code Smells

  4. Home page templates The Template Hierarchy provides: • home.php •

    front-page.php
  5. Indenting <?php if (is_lt_IE9()) : ?> <?php if (function_exists('dynamic_sidebar') &&

    dynamic_sidebar ('countdown-ie')) : else : ?> <div class="pre-widget"> <p><strong>Widgetized area 1</strong></p> <p>This panel is active and ready for you to add some widgets via the WP Admin</p> </div> <?php endif; ?> <?php else : ?> <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar ('countdown')) : else : ? > <div class="pre-widget"> <p><strong>Widgetized area 2</strong></p> <p>This panel is active and ready for you to add some widgets via the WP Admin</p> </div> <?php endif; ?> <?php endif; ?>
  6. Throwing away the main query query_posts(‘...’);

  7. Warnings and @ PHP Warning: strip_tags() expects parameter 1 to

    be string, array given in … $str = @strip_tags(‘<b>example</b>’);
  8. eval & create_function add_action( ‘the_content’, create_function(‘return “hello”; ’) );

  9. Anonymous Objects class Davina_Plugin { function __construct() { add_filter( 'the_content',

    array( $this, 'swear' ), -1 ); } function swear( $content ) { return "Hello big brother house, you are live on channel 4, do not say sh*t f*ck or b*gger"; } } new Davina_Plugin();
  10. Prevention by Design

  11. pre_get_posts function exclude_cat( $query ) { if ( $query->is_home() &&

    $query->is_main_query() ) { $query->set( 'cat', '-1,-1347' ); } } add_action( 'pre_get_posts', 'exclude_cat' );
  12. Dependency Injection Constructor Injection $obj = new test( $dependencies );

    Method injection $obj->setThing( $thing );
  13. Demeters Law $object->getChild()->doThing();

  14. No global variables

  15. Prevention by Tools and Numbers

  16. PHP Syntax checking If your editor doesn’t do it for

    you, something is very wrong...
  17. PHP Code Sniffer

  18. PHP Metrics

  19. PHPLOC

  20. PHP Mess Detector

  21. NPath & Cyclomatic Complexity

  22. So How Does WordPress Fair?

  23. PHPMD phpmd wordpress text codesize, controversial, design, unusedcode > core.txt

  24. wp_insert_post 661,197,634,560,000,000,000 6.6x10^19

  25. redirect_canonical 1,620,651,275,242,427,205,550,080 1.62x10^24

  26. WP_Query::get_posts 1,435,733,941,397,422,709,124,940,625,188, 500,371,668,992,000,000 1.43x10^48

  27. Calculations 1 unit test = 1.5kb 1 Samsung 1TB drive

    = 0.4kg 80 Undecillion kg or 8x1037kg or~4 solar masses
  28. Beta Sagittae

  29. WP_Query::get_posts WP 4.0 58,430,498,159,114,868,447,575,084,102,205, 776,212,823,109,373,305,815,040,000 5.8x10^58

  30. None
  31. The Sombrero Galaxy

  32. Large Megallenic Cloud

  33. 162,789,208,987 Solar Masses 1x10^9 - Sombrero Galaxy 1x10^10 - Large

    Megallenic Cloud dwarf galaxy 1.6x10^11 - WP_Query::get_posts unit test ( 2TB Samsung HD's ) 1.5×10^12 - Milky Way
  34. Questions? @tarendai tomjn.com