Click here to Skip to main content
16,016,760 members
Articles / Programming Languages / C#
Tip/Trick

Preventing stubbed methods from being released

Rate me:
Please Sign up or sign in to vote.
4.96/5 (24 votes)
1 Sep 2011CPOL1 min read 42.6K   15   15
A handy way to stub methods in development, but preventing them from being shipped in release mode.

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.


C#
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.


C#
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.


C#
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:


C#
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)


Written By
Program Manager
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 nice tip, thank you Pin
johannesnestler1-Mar-12 23:17
johannesnestler1-Mar-12 23:17 
GeneralReason for my vote of 5 Nice Pin
zenwalker198526-Oct-11 0:45
zenwalker198526-Oct-11 0:45 
GeneralGood if visual studio does this by default atleast!! Instead... Pin
zenwalker198526-Oct-11 0:45
zenwalker198526-Oct-11 0:45 
GeneralReason for my vote of 5 Nice! Didn't know about that... Pin
Mel Padden25-Oct-11 4:26
Mel Padden25-Oct-11 4:26 
GeneralReason for my vote of 5 nice tip Pin
Roman_wolf15-Oct-11 6:08
Roman_wolf15-Oct-11 6:08 
GeneralReason for my vote of 5 Thanks for sharing Pin
linuxjr14-Oct-11 6:06
professionallinuxjr14-Oct-11 6:06 
GeneralReason for my vote of 5 very useful. Pin
Jani Giannoudis14-Sep-11 10:34
mvaJani Giannoudis14-Sep-11 10:34 
GeneralReason for my vote of 5 Really practical!!! Pin
Hector Garcia Noguera5-Sep-11 22:48
Hector Garcia Noguera5-Sep-11 22:48 
GeneralReason for my vote of 5 A great, simple solution to a common... Pin
mikemkii5-Sep-11 22:28
mikemkii5-Sep-11 22:28 
GeneralReason for my vote of 5 nice Pin
lizichao19835-Sep-11 15:09
lizichao19835-Sep-11 15:09 
GeneralThis would be easier if combined with the VC# "code snippets... Pin
Qwertie5-Sep-11 6:35
Qwertie5-Sep-11 6:35 
GeneralReason for my vote of 5 i didn't even know #error existed. I... Pin
GParkings2-Sep-11 12:25
GParkings2-Sep-11 12:25 
GeneralReason for my vote of 5 Very practical! Pin
AspDotNetDev1-Sep-11 7:43
protectorAspDotNetDev1-Sep-11 7:43 
GeneralAh, but that assumes you're shipping release code... :-D Pin
Andrew Rissing1-Sep-11 4:23
Andrew Rissing1-Sep-11 4:23 
GeneralRe: Very true, but if you are shipping non-release code then you... Pin
Reiss1-Sep-11 21:13
professionalReiss1-Sep-11 21:13 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.