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

    2016– Alain Schlesser The Problem With Reusable Code…
  5. 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.
  6. The Secret Sauce For Writing Reusable Code – 4th September

    2016– Alain Schlesser So We Need To Rearrange This…
  7. 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.
  8. The Secret Sauce For Writing Reusable Code – 4th September

    2016– Alain Schlesser This Easily Allows Us To Go From This…
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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 ); } }
  14. 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
  15. The Secret Sauce For Writing Reusable Code – 4th September

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

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

    2016– Alain Schlesser <?php // 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
  18. The Secret Sauce For Writing Reusable Code – 4th September

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

    2016– Alain Schlesser Secondary Benefits • Forced modularisation • Tested code • Collaboration
  20. 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…
  21. The Secret Sauce For Writing Reusable Code – 4th September

    2016– Alain Schlesser <?php // 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
  22. 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' ); } }
  23. 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
  24. The Secret Sauce For Writing Reusable Code – 4th September

    2016– Alain Schlesser <?php // 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
  25. The Secret Sauce For Writing Reusable Code – 4th September

    2016– Alain Schlesser <?php namespace Example\Greeter; $config = ConfigFactory::create( __DIR__ . 'config/defaults.php' ); $plugin = new Plugin( $config->getSubConfig( __NAMESPACE__ ) ); $plugin->run(); Prefix For Multiple Plugins
  26. 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; } }
  27. 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
  28. The Secret Sauce For Writing Reusable Code – 4th September

    2016– Alain Schlesser PHP Config ... Closures! ( = ~DSL )
  29. 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) • …
  30. The Secret Sauce For Writing Reusable Code – 4th September

    2016– Alain Schlesser Site/App-specific Configurations config/defaults.php config/dt.php
  31. 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
  32. 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
  33. 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