A rule that I like that is 100% enforceable: commented code is allowed, but there needs to be a comment above the code explaining why the code was commented. The effect in practice of this rule is that
- People delete code more and comment it less
- Readers know whether to need to pay attention to the commented code or not.
Point 1 happens the most. I begin to write down something like "keeping this code around because I'm not sure yet whether the new code is better" and then I backspace and delete the whole thing because I realise it clearly is.I rationally know that I can remove and re-add it later and that commit/revert is better, but the pain associated with solving conflicts is strong enough that I subconsciously want to avoid it.
It would be nice to have an emacs or vim plugin in which you select a block of code, and it slowly walks back the graph of commits, showing each commit for 5 seconds. That way you could nicely see how your code-block evolved over time... (of course in many cases the code-block itself is useless without context)
:Gblame to show a git blame sidebar, and P to open the file at that commit.
E.g. I might start a rewrite with:
#if BLEEDING_EDGE ...new code... #else ...old code... #endif
Eventually moving the old code into a further removed #if 0 as the remaining bugs taper off (i.e. I'll just fix them instead of reverting back to the old code for a milestone) before deleting the old code outright.Much like evicting data from L1, L2, L3, and finally main memory.
Sometimes I'll even write explicit comments to delete code by a certain date.
-S<string>
Look for differences that change the number of occurrences of the specified string (i.e. addition/deletion) in a file. Intended for the scripter’s use.
It is useful when you’re looking for an exact block of code (like a struct), and want to know the history of that block since it first came into being: use the feature iteratively to feed the interesting block in the preimage back into -S, and keep going until you get the very first version of the block.