Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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/

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser 'Hello', ]; Basic Config File

Slide 20

Slide 20 text

The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser 'Hello', ]; = reusable code = project-specific code => Basic Config File

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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…

Slide 24

Slide 24 text

The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser [ 'greeting' => 'Hello', ], ]; Prefix For The Reusable Class

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser 'Hello', ]; return [ 'Example' => [ 'Greeter' => [ 'ReusableGreeter' => $reusable_greeter; ], ], ]; Prefix For Multiple Plugins

Slide 28

Slide 28 text

The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser getSubConfig( __NAMESPACE__ ) ); $plugin->run(); Prefix For Multiple Plugins

Slide 29

Slide 29 text

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; } }

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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) • …

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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