Hacker News new | ask | show | jobs
by user3939382 1197 days ago
I don't know if there's a fancy programming acronym for this, but as much as DRY or SRP my rule is this: if I were to break this chunk of code off into a function, it would:

* Give this chunk of code a name

* Clearly document, in types and names, the inputs and outputs at its boundaries, without having to discover this through a breakpoint

Does the clarity of adding that documentation outweigh the indirection?

A great example is a set of 4-5 if-conditions. Looking at them might be unavoidable arcane-looking complexity with regex's or who knows. Now instead it's called:

  if ($this->orderIsValid($order))
Isn't that nicer in most cases for the person reading it, who's trying to understand what the larger function does? Yes, even if it's only used in that one spot.

A lot of this is subjective so I'm not going to pretend to have written the programmer's stone tablet of rules but that's my strategy.