Defensive Programming Techniques for any language

Never put code in else. What goes within else can change if someone changes conditions tomorrow. Always check exact a.k.a else if (condition) explicitly.

Never assume something of the underlying framework. If the object supports a close + dispose, calling close() maybe a good idea, there was a case where the framework would not actually call close when dispose was called. So this is a good idea.

Don't assume that books provide the best practices for anything. Try it out and find the best practices yourself.

Your life can be a hell of NULL condition checks when you define properties of lists or complex objects. Maybe the best idea is to instantiate these properties in the constructor so they will never be NULL to begin with?

Try not to put any kind of conditions within conditions which make it difficult to read the code, set the IF condition in such a way if possible that you don't even process further if it is false, then the next line of code will be at the same level as the IF condition, otherwise you will end up in TAB hell.

Don't use a technology because it is cool or everyone is raving about it. A lot of programmers have ended up in hell because of unwanted use of XML, Silverlight, WCF, MVC, Azure - you name it - any technology used just for the sake of its "fame" and not really because it solves a problem just makes life more difficult.

Comments

Popular posts from this blog

Tutorial: Using Google Cloud Storage from C# and .NET

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?

Enable Visual Studio to use more than 2GB of memory