refactoring

  • 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.

  • Array.from converts anything iterable

    Converting a NodeList or an arguments object into a real array has been Array.prototype.slice.call(x) for a decade — an incantation that works because of how slice was specified…

  • list() inside foreach unpacks as you iterate

    Iterating a list of pairs normally costs two lines at the top of the loop body to name the parts. list() in the foreach itself does the naming…

  • $.each and forEach take their arguments in opposite orders

    $.each hands the callback the index first and the value second. Array.prototype.forEach hands it the value first and the index second. Both signatures are two arguments of convenient…