To Optimize, or Not?
A few weeks ago at the BIL conference in Los Angeles, I gave a quick presentation about my belief that we should use “crappy computers” for training and education, because they tend to force students to learn to code efficiently and make use of whatever the hardware can do, rather than allowing them to be lazy and presuming that the hardware will be fast enough.
Sorry for the truly crappy presentation, it was written about
20 minutes prior, and given unrehearsed. Slides at:
https://gitlab.com/michaelgat/Presentations/blob/master/CrappyComputers.pdf
Today, my friend Chris Fox has a slightly different take, which is that for most circumstances, maximal performance optimization is a bad thing, as it often causes you to do things that make your code unreadable, unmaintainable and generally buggy. I’ve often had that argument in the Python world, where my point has been that in most cases it’s far better to use a clear and generally understood structure like a “for” loop than to use Python’s slightly more efficient list comprehension. Why? Because most of the time it doesn’t matter, if it really does matter you probably shouldn’t be using an interpreted language like Python in the first place, and in the real world the next person to look at that code might not be as experienced as you. In fact, the next person might have just transitioned from Java or some other language that doesn’t have list comprehensions! But she will surely be able to recognize a for loop and understand what it does which will save her the time and potential errors that go along with figuring out what you’re really trying to do.
I feel the same way about a lot of structures I see in a lot of code. Lambda functions, for example, are wildly overused in places where they provide no benefit but sure make it hard to figure out what you were trying to do.
The two viewpoints are not contradictory and I hold them both. You should be forced to write efficient code and know how to do it in situations where it matters. You should also be able to recognize when it’s more important to optimize for readability, maintainability and clarity. Working on “crappy computers” teaches you when the performance matters and when you don’t need to care, which is another great thing about them.
“Optimization” is a bad word in this case. We should always optimize our code. The question is what are we optimizing for?