16

As you're developing you often use things like

throw new NotImplementedException("Finish this off later") 

or

// TODO - Finish this off later

as a placeholder to remind you to finish something off - but these can be missed and mistakenly end up in the release.

You could use something like

#if RELEASE
   Finish this off later
#endif

so it won't compile in Release build - but is there a more elegant way?

Ryan
  • 23,871
  • 24
  • 86
  • 132
  • Create a technical debt task (JIRA, TFS, CQ, whatever)? – sll Feb 06 '12 at 15:08
  • BTW, I would argue that such check should be a part of build process, maybe check in instead? Most of version control systems have hooks which can handle such cases. – Snowbear Feb 06 '12 at 15:09
  • 1
    @Snowbear So you don't let them check in until everything is finished - or you just don't build it with a TODO? – weston Feb 06 '12 at 15:13
  • @weston, yep, what's a point in checking in if you will not build it? – Snowbear Feb 06 '12 at 16:11
  • 2
    @Snowbear backup, history, and all the other benefits of source control. I might be working on something for weeks or longer before building it as a release. – weston Feb 06 '12 at 16:14

4 Answers4

12

I saw an elegant implementation here

#if DEBUG

namespace FakeItEasy
{
    using System;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// An exception that can be thrown before a member has been
    /// implemented, will cause the build to fail when not built in 
    /// debug mode.
    /// </summary>
    [Serializable]
    [SuppressMessage("Microsoft.Design",   
        "CA1032:ImplementStandardExceptionConstructors", 
        Justification = "Never used in production.")]
    public class MustBeImplementedException
        : Exception
    {
    }
}

#endif
PHeiberg
  • 29,411
  • 6
  • 59
  • 81
  • 2
    @crazy2be - you make your unfinished methods throw the `MustBeImplementedException`. The compiler directive `#if DEBUG` makes sure that the `MustBeImplementedException` class is only compiling in debug, so for the release build any forgotten throw will result in the build breaking because the class is not available. – PHeiberg Feb 06 '12 at 21:03
10

I would recommend to use #warning:

#warning Finish this off later

And in the Release configuration set Treat Warnings as Errors to True.

In this case in Debug you will see it only as warnings but in release it will throw exceptions.

Samich
  • 29,157
  • 6
  • 68
  • 77
  • Problem is there are some warnings that we want to ignore (e.g. calling API methods now marked [obsolete] in new version but where we want single source for old/new version. – Ryan Feb 06 '12 at 15:20
  • 1
    You can configure what types of warnings should be ignored. Here is related thread: http://stackoverflow.com/questions/267168/treat-all-warnings-as-errors-except-in-visual-studio – Samich Feb 06 '12 at 15:32
10

You can use the #error and #warning directives to throw your own build errors and warnings:

#if RELEASE
#error Not finished!
#endif

http://msdn.microsoft.com/en-us/library/c8tk0xsk(v=vs.80).aspx

weston
  • 54,145
  • 21
  • 145
  • 203
  • I like it so + but PHeibergs answer has the benefit of being a one liner in use so I gave him the answer. – Ryan Feb 06 '12 at 17:54
1

You could encapsulate this in it's own method, that way it only has to be changed in one place for a release build.

void NotImplemented()
{
    #if RELEASE
        Finish this off later
    #endif
}
N_A
  • 19,799
  • 4
  • 52
  • 98