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

Code Deodorant 2014

Tom J Nowell
September 28, 2014

Code Deodorant 2014

Given at WordCamp Europe Sophia 2014

Tom J Nowell

September 28, 2014
Tweet

More Decks by Tom J Nowell

Other Decks in Technology

Transcript

  1. Code Deodorant
    Prevention > Cure

    View Slide

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

    View Slide

  3. Code Smells

    View Slide

  4. Home page templates
    The Template Hierarchy provides:
    ● home.php
    ● front-page.php

    View Slide

  5. Indenting


    Widgetized area 1
    This panel is active and ready for you to add some widgets via the WP Admin



    >
    Widgetized area 2
    This panel is active and ready for you to add some widgets via the WP Admin



    View Slide

  6. Throwing away the main query
    query_posts(‘...’);

    View Slide

  7. Warnings and @
    PHP Warning: strip_tags() expects
    parameter 1 to be string, array given
    in …
    $str = @strip_tags(‘example’);

    View Slide

  8. eval & create_function
    add_action( ‘the_content’,
    create_function(‘return “hello”; ’) );

    View Slide

  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();

    View Slide

  10. Prevention by Design

    View Slide

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

    View Slide

  12. Dependency Injection
    Constructor Injection
    $obj = new test( $dependencies );
    Method injection
    $obj->setThing( $thing );

    View Slide

  13. Demeters Law
    $object->getChild()->doThing();

    View Slide

  14. No global variables

    View Slide

  15. Prevention by Tools
    and Numbers

    View Slide

  16. PHP Syntax checking
    If your editor doesn’t do it for you, something is
    very wrong...

    View Slide

  17. PHP Code Sniffer

    View Slide

  18. PHP Metrics

    View Slide

  19. PHPLOC

    View Slide

  20. PHP Mess Detector

    View Slide

  21. NPath & Cyclomatic Complexity

    View Slide

  22. So How Does WordPress Fair?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  27. Calculations
    1 unit test = 1.5kb
    1 Samsung 1TB drive = 0.4kg
    80 Undecillion kg or 8x1037kg
    or~4 solar masses

    View Slide

  28. Beta Sagittae

    View Slide

  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

    View Slide

  30. View Slide

  31. The Sombrero Galaxy

    View Slide

  32. Large Megallenic Cloud

    View Slide

  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

    View Slide

  34. Questions?
    @tarendai
    tomjn.com

    View Slide