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

Having documentation when using a piece of code you didn’t write is nice, but usually it’s not that hard to mess something up/missuse it. Using some basic OOP principles we can enforce proper use of our code onto developers who are using it, and greatly reduce someones time when implementing something for the first time, while increasing the quality of the code.

Bogdan Habic

March 14, 2015
Tweet

More Decks by Bogdan Habic

Other Decks in Programming

Transcript

  1. 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; } }
  2. interface StringDecorator { /** * Decorates the string. * *

    @param string $string * @return string */ public function decorate($string); }
  3. 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); } }
  4. 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); } }
  5. 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); } }
  6. 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); } }
  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 parameter types •

    Known return types • 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; }