Code just in time
Time to read
, 385 words, 4th grade
As discussed in less is more, to avoid multiplying entities needlessly is not enough.
It is important to delay writing code until you are certain that you will need it. Even then, write it only at the last moment.
Why? For at least two good reasons:
- You might never need it. Why, then, waste all that time and effort? Thatʼs effort, time, and money that you could put toward something useful.
- Even if you do need it, writing the code too soon may result in the code becoming obsolete before it becomes useful. Now you have to rewrite it.
How many times have you written a piece of code only to toss it out, unused? How many times have you written code that you didnʼt immediately need? And then when the need arose, you found that code was obsolete and had to be rewritten?
If you are like most coders, then the answer is too many times.
But it is more than writing code that you may never use or writing it too soon. There is also premature optimization.
Suppose you know two or more ways to code something. If one of them has proven more optimal, then by all means write the code that way. See do it right the first time for more detail.
But, often, we have no idea where our bottlenecks will be before we write our code and test it. Optimizing code when you have no idea what the impact of that optimization will be is an exercise in futility. You may never need to optimize that code.
And if thatʼs the case, then optimizing it now will be a waste of time. Your time is better spent doing the right optimizations later: the ones that give you the most bang for the buck.
You will save time by avoiding useless coding and premature optimization. That is time that you can then use to polish up your code and make it sparkle. Now weʼre talking Craft Code.
As Guille recommends: never write a line of code until you have to.
Code just in time.