The question I want to ask you is quite wide but in the same time it's very concrete. First, I have to say, that I mostly interested in answers which are applicable in the .net environment.
Well, I want to increase the level of the code I produce. Now I mostly use the TDD and the static code analysis to ensure that my code is correct. Recently I've listened to Dino Esposito's speech about code contracts and now I want to use it in conjunction with other techniques. While listening to Dino I've also recalled the Debug.Assert()
and Trace.Assert()
.
To be concrete I will ask several questions:
- How should I write the contracts and unit tests to complement each other?
- Should I use code contracts in every method or in public methods only?
- Should I prevent the usage of
Debug.Assert()
? When it's OK to use them? (For example, notice that invariants in .net are checked only on public method/property exit. So, is it OK to make some checks in the middle of the method by simpleAssert()
?) - Could you please recommend me the open source project where all these techniques are properly used because a picture paints a thousand words?
Debug.Assert
is that it never reaches production. You'll only find bugs with it in your dev/test environment. If such a bug that the assert would catch reaches the wild, it will make your repro harder. Using it instead of a release-available assert is an optimization step, and premature optimization is never good. – Merlyn Morgan-Graham Sep 4 '11 at 21:20Trace.Assert()
instead ofDebug.Assert()
. Here what MSDN says about it: > By default, the Debug.Assert method works only in debug builds. Use the Trace.Assert method if you want to do assertions in release builds. – Igor Soloydenko Sep 5 '11 at 7:36