Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is '#IF DEBUG' deprecated in modern programming?

Tags:

c#

This is my first StackOverflow question so be nice! :-)

I recently came across the following example in some C# source code:

public IFoo GetFooInstance()
{
    #IF DEBUG
        return new TestFoo();
    #ELSE
        return new Foo();
    #ENDIF
}

Which lead me to these questions:

  1. Is the use of "#IF DEBUG" unofficially deprecated? If not what is considered to be a good implementation of its use?
  2. Using a class factory and/or tools like MOQ, RhinoMocks, etc how could the above example be implemented?
like image 325
Michael Avatar asked Apr 27 '09 23:04

Michael


Video Answer


2 Answers

Using an IoC container, the entire function becomes redundant, instead of calling GetFooInstance you'd have code similar to:

ObjectFactory.GetInstance<IFoo>();

The setup of your IoC container could be in code or through a configuration file.

like image 140
Richard Avatar answered Oct 22 '22 05:10

Richard


Nope. We use it all the time to sprinkle diagnostic information in our assemblies. For example I have the following shortcut used when debugging:

#if DEBUG
        if( ??? ) System.Diagnostics.Debugger.Break();
#endif

Where I can change ??? to any relevant expresion like Name == "Popcorn". etc. This ensures that none of the debugging code leaks into the release build.

like image 26
Paul Alexander Avatar answered Oct 22 '22 07:10

Paul Alexander