refactoring

  • Utils classes are where responsibilities go to hide

    A class named Utils, Helpers or Common has no definition, so nothing can be excluded from it. It grows monotonically, it is imported everywhere, and it ends up…

  • Migrating PSR-0 to PSR-4 removes a directory level

    PSR-0 mapped the entire namespace onto the path, so AppReportBuilder had to live at src/App/Report/Builder.php — the vendor and package name repeated inside a directory that was already…

  • Restructuring an application for Laravel 5

    4.2 to 5.0 is not an upgrade, it is a relocation. Moving an application through it in an order that keeps it booting at every step.

  • Default parameters replace the argument-check dance

    The traditional way to give a parameter a default is x = x || fallback, which is wrong whenever a legitimate value is falsy. Passing 0 for a…

  • A service layer is where the fat controller was going anyway

    Business logic in a controller is reachable from exactly one place: an HTTP request. The first time the same operation is needed from a console command, a queue…

  • array_walk_recursive for nested structures

    Applying a transformation to every scalar in a nested array — trimming decoded JSON, casting numeric strings — usually produces a small recursive function that gets copied between…

  • array_column flattens a result set in one call

    Pulling one field out of a result set is a foreach that everyone writes and nobody enjoys. array_column() does it in one call, and its second use —…

  • Template literals end string concatenation

    Building a string out of several variables in JavaScript has meant a chain of + and a careful audit of the spaces. Backtick strings interpolate directly and, more…

  • Array.prototype.find returns the element, not the index

    indexOf only works when you already have the value you are looking for. Finding the first element matching a predicate has meant either a filter() that builds a…

  • Moving a legacy PHP application onto Composer and PSR-4

    Two hundred and forty require_once calls where load order was load-bearing, moved onto an autoloader one namespace at a time without a big-bang rewrite.