Click here to Skip to main content
Click here to Skip to main content

Preventing stubbed methods from being released

By , 1 Sep 2011
 

When writing large systems, it is very common to stub out methods that haven't been fully implemented and return to them later to complete the internal logic.

Often we need to use the method prior to it being implemented, so the default logic is placed in the method body and adding a TODO tag to remind us that we need to complete the method at a future date.


private bool ValidateUserDetails(string userName, string password)
{
  //TODO: Default value used for stub - implement this later
  return true;
}

This allows us to use the method in our development process, but if the TODO isn't picked up, this can lead to the method not being implemented correctly, often causing our applications to fail in real-lfe scenarios when shipped.

A common approach to ensure that we get round this is to throw a System.NotImplementedException() exception.

private bool ValidateUserDetails(string userName, string password)
{
  //TODO: Implement this later
  throw new System.NotImplementedException();
}

However, this doesn't allow us to use the method during development, so the two approaches are combined by using the #if[^] preprocessor.

private bool ValidateUserDetails(string userName, string password)
{
  //TODO: Default value used for stub - implement this later
  #if DEBUG
  return true;
  #else
  throw new System.NotImplementedException();
  #endif
}

This allows us to use the method in our development process, but this can be dangerous, as it is possible that if not all code paths are tested fully in release mode, then unexpected code can be shipped.

However, by combining this approach with the #error[^] preprocessor, we get the following:

private bool ValidateUserDetails(string userName, string password)
{
  //TODO: Default value used for stub - implement this later
  #if DEBUG
  return true;
  #else
  #error Not Implented
  #endif
}

This allows us to use the method in our development process, but when the project is switched into release mode, it won't compile, so incomplete code can't be shipped.

References:

MSDN has the full list of C# preprocessor directives[^] available.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Reiss
Architect
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 nice tip, thank youmemberjohannesnestler1 Mar '12 - 23:17 
GeneralReason for my vote of 5 Nicememberzenwalker198526 Oct '11 - 0:45 
GeneralGood if visual studio does this by default atleast!! Instead...memberzenwalker198526 Oct '11 - 0:45 
GeneralReason for my vote of 5 Nice! Didn't know about that...memberMel Padden25 Oct '11 - 4:26 
Reason for my vote of 5
Nice! Didn't know about that...
GeneralReason for my vote of 5 nice tipmemberRoman_wolf15 Oct '11 - 6:08 
GeneralReason for my vote of 5 Thanks for sharingmemberlinuxjr14 Oct '11 - 6:06 
GeneralReason for my vote of 5 very useful.mvpJani Giannoudis14 Sep '11 - 10:34 
GeneralReason for my vote of 5 Really practical!!!memberHector Garcia Noguera5 Sep '11 - 22:48 
GeneralReason for my vote of 5 A great, simple solution to a common...membermikemkii5 Sep '11 - 22:28 
GeneralReason for my vote of 5 nicememberlizichao19835 Sep '11 - 15:09 
GeneralThis would be easier if combined with the VC# "code snippets...memberQwertie5 Sep '11 - 6:35 
GeneralReason for my vote of 5 i didn't even know #error existed. I...memberGParkings2 Sep '11 - 12:25 
GeneralReason for my vote of 5 Very practical!protectorAspDotNetDev1 Sep '11 - 7:43 
GeneralAh, but that assumes you're shipping release code... :-DmemberAndrew Rissing1 Sep '11 - 4:23 
GeneralRe: Very true, but if you are shipping non-release code then you...memberReiss1 Sep '11 - 21:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 1 Sep 2011
Article Copyright 2011 by Reiss
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid