the secret sauce for writing reusable code

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

Upload: alain-schlesser

Post on 18-Jan-2017

544 views

Category:

Software


4 download

TRANSCRIPT

The Secret Sauce

Writing Reusable CodeAlain Schlesser www.alainschlesser.com

Software Engineer & WordPress Consultant @schlessera

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/

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, bestpractices, principles, patterns, andeverything related.

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

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

The Problem With Reusable Code…

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.

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

So We Need To Rearrange This…

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

…Into This

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.

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

This Easily Allows Us To Go From This…

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

…To This

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

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

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

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

The reusable class should completelyignore where it gets its business

logic from. It should act onwhatever gets passed to it.

à Injection

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

}}

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

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

Business-specific Code à Config File

Con

figF

ile

Reusable Class

Business Logic

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

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

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

Immediate Benefits

• Separate files• Injection• Type-hinting• Validation

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

Secondary Benefits

• Forced modularisation• Tested code• Collaboration

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…

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

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

}}

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

Con

figF

ile

Getting The Config File Into The Reusable Class

Reusable Class

Plugin ClassBusiness Logic

Business Logic

Business Logic

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

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

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;

}}

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

Domain-Specific Language

Domain-specific language (noun):

a computer programming language of limitedexpressiveness focused on a particular domain.

- Martin Fowler, ThoughtWorks

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

PHP Config ... Closures! ( = ~DSL )

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

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

Site/App-specific Configurationsconfig/defaults.php

config/dt.php

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

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/configsymfony/config

https://github.com/zendframework/zend-configzendframework/zend-config

brightnucleus/config

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

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