Coming up an epigram
2020-07-06 21:50
Been thinking more about epigrams, and came up with one today:
If you want to code fast, be general; if you want your code fast, be specific.
What I have in mind is the observation that, code that handles generic cases tend to be slower than code that handles specific cases.
For example, many JavaScript engines track the runtime type of a value, and specialize on the type to be able to perform operations faster. They also use techniques like tagged pointers, NaN boxing in to track the type of dynamic objects, and if the types can be optimized (like small integers), they can take the fast path.
In this epigram, I am trying to be clever with the juxtaposition of “code” and “fast”. I was thinking of the second half first, then came up with the first half as a contrast. The choice of “general” and “specific” is also for contrast - the order of “code” and “fast” can lead to (what I thought was) opposite results. In some ways, the second half is easier to agree with, and the first half is slightly forced.
When writing code, it is usually faster to write for a specific use case in mind. This is also true when coming up with an algorithm (e.g. solving interview-style questions) - coming up with a case to handle a single example of a graph is easier than solving the problem handling all possible graphs. (A friend brought this up after I shared this epigram.)
Another point of view is that, when you are writing “general” code, you are thinking at a higher level. The code is not fixed to a structure, and hence is free to make whatever assumptions it wants. It is free to define the input structure, even if it might not exist in the system yet. It doesn’t care about edge cases, everything is fine, there is no error handling. This meaning is closer to I am driving at. The definition I’m referring to is:
1.3 Normal or usual.
2 Considering or including the main features or elements of something, and disregarding exceptions; overall.
Considering that, a word that means “handling all cases” (in the sense of a solution to an algorithm question) is generic:
1 Characteristic of or relating to a class or group of things; not specific.
The funny thing here is, I meant for “general” and “specific” to be contrasting, but picked the wrong word due to my misunderstanding of the meanings of those word. I should have used “generic”. But because of my incorrect use of “general”, the epigram ended up being more correct, or at least closer to my intended meaning.