C# : I hate commented out code

Even though retaining commented lines of code in your source-base is not a good thing, it's a fact that they do exist. Whenever I see a chunk of code commented out I first make a yucky face and then the next thing that comes to my mind is why did someone do this (was he unsure??) and in case it was required, how they hell do I find out all such commented blocks of code and clean up the files.

How you comment out code in a large project heavy impacts whether all such code will be found easily and cleaned (or in some cases un-commented). There are multiple styles people use and some of them are bad practices. The so called *advanced* editor features in the IDE aggravates this to a great deal. Some of the common styles I have encountered are listed below

Multi-line comment

This was the most common way of commenting out code in the C++ era, however this is less used in C#

 /*foreach (IMotionDetector detector in list){Console.WriteLine("{0} ({1})", detector.Name, detector.Description);}*/

If you have a coding standard in place which does not allow multi-line comments anywhere else in your code then you might live with this. However, if you have a thousand place in the source base that uses this (as in copy-right notice on top of each file and block comments on top of all public methods)  then you'll be in deep trouble to find this piece out. The only advantage of this method is that its really simple to uncomment the code using any editor.

The single line comment

This irritates me the most

 //foreach (IMotionDetector detector in list)//{// Console.WriteLine("{0} ({1})", detector.Name, detector.Description);//}

Any source base will have thousands of // scattered every-where, so how the hell am I supposed to find code commented out in this fashion?

This is worsened by the fact that the VS IDE has the cool (???) Edit->Advanced->Comment Selection feature which actually uses this format to comment out a block of selected code. If you are using the same editor or some other editor which has the corresponding uncomment feature then you are fine. If you are unfortunate enough to like some other editor like notepad (or is being forced to use another editor due to some constrains) then happy hunting. Go to the beginning of each line and hit del twice :(

#if <token> ... #endif

 #if commentforeach (IMotionDetector detector in list){    Console.WriteLine("{0} ({1})", detector.Name, detector.Description);}#endif

I think this is the only style that renders commented code easy to find. You can just do a search for #if comment or some other token and get to each of these. The developers using the VS IDE will be happy because VS is intelligent enough to gray out the code enclosed in #if token when token is not defined. The only issue is that if someone goes and #defines comment. So the really safe thing to do is use #if false

 #if falseforeach (IMotionDetector detector in list){Console.WriteLine("{0} ({1})", detector.Name, detector.Description);}#endif

Do you really need the commented piece of code

Before keeping commented-out code you really need to think twice (maybe thrice). Almost all projects use source-control so you can always get back to the code once it gets deleted. If you decide on leaving the code, add a comment on top indicating why you commented it out and why you think you'll need it back in the future.