Deleting commented out code

This is a rule I al­ways en­cour­age in soft­ware de­vel­op­men­t. More­over, I con­sid­er it to be some­thing that has to be in­clud­ed in ev­ery cod­ing guide­line of a good projec­t.

There are sev­er­al rea­sons for this, but prob­a­bly the best ex­pla­na­tion can be found in the book “Clean Code”1 by un­cle Bob, on which ex­plains that the code, gets out­dat­ed (rot­ten) with the rest of the sur­round­ing code, and hence it makes a place for con­fu­sion, lead­ing to an er­ror-prone spot.

There are, how­ev­er, peo­ple that seem to find some ar­gu­ments for com­ment­ing out code, or leav­ing it. Some com­mon ar­gu­ments/rea­sons usu­al­ly are:

  • I might need this func­­tion­al­i­­ty lat­er..”

We have source con­trol sys­tems (for ex­am­ple git) for this. In git, any­thing can be re­stored from a pre­vi­ous point. If the soft­ware is prop­er­ly un­der ver­sion con­trol, there is no rea­son to fear da­ta loss. Trust git, code fear­less­ly.

  • This is tem­po­rary dis­­abled… It will be re­­s­tored lat­er”.

Again, same prin­ci­ple, re­ly on the ver­sion con­trol sys­tem. Save a patch, and then re­store lat­er, or stash the changes, re­vert the com­mit, etc. As you see, there are plen­ty of bet­ter op­tions for solv­ing this sce­nari­o.

  • Code that was left from the fist ver­­sion

Prob­a­bly de­bug­ging left­over­s. No doubt here: seek, lo­cate, de­stroy.

There is, a clear prob­lem with code that is un­der com­men­t, which is that it is “frozen” in time: it was good at some point, but then it was left there while the rest of the code around it, evolved, so this old code might not cer­tain­ly work (hence it is “rot­ten”), so un-­com­ment­ing it is a bad idea be­cause it will prob­a­bly crash.

An­oth­er prob­lem is that it can be a source of bias for some oth­er de­vel­op­er, who wants to main­tain that code at a fu­ture point in time. The one who left the rot­ten code, might have thought that it was a source of in­spi­ra­tion for when this func­tion­al­i­ty was go­ing to be ap­plied, but in­stead, it is just bi­as­ing the new de­vel­op­er with this skele­ton, pre­vent­ing from a brand new, fresh idea for that code.

There­fore, for these main rea­sons (an prob­a­bly much more), hav­ing code that is com­ment­ed in a code base, is a poor prac­tice, (not to men­tion a code smell). If you are a sea­soned de­vel­op­er, who cares about code qual­i­ty, and best prac­tices, you must not doubt when delet­ing it. Delete com­ment­ed out code mer­ci­less­ly: seek, lo­cate and de­stroy.

1

A book I high­ly rec­om­mend: http­s://www.a­ma­zon.­com/­Clean-­Code-Hand­book-­Soft­ware-Crafts­man­ship/d­p/0132350882