C# Code Contracts userMessage Parameter -


i using code contracts in c# bit curious should typing in usermessage parameter. give short example.

i have following assertion in code:

contract.assert(isvalidreferencedata(refdata)); 

this message never displayed user, nice have english message description in exception myself , other developers/maintainers of software.

initially thought

contract.assert(isvalidreferencedata(refdata), "payment reference not valid"); 

but thought usermessage complete opposite boolean condition, rewrote as:

contract.assert(isvalidreferencedata(refdata), "payment reference valid"); 

therefore message , condition same. confuse people when see exception report , think, "hang on, if reference valid, why exception thrown?".

lastly, thought, why not make neutral statement, says what must true:

contract.assert(isvalidreferencedata(refdata), "payment reference must valid"); 

which of above best practice? want get messages right because plan use assertions on place, prevent irregularities in data, , enabling run-time checking.

it's worth mentioning although there no guidelines user messages on failing code contracts, there xunit test pattern called expectation describing message fits last example. answering question should have happened in assertion message, fulfill both of goals: code readability + providing helpful, self-explanatory debug message.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -