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

Tagged as

Preventing stubbed methods from being released

, 1 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


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)

Share

About the Author

Reiss
Program Manager
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 very useful. PinmvpJani Giannoudis14-Sep-11 11:34 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 1 Sep 2011
Article Copyright 2011 by Reiss
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid