$30 off During Our Annual Pro Sale. View Details »

The Secret Sauce For Writing Reusable Code

schlessera
October 15, 2016

The Secret Sauce For Writing Reusable Code

Everyone knows that you need to write reusable code to be able to grow as a developer, right?

However, most developers struggle to understand how to split up their code to make it truly reusable, so they end up copy-pasting parts of code and modifying as needed, instead of effectively reusing the code that was already written, without a single change.

This session explains the concept of Config files and how they allow you to cleanly separate reusable code from project-specific code.

( This talk was held at WordCamp Netherlands in Utrecht on Oct. 15, 2016: https://2016.netherlands.wordcamp.org/session/the-secret-sauce-for-writing-reusable-code/ )

( Complimentary blog posts: https://www.alainschlesser.com/config-files-for-reusable-code/ )

schlessera

October 15, 2016
Tweet

More Decks by schlessera

Other Decks in Programming

Transcript

  1. The Secret Sauce
    Writing Reusable Code
    Alain Schlesser www.alainschlesser.com
    Software Engineer & WordPress Consultant @schlessera

    View Slide

  2. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    NOTE:
    This talk was held at WordCamp Nederlands in Utrecht on Oct. 15, 2016.
    There’s an (on-going) series of complimentary blog posts:
    https:/
    /www.alainschlesser.com/config-files-for-reusable-code/

    View Slide

  3. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    About The Person In Front Of You
    Born in Luxembourg
    Living in Germany
    Working in the Cloud
    Passionate about:
    Code quality, software architecture, best
    practices, principles, patterns, and
    everything related.

    View Slide

  4. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    What To Expect
    1. General principle that makes
    code reusable
    2. Common way of
    implementing this principle

    View Slide

  5. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    The Problem With Reusable Code…

    View Slide

  6. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Object-oriented syntax in and
    of itself does not make your
    code reusable.

    View Slide

  7. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    So We Need To Rearrange This…

    View Slide

  8. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    …Into This

    View Slide

  9. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Design your code so that the
    reusable parts and the project-
    specific parts never intermingle.

    View Slide

  10. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    This Easily Allows Us To Go From This…

    View Slide

  11. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    …To This

    View Slide

  12. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Example Code
    Problem:
    We want to have a reusable
    Greeter class that can show
    different greetings in different
    projects.*
    * Silly example that can still fit on slides

    View Slide

  13. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    class Greeter {
    /**
    * @param string $name The name of the person to greet.
    */
    public function greet( $name ) {
    printf( 'Hello %2$s!', $name );
    }
    }
    Mixed Code Types

    View Slide

  14. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    class Greeter {
    /**
    * @param string $name The name of the person to greet.
    */
    public function greet( $name ) {
    printf( 'Hello %2$s!', $name );
    }
    }
    = reusable code = project-specific code
    Mixed Code Types

    View Slide

  15. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    The reusable class should completely
    ignore where it gets its business
    logic from. It should act on
    whatever gets passed to it.
    à Injection

    View Slide

  16. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    class ReusableGreeter {
    /** @var ConfigInterface */
    protected $config;
    /**
    * @param ConfigInterface $config The Config to use.
    */
    public function __construct( ConfigInterface $config ) {
    $this->config = $config;
    }
    /**
    * @param string $name The name of the person to greet.
    */
    public function greet( $name ) {
    $greeting = $this->config->get( 'greeting' );
    printf( '%1$s %2$s!', $greeting, $name );
    }
    }

    View Slide

  17. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    class ReusableGreeter {
    /** @var ConfigInterface */
    protected $config;
    /**
    * @param ConfigInterface $config The Config to use.
    */
    public function __construct( ConfigInterface $config ) {
    $this->config = $config;
    }
    /**
    * @param string $name The name of the person to greet.
    */
    public function greet( $name ) {
    $greeting = $this->config->get( 'greeting' );
    printf( '%1$s %2$s!', $greeting, $name );
    }
    }
    = reusable code = project-specific code

    View Slide

  18. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Business-specific Code à Config File
    Config File
    Reusable Class
    Business Logic

    View Slide

  19. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    // We return a standard PHP array as a result of including
    // this Config file.
    return [
    'greeting' => 'Hello',
    ];
    Basic Config File

    View Slide

  20. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    // We return a standard PHP array as a result of including
    // this Config file.
    return [
    'greeting' => 'Hello',
    ];
    = reusable code = project-specific code
    =>
    Basic Config File

    View Slide

  21. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Immediate Benefits
    • Separate files
    • Injection
    • Type-hinting
    • Validation

    View Slide

  22. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Secondary Benefits
    • Forced modularisation
    • Tested code
    • Collaboration

    View Slide

  23. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Source Is Flexible
    As the source should be irrelevant for the
    classes that use the Config data, you can
    combine several files into one, read them
    from a database or network, build them at
    run-time for unit tests, etc…

    View Slide

  24. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    // We can prefix a section of a Config, so that one file
    // can be used for multiple classes.
    return [
    'ReusableGreeter' => [
    'greeting' => 'Hello',
    ],
    ];
    Prefix For The Reusable Class

    View Slide

  25. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    class Plugin {
    /** @var ConfigInterface */
    protected $config;
    /**
    * @param ConfigInterface $config The Config to use.
    */
    public function __construct( ConfigInterface $config ) {
    $this->config = $config;
    }
    public function run() {
    $greeter = new ReusableGreeter(
    $this->config->getSubConfig( 'ReusableGreeter' )
    );
    $greeter->greet( 'World' );
    }
    }

    View Slide

  26. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Config File
    Getting The Config File Into The Reusable Class
    Reusable Class
    Plugin Class
    Business Logic
    Business Logic
    Business Logic

    View Slide

  27. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    // We can prefix the entire Config, so that a single file
    // can be shared across several plugins.
    $reusable_greeter = [
    'greeting' => 'Hello',
    ];
    return [
    'Example' => [
    'Greeter' => [
    'ReusableGreeter' => $reusable_greeter;
    ],
    ],
    ];
    Prefix For Multiple Plugins

    View Slide

  28. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    namespace Example\Greeter;
    $config = ConfigFactory::create( __DIR__ . 'config/defaults.php' );
    $plugin = new Plugin( $config->getSubConfig( __NAMESPACE__ ) );
    $plugin->run();
    Prefix For Multiple Plugins

    View Slide

  29. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Configs & Auto-wiring Injection
    interface DatabaseConfig extends ConfigInterface { }
    class Database {
    /** @var DatabaseConfig */
    protected $config;
    /**
    * @param DatabaseConfig $config The Config to use.
    */
    public function __construct( DatabaseConfig $config ) {
    $this->config = $config;
    }
    }

    View Slide

  30. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Domain-Specific Language
    Domain-specific language (noun):
    a computer programming language of limited
    expressiveness focused on a particular domain.
    - Martin Fowler, ThoughtWorks

    View Slide

  31. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    PHP Config ... Closures! ( = ~DSL )

    View Slide

  32. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Adapted Configs
    Provide defaults and then override with different Configs …
    • for different sites/apps (site_a.php)
    • for different environments (site_a-development.php)
    • for different contexts (unit-tests.php)
    • for specific situations (run-backups.php)
    • …

    View Slide

  33. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Site/App-specific Configurations
    config/defaults.php
    config/dt.php

    View Slide

  34. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser

    View Slide

  35. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    Config Library
    https://github.com/brightnucleus/config
    My own library:
    Alternatives:
    https://github.com/symfony/config
    symfony/config
    https://github.com/zendframework/zend-config
    zendframework/zend-config
    brightnucleus/config

    View Slide

  36. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    “Can You Summarize, Please?”
    • Design for reusability from the get-go
    • Have a clean separation between
    different types of code
    • Config files provide a structured way of
    injecting project-specific logic into
    reusable classes

    View Slide

  37. The Secret Sauce For Writing Reusable Code – 15th Octobre 2016– Alain Schlesser
    The End
    I’m Alain Schlesser.
    Follow me on twitter:
    @schlessera
    Or visit my site:
    www.alainschlesser.com

    View Slide