Tag Info

New answers tagged

1

There is some ambiguity with the term "preconditions": it could mean "do not call the function if these are not valid" (that is, something that client code should handle) it could mean "if these are not valid in the function, (log message and) compute nothing" it could mean "if these are not valid exit the application" When implementing this, you should ...


0

You can also calculate the result, and log if it fails. Something like this : bool Foo::areConditionsValid(String action) { const bool result = isConditionValid() && isTheOtherConditionValid(); if (!result) { log(action + " not possible because some conditions are not valid"); } return result; } ...


0

Simply use an implementation of code contracts, in some implementations such as one provided in. Net these conditions can be checked in compile time. https://github.com/Microsoft/CodeContracts


2

A problem I can see with your approach is that your preconditions return, instead of throwing an exception. Usually, preconditions throw an exception when violated. Since you are using C++, asserts can be used instead. If you use exceptions/asserts, an additional refactoring can be made easily. Instead of: if (!isConditionValid()) { log("doFoo not ...



Top 50 recent answers are included