Conditional Build

This has been used and misused in all programming languages that has this feature. This makes the code more unpredictable and thus more prone to errors due to testing and overview difficulty especially the #if/#endif.

If you need to have conditional builds to configure for different releases you are normally better of using Inversion of Control pattern (See Martin Fowler’s article here), which in basic is a way to move away from conditional builds by moving the specific objects into the configuration and then only distribute the dll’s that you want the customers to use.

In .Net you can do it in two ways

  • #if/#endif blocks.
  • using contitional attribute

I have until now only found one place where the #if/#endif block makes sence and that is to make a Windows Service easily testable (read about that here).

In a normal project and everywhere else where you need different variants use Conditional Attributes such as

[ Conditional( "DEBUG" ) ]
private void logDebugInformation()
{
// Autolog something here
}

thus executing this in “DEBUG” compilation mode

public void execute()
{
logDebugInformation();
executeSomeFunction();
logDebugInformation();
}

and this in every other mode

public void execute()
{
executeSomeFunction();
}

Leave a Reply

Your email address will not be published. Required fields are marked *