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 full-size slide

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

    View full-size slide

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

    View full-size slide

  4. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  8. 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 full-size slide

  9. Prevention by Design

    View full-size slide

  10. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  13. No global variables

    View full-size slide

  14. Prevention by Tools
    and Numbers

    View full-size slide

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

    View full-size slide

  16. PHP Code Sniffer

    View full-size slide

  17. PHP Mess Detector

    View full-size slide

  18. NPath & Cyclomatic Complexity

    View full-size slide

  19. So How Does WordPress Fair?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. 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 full-size slide

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

    View full-size slide

  25. Beta Sagittae

    View full-size slide

  26. 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 full-size slide

  27. The Sombrero Galaxy

    View full-size slide

  28. Large Megallenic Cloud

    View full-size slide

  29. 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 full-size slide

  30. Questions?
    @tarendai
    tomjn.com

    View full-size slide