Ask HN: If your code no longer makes sense to you, do you leave it as is?
There was probably a good reason why you wrote it that way a long time ago.
So as long as it seems to work, wouldn't leaving it alone reduce the probability of introducing bugs?
21 comments
[ 2.9 ms ] story [ 61.6 ms ] threadOtherwise you have to concede that you're getting dumber. Someone better than you wrote code you don't understand, and it happened to be your younger self.
If the solutions are both correct and use the same approach, but one has managed to make it understandable, then that is better. Understandable beats obfuscated, all else being equal.
The solution that the programmers don't understand reveals a weakness in their analytic skills, though. The author could well be smarter or know more techniques, or both.
There is one way in which reading is absolutely harder that writing. Writing (a given revision of the code, or any other text) is only done once. Once you have written that, nobody else has to write that ever again.
Reading is not like that. Nobody can read it once, so that then nobody else has to read it. Everyone who needs to read it must do it for themselves; explanations and summaries given by others who have read it before only go so far. Even one individual must repeatedly read it in order to get it, after which he or she will forget it anyway with the passage of time. The code doesn't "stay read" the way it stays written; if you come back to it in 25 years, there is nothing to write (other than a new revision, but my point is no need to write the original revision). If you come back to reread something in 25 years, you might as well have never seen it before.
If I need to add features to it I'll consider it though.
If it needs rewriting, I'll rewrite it.
If you don't understand the requirements, you likely don't understand the solution and potentially the problem either. In such circumstances it is best to either not intervene, or take the time to familiarize yourself with both the problem and the solution before you attempt to intervene.
Either way I wouldn’t leave it as is, that’s just a recipe for ensuring it becomes even more opaque over time
BUT this is a sign and a lesson for you to improve your documentation.
You should be able to sit down with any code you have written and understand it by reading the comments.
IMHO: Any code that is NOT documented sufficiently that you can puck it up later is "Throw away code!"
I sometimes code out "in long' if I feel it is necessary. That is, I deliberately add extra lines to 'show my working'. As in, perhaps:
might possibly be laid out as to show how and why a particular result is generated, if I feel the original code is too abstruse to see quickly why it is so.Yes, there is a (usually) negligible time cost in the running of the program that might even be fixed by an optimising compiler. But I will save a lot more time than that for myself and any other programmer in the future when maintenance time comes around.