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

Tagged as

The Golden Rule of Programming

, 18 Jan 2013
Rate this:
Please Sign up or sign in to vote.
There is one particular issue that seems to be the root of most bugs in software programs. I see it over and over and over.

There is one particular issue that seems to be the root of most bugs in software programs. I see it over and over and over. Like most programmers, it has caused me heartburn since the day I started programming. So I have worked hard to make sure that my code never breaks my golden rule. So now I remind my development team every chance I get to also follow what I call the golden rule of programming. I think every new programmer needs a tatoo that says it.

So what is the golden rule of programming?

"If it can be null, it will be null"

Null reference type errors are responsible for a good percentage of all application bugs. They are usually very simple problems. Typically caused by not adding additional logic to ensure that objects have valid values before using them.

How about some simple examples of null values causing problems?

Here is a classic example below. A string variable passed in to a method is used before ensuring it isn't null. KA-BOOM!

public void DoSomething(string text)
{
     if (text.ToUpper() == "Hello World")
     {
          //do something
     }
}

Some of the most common causes are settings, database calls, or API type calls not returning expected values. For example, you add a new field to your database and don't populate default values for every record. Randomly records get queried and the code didn't account for that new field being null. KA-BOOM!

The good news is that a lot of null reference errors can be avoided by adding additional logic and code to ensure objects are not null before trying to use them. Developers should always assume that everything is FUBAR and be very defensive in their code. Pretend every database call is going to fail, every field is going to have messed up data in it.

Some tips to prevent null reference exceptions:

  1. Initialize variables with valid values.
  2. If a variable can be null, then check for null and handle it appropriately
  3. For C# use the ?? operator with strings when appropriate. ex. (stringvar ?? "").ToUpper()

If you follow my golden rule, I promise you will have far fewer bugs in your apps. Because if it can be null, it will be null! 

License

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

Share

About the Author

Matt Watson (Stackify)
CEO Stackify
United States United States
Founder & CEO of Stackify. Focused on helping software development teams support their production applications.
Follow on   Twitter

Comments and Discussions

 
GeneralAgree 100% PinmemberH.Brydon27-Jan-13 17:11 
I've followed this rule for several years (in C, C++) but I never had a name for it.
 
Good stuff!
--
Harvey

QuestionNice little tip to go with my coffee. Can I add my point? PingroupGrasshopper.iics21-Jan-13 1:15 

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 | Mobile
Web01 | 2.8.140827.1 | Last Updated 18 Jan 2013
Article Copyright 2013 by Matt Watson (Stackify)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid