Geeks With Blogs

@mattwatson81
  • mattwatson81 So Snapchat might be worth a billion dollars but yet has no revenue... That's insane because it is also likely a fad that will die soon. about 437 days ago
  • mattwatson81 If it works, stop coding. Nobody cares how it works. about 439 days ago
  • mattwatson81 @dmobbes I will be driving it to every SKC game. So see you there! about 446 days ago

Matt Watson Software developer, product visionary, and master of #dadops

Originally published on http://www.stackify.com/golden-rule-programming/

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
     }
}

One of the most common causes is 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!

Posted on Friday, January 18, 2013 7:13 AM | Back to top


Comments on this post: The Golden Rule of Programming

# re: The Golden Rule of Programming
Requesting Gravatar...
KA-BOOM! :)
This is a great advice, sadly it is often totally ignored when teaching programming.
Left by SuperstarX64 on Jan 18, 2013 9:26 AM

Your comment:
 (will show your gravatar)
 


Copyright © Matt Watson | Powered by: GeeksWithBlogs.net | Join free