If You’re Using “#if DEBUG”, You’re Doing it Wrong

I was going through some legacy code the other day, refactoring it all over the place and I ran into many blocks of code wrapped in “#if DEBUG”.  Of course, after a bit of refactoring in a RELEASE configuration these blocks of code were quickly out of date (and by out of date, I mean no longer compiling).  A huge PITA. For example, take the following code: public class MyCommand { public DateTime DateAndTimeOfTransaction; } public class Test { //… public void ProcessCommand(MyCommand myCommand) { #if DEBUG if (myCommand.DateAndTimeOfTransaction > DateTime.Now) throw new InvalidOperationException("DateTime expected to be in the past"); #endif // do more stuff with … Continue reading If You’re Using “#if DEBUG”, You’re Doing it Wrong