The importance of assertion statements.

When implementing a complex system, where there are many variables/objects, I can’t stress enough how important it is to have some sort of assertion tests in your code.
In C# we can write assertion tests that are only run when compiled in Debug mode like so:

public void ReturnPointLight(PointLight3D light)
    // Assertion tests.

    Debug.Assert(light == _pointLightPool[light.PoolIndex]);
    Debug.Assert(_pointLightAvailable[light.PoolIndex] == false);

    // Make sure that light is not already in available stack.
    foreach (int index in _pointLightAvailableIndices)
        Debug.Assert(index != light.PoolIndex);

    _pointLightAvailable[light.PoolIndex] = true;

That is an excerpt from my LightingManager class, which has a PointLight pool built in.

That function returns a point light to the pool when it is no longer needed. This is roughly the equivalent of doing free(myPointLightInstance) in C or C++ when the instance is no longer being used. Sadly, the compact CLR that the XBOX uses has a terrible garbage collection implementation, so using object pools and allocating and freeing objects from the pools manually is a necessity.
As you can see in this function, several assertions are done.

  • Assert that the light instances’ index matches the index of the internal pool.
  • Assert that the light is actually not available (because the function is meant to make unavailable lights available).
  • Assert that the light is not already stated as being available in the availability stack.

These checks are a bit slow, so I run them only when the DEBUG flag is set.
Edit: Using the DEBUG conditional is not necessary if you only write Debug.Assert() since Debug methods are only called if the DEBUG flag is set automatically, but are useful if you’re writing other code, as I have above.
And actually, the bool[] _pointLightAvailable array is only used for debugging/testing. Setting up some decent sanity checks light this throughout my code makes development much faster in the long run, because I can be confident that things are doing what they are supposed to.

With the lighting manager in particular, it would be easy for the manager to do something unwanted, and cause the lights to LOOK wrong, although the program will not crash. In this type of case, it’s hard to know what’s causing the problem without lengthy code tracing/debugging if you don’t have some pre/post condition assertions in the code.

This entry was posted in Coding, DBP2011, XNA and tagged . Bookmark the permalink.

6 Responses to The importance of assertion statements.

  1. Martin says:

    Thanks for taking the time to write these posts :) Very interesting to follow the development, and especially to see which algorithms you choose to implement, how and where you find the theory behind them.

  2. olhovsky says:

    Thanks Martin! Knowing that people actually care keeps me going :)

  3. Lavonn says:

    en6fZV I’m not easily impressed. . . but that’s impressing me! :)

  4. Hello there, just became alert to your blog through Google, and found that it is really informative.
    I’m gonna watch out for brussels. I will appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

  5. Thanks for finally talking about > The importance of assertion statements.
    | Olhovsky < Liked it!

  6. csgo weapon says:

    Astonishingly user friendly website. Astounding details readily available on couple of gos to

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>