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

It's my way or the highway - How to make your code usable by others

It's my way or the highway - How to make your code usable by others

With npm, composer, gem, pip and numerous other package managers, we have more libraries at our disposal then ever before.

The problem with a lot of those libraries is that they address just one aspect: functionality, neglecting another important one: the developer who is implementing the library.

Did you ever find yourself at 3AM, scrolling through pages and pages of documentation, wishing you wrote a piece of code on your own instead of waisting your time implementing someone elses? During this talk we will go over a couple of OOP principles and design patterns that will greatly reduce someones time when implementing something you wrote, while increasing the quality of the code in general.

Bogdan Habic

October 04, 2015
Tweet

More Decks by Bogdan Habic

Other Decks in Programming

Transcript

  1. Hi

  2. function doWork2(idLinije) { httpObject = GetXmlHttpObject(); if (httpObject != null)

    { var url = "comboLinije.php" //url = url + "?idLinije=" + idLinije; //url= url + "&smer" + smer; url = url + "?kodLinije=" + idLinije httpObject.open("GET", url ,true); httpObject.send(null); httpObject.onreadystatechange = setOutput2; } }
  3. DRY

  4. • Only One Level Of Indentation Per Method
 • Don't

    Use The ELSE Keyword
 • Wrap All Primitives And Strings
  5. • Keep All Entities Small
 • No Classes With More

    Than Two Instance Variables
 • No Getters/Setters/Properties
  6. interface StringDecorator { /** * Decorates the string. * *

    @param string $string * @return string */ public String decorate($string); }
  7. abstract class RegexDecorator implements StringDecorator { protected $regularExpression; protected $replacement;

    public final function __construct() { $this->regularExpression = $this->getRegularExpression(); $this->replacement = $this->getReplacement(); } public abstract function getRegularExpression(); public abstract function getReplacement(); protected function replace($string) { return preg_replace($this->regularExpression, $this->replacement, $string); } public function decorate($string) { return $this->replace($string); } }
  8. abstract class RegexDecorator implements StringDecorator { protected $regularExpression; protected $replacement;

    public final function __construct() { $this->regularExpression = $this->getRegularExpression(); $this->replacement = $this->getReplacement(); } public abstract function getRegularExpression(); public abstract function getReplacement(); protected function replace($string) { return preg_replace($this->regularExpression, $this->replacement, $string); } public function decorate($string) { return $this->replace($string); } }
  9. • Forced to declare attributes • Known “ consumption “

    • Expected behavior • It’s self explanatory
  10. trait StringDecoratorTrait { public function decorate($string) { foreach($this->getDecorators() as $decorator)

    { $decoratorInstance = new $decorator(); $string = $decoratorInstance->decorate($string); } return $string; } abstract public function getDecorators(); }
  11. class MyDecoratorQueue { use StringDecoratorTrait; public function getDecorators() { return

    [ 'App\Decorators\FirstCapitalLetter', 'App\Decorators\ReplaceSpacesWithUnderscores', 'App\Decorators\LastLetterSmall', ]; } }
  12. public function decorate($string) { foreach($this->getDecorators() as $decorator) { $decoratorInstance =

    new $decorator(); $string = $decoratorInstance->decorate($string); if(!is_string($string)) throw new Exception(“The return value should be a string”); } return $string; }