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

My Appication Layer Keeps Breaking!

My Appication Layer Keeps Breaking!

Code and style patterns and anti-patterns that keep code clean and easier to maintain.

Avatar for Stephen Frank

Stephen Frank

October 03, 2014

Other Decks in Programming

Transcript

  1. aka Advice to myself What advice would I give myself

    two years ago that would help me to write code that doesn’t break and is easier to maintain.
  2. Use auto-loaders require ("vendor/autoload.php"); // None of this malarky $this->load->library('article');

    $this->article->foo(); // Clean and simple loading $article = new Article; // ...or a Service Locator $article = App::make('article');
  3. Pseudo-singleton patterns and “god” objects class Reports { function report_generator()

    { $this->load->library('table'); $this->table->set_heading('Name', 'Color', 'Size'); $this->table->add_row('Fred', 'Blue', 'Small'); echo $this->table->generate(); $this->table->clear(); // LUL WAT? $this->table->set_heading('Name', 'Color', 'Size'); $this->table->add_row('Fred', 'Blue', 'Small'); echo $this->table->generate(); } }
  4. class Reports { function report_generator() { $table = new Table;

    $table->setHeading('Name', 'Color', 'Size'); $table->addRow('Fred', 'Blue', 'Small'); echo $table->generate(); $table = new Table; $table->setHeading('Name', 'Color', 'Size'); $table->addRow('Fred', 'Blue', 'Small'); echo $table->generate(); } }
  5. use App\HTMLGenerator\TableFactory; use App\HTMLGenerator\TableEvents; use App\HTMLGenerator\Table\Elements\Table; use App\HTMLGenerator\Table\Elements\Row; use App\HTMLGenerator\Table\Elements\Cell;

    use App\HTMLGenerator\Presenters\HTML5Presenter; use App\Events\EventEmitter; class Reports { function reportGenerator() { $tableFactory = new TableFactory; $table = $tableFactory->newTable(); EventEmitter::emit(TableEvents::TABLE_CREATED, [$table]); $row = new Row; $row->addCell(new Cell('Name')); $table->addRow($row); $htmlPresenter = new HTML5Presenter(); echo $htmlPresenter->render($table); } }
  6. Rather have an application that doesn’t work at all than

    failing silently Rather have no users able to register than register without the password field Rather have a contact form burn up in flames than have it send without the message body Rather have the reports email die than have it mail the client 300 notifications in an hour
  7. Turn all errors into exceptions set_error_handler( function ($level, $message, $file,

    $line) { throw new ErrorException($message, 0, $level, $file, $line); } );
  8. Throw exceptions instead of returning $this->email->to('[email protected]'); $this->email->from('[email protected]'); $this->email->message('Hi.'); if (!

    $this->email->send()) { foreach ($this->email->_debug_msg as $error) { // Do something... } } $this->email->clear();
  9. function a() { if (! b()) { return false; }

    do_some_work(); return true; } function b() { if (some_error_condition()) { return false; } } if (! a()) { // error }
  10. function a() { b(); do_some_work(); return true; } function b()

    { if (some_error_condition()) { throw new Exception(); } } try { a(); } catch (Exception $e) { // error }