Ask HN: Do you keep wrong code in a comment as a reminder of what doesn't work?
Or if an algorithm is wrong, do you keep it in a comment as a reminder and perhaps explain why it doesn't work?
Or if an algorithm is wrong, do you keep it in a comment as a reminder and perhaps explain why it doesn't work?
I tend to like code to be structured in a way that allows maximal contextual readability (as in: a glance at a page of code should allow to see enough context to understand what's going on)… and that requires to keep any of the clutter that tends to stretch the code too much vertically to a minimum.
So: if it's just a short line so that it fits as an inline comment right side of the corrected version AND holds some lesson that may be of use again, I keep it like that. If it's too long, I just put a succint ref to the problem as an inline comment to the corrected version and move the old code to be replaced into a separate markdown file together with a short explanation of the problem. And if the info of the old version clearly isn't gonna be of any use again, I consider the version control system to be the place for it, not the code either.
And as others have said, you could add some unit tests to prevent others (or your future self) from creating a regression.
Then when working on refactoring you have the tests as a reminder of what cases need to be supported.
Generally the code should be correct and work as current requirements dictate. So probably you don't want to keep an entire section of the old code just commented out and sitting there.
What I've seen done as a practice is to leave a brief comment referencing specific commit hashes in cases where an important change to business logic is involved.
Most of the time this shouldn't be necessary though. In the course of investigating bugs, going through the commit history should provide most of the information needed. Do a "git blame" and read the notes the previous person left in their commit messages.
While I remove the old code, I do sometimes leave a comment explaining why the new code is there, e.g.
# Using a heap sort here as we keep getting data in a order that is pathological for quick sortSomeone opening the file should get as little as they need to be effective at their job/ticket. Sometimes that means they need extensive history of how we got here, but probably not.
Otherwise, just clear off wrong code without much comment. I've seen cases where comments too get bloated up over the years and no body owns to clear it up
Like "SSL is disabled here due to compatibility issues with system XYZ, <link to docs>"