C# Code tips #2, Preprocessor directives
How often have you had an "oops" after releasing some code for a web site to a production environment. We all know that developers are most often inhuman in their ability to code for looooong hours and sometimes...yes, sometimes, we forget this connectionstring or hard-coded value in a class we've worked on all night.
another tip here is to use preprocessor directives.....
Code:
public class Foo
{
private bool m_doSomethingInternal;
public Foo()
{
//TODO: remove this hard coded value before going to bed
this.m_doSomethingInternal = true;
}
} yes i know - it's an example and you wouldn't do this - ever.....
uhu...of course not - just testing boss...well it happens and i've seen quite a lot of examples of this happen even from very experienced developers. I've done it before..heaps of times..developers are smart cookies, but we do make mistakes...but - since i've started encapsulating these kind of things with conditional directives it just haven't happened.
Code:
#define DEBUG
using System;
using System.Shizzle;
public class Foo
{
private bool m_doSomethingInternal;
public Foo()
{
#if DEBUG
//TODO: remove this hard coded value before going to bed
this.m_doSomethingInternal = true;
#endif
}
} the most common preprocessor directive you'd be using would be the #region and #endregion directives...
this concept is of course taken from the C language so those of you who has that background would be familiar with this as well.
of course preprocessing can be used for many other functions but this yet another way to let the developers breathe easier.
cheers and happy coding...