locked
Can we get a Contract.Fail() method that doesn't generate warnings? RRS feed

  • Question

  • I currently tend to create code like this for exceptional or unexpected conditions like in a switch statememt:

     default:
         string message = String.Format("MarkerCommands.CommandId {0} is not recognized", commandId);
         Trace.WriteLine(message);
         Contract.Assert(false, message);
        return String.Empty;

    I want the release code to at least spit out something if someone is listening but I want the debug code to stop dead in its tracks cause I didn't expect this particular switch value.  However, the Contract.Assert(false, message) statement generates a warning that it is false.  True enough.  I'd be happier if I had a Contract.Fail(message) method that wouldn't generate this warning and it would more accurately reflect my intent.  Note that .NET does provide Debug.Fail().

     

    • Edited by Keith Hill MVPMVP Monday, September 27, 2010 2:57 PM formatted code to not wrap
    Monday, September 27, 2010 2:56 PM

Answers

  • Hi Keith,

    You can use Contract.Assume, which the static checker does not try to prove.  The static checker will always attempt to prove Contract.Assert.

    Note that another option may be to add Contract.Requires to your method to explicitly specify the scope of allowable values for the parameter that is being used in the switch statement, assuming the scope of case values can be calculated within a precondition.  Then, you won't even need a default case at all (except maybe to make the C# compiler happy).  Unfortunately, this isn't always possible even for some simple cases, such as switching on an enum (because enums, as with other type-system related preconditions, aren't yet proven by the static checker).

    - Dave


    http://davesexton.com/blog
    Monday, September 27, 2010 8:24 PM
  • Hi Keith,

      one way of solving your problem is by using Contract.Assume: it will not generate the warning (unless you are using the "check redundant assumptions" switch in the VS pane - in this case will only be a message).

      The other way of solving it is by using the SuppressMessage attribute to filter the warning on this message.

    Hope it helps.

    f

    Tuesday, September 28, 2010 5:27 AM

All replies

  • Hi Keith,

    You can use Contract.Assume, which the static checker does not try to prove.  The static checker will always attempt to prove Contract.Assert.

    Note that another option may be to add Contract.Requires to your method to explicitly specify the scope of allowable values for the parameter that is being used in the switch statement, assuming the scope of case values can be calculated within a precondition.  Then, you won't even need a default case at all (except maybe to make the C# compiler happy).  Unfortunately, this isn't always possible even for some simple cases, such as switching on an enum (because enums, as with other type-system related preconditions, aren't yet proven by the static checker).

    - Dave


    http://davesexton.com/blog
    Monday, September 27, 2010 8:24 PM
  • Hi Keith,

      one way of solving your problem is by using Contract.Assume: it will not generate the warning (unless you are using the "check redundant assumptions" switch in the VS pane - in this case will only be a message).

      The other way of solving it is by using the SuppressMessage attribute to filter the warning on this message.

    Hope it helps.

    f

    Tuesday, September 28, 2010 5:27 AM