Search code examples
debuggingassertions

Benefits of Assertive Programming


What is the point of putting asserts into our code ? What are the benefits of assertive programming ?

private void WriteMessage(string message)
{
    Debug.Assert(message != null, "message is null");

    File.WriteAllText(FILE_PATH, message);
}

For example we can check the message variable and throw an exception here. Why do I use assert here ? Or is this a wrong example to see benefits of asserts ?


Solution

  • They also support the philosophy of fail fast, explained in this article by Jim Shore.