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

The Secret Sauce For Writing Reusable Code

schlessera
September 04, 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 Frankfurt on Sep. 4, 2016: https://2016.frankfurt.wordcamp.org/session/the-secret-sauce-for-writing-reusable-code/ )

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

schlessera

September 04, 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 full-size slide

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

    View full-size slide

  3. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  12. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  13. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  14. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  15. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  16. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  17. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  20. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  23. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  24. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  25. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

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

    View full-size slide

  27. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  28. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  29. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  30. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

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

    View full-size slide

  32. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

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

    View full-size slide

  34. The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser

    View full-size slide

  35. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

  36. The Secret Sauce For Writing Reusable Code – 4th September 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 full-size slide

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

    View full-size slide