Retrace will reach End of Life on March 31, 2027. Click here to learn more.

The Golden Rule of Programming

By: mwatson
  |  January 21, 2013
The Golden Rule of 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. 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!

Also Read-https://stackify.com/should-you-use-flutter-in-your-next-project/

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More
situs toto pucuk4d indobet toto slot slot depo 10k 8kuda4d judi bola toto toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto ilmutoto https://foundvinylrecords.com/ depo 5k panen100 semibola popo togel https://asupantoto.co/ pompa4d slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ https://shopjoliboutique.com/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot situs toto slot situs slot pakde4d link slot popotogel login nobu99 logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo bobatoto istanaslot sumbartoto mataramtoto meteorbet88 login rp888 AMANAHTOTO www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot toto slot SLOT GACOR slot gacor slot bento11 link slot gacor AMANAHTOTO KARI4D PUCUK4D pascol4d situs slot situs slot toto slot titi4d KARI4D DVTOTO PUCUK4D AMANAHTOTO slot gacor toto toto slot slot AMANAHTOTO toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot AMANAHTOTO https://www.asupan188.dev/ slot gacor gampang menang coloktoto coloktoto coloktoto toto slot toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor slot gacor slot gacor slot gacor situs slot link slot toto toto toto slot gacor slot resmi slot88 situs toto MySlot188 SlotPoker188 slot gacor slot gacor pucuk4d PASCOL4D slot gacor slot gacor situs slot link slot pentaslot SLOT GACOR AMANAHTOTO TOTO 4D pajaktoto slotvip toto togel situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 toto slot slot gacor situs slot link slot
situs toto pucuk4d indobet toto slot slot depo 10k 8kuda4d judi bola toto toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto ilmutoto https://foundvinylrecords.com/ depo 5k panen100 semibola popo togel https://asupantoto.co/ pompa4d slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ https://shopjoliboutique.com/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot situs toto slot situs slot pakde4d link slot popotogel login nobu99 logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo bobatoto istanaslot sumbartoto mataramtoto meteorbet88 login rp888 AMANAHTOTO www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot toto slot SLOT GACOR slot gacor slot bento11 link slot gacor AMANAHTOTO KARI4D PUCUK4D pascol4d situs slot situs slot toto slot titi4d KARI4D DVTOTO PUCUK4D AMANAHTOTO slot gacor toto toto slot slot AMANAHTOTO toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot AMANAHTOTO https://www.asupan188.dev/ slot gacor gampang menang coloktoto coloktoto coloktoto toto slot toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor slot gacor slot gacor slot gacor situs slot link slot toto toto toto slot gacor slot resmi slot88 situs toto MySlot188 SlotPoker188 slot gacor slot gacor pucuk4d PASCOL4D slot gacor slot gacor situs slot link slot pentaslot SLOT GACOR AMANAHTOTO TOTO 4D pajaktoto slotvip toto togel situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 toto slot slot gacor situs slot link slot