Debug.Assert vs Exceptions

前端 未结 8 1130
面向向阳花
面向向阳花 2020-12-13 21:03

Surprisingly I was only able to find one previous question on SO about this subject, and I\'d just like to get the community \"Vote of Confidence\" (or not!) on my approach.

8条回答
  •  时光取名叫无心
    2020-12-13 22:04

    First, MyClass being valid should be, of course, expressed by MyClass's invariant.

    Second, you say "We expect MyMode to be updated by external users of this class" - of course the setter of this mode should have the typical design-by-contract form (as any public function):

      void Setter(mode m)
      {
        // INVARIANT ASSERT (1)
        // PRECONDITION ASSERTS (uses "m") (2)
    
        // BODY (3)
    
        // POSTCONDITION ASSERTS (if any) (4)
        // INVARIANT ASSERT (5)
      }
    

    In (5) you would fail with a screaming assertion violation that the invariant doesn't hold. But in (2) you would fail, earlier, because the mode m passed is invalid. This would send a clear message to the user and thus solves your problem.

    And please don't tell me that the mode field is public and users change it without any control whatsoever.

    Edit: About assertions and release mode, see also:

    • Are assertions always bad?

    • When should assertions stay in production code?

    • Design by contract using assertions or exceptions?

提交回复
热议问题