Time to read
, 414 words, 5th grade
Power, in programming, is a product of abstraction. We abstract to make our code more powerful.
It is surprising the number of developers that fail to understand this concept.
The problem is this: with abstraction comes greater complexity, thus greater cognitive load. We measure the cost of complexity, ergo cognitive load, in time … and money.
Thus it is important to abstract only when necessary. Begin with a concrete implementation. If you need a similar object elsewhere, then copy, paste, and tweak. Do this until the need for abstraction becomes obvious. Then, and only then, abstract out the pattern.
And be sure always to do it right the first time.
We have seen codebases with absurd levels of abstraction. Approaching obscene almost. Layer upon layer upon layer of wrappers and other code. All intended to make a component do a hundred different things.
And often when it was only ever going to have to do one or two.
We find both premature optimization and gross over-engineering present in these instances, too.
The Principle of Least Power derives from the Rule of Least Power. That rule directs us to use a programming language just powerful enough to do the job. No more.
The Principle of Least Power says: Abstract only when necessary. Abstract only when duplication has become the barrier to progress. And then abstract only to the least degree required. Write code just powerful enough to do what you need it to do; no more.
In short, stay as low as practicable on the tree of abstraction.
We would say, prefer duplication until duplication is the problem. It might surprise you how often duplication is enough.
Another approach is to create several specialized items rather than one generic item. Donʼt create a single function that tries to do many things. Instead, prefer several small, composable functions that each do one thing well.
When you find that you are producing complex branching code, thatʼs a hint that youʼve taken a wrong turn. Your function, component, or process is trying to do too much.
We could say much more about this, but we wonʼt go into depth here. Weʼll save it for an essay or three.
Of course, the easiest way to ensure that you use the least power is to plan properly and keep it simple.