For instance returning DivideResult.Success means Operation finished with calculation, DivideResult.DivisorIsZero means I have to inform about special value of input parameter etc.
Mathcad Pattern Match Exception Error Code Review SomeoneAt code review someone just asked: What should happen when n 0.The question is important because is asking about functions contract, not the result of current implementation.Exploring consequenc e s of that simple question, we have a few options: - Avoid the answer, stick to consequences of just created implementation: Function throws an exception when n0.
We do not care what should happen, we deliver something what just works. Or, we can - Add functions contract where we describe what should happen. In that case is not a surprise current contract is same as the result of implementation. ![]() Mathcad Pattern Match Exception Error Professional Developers WeIn the future function will be re-implemented, extended, more code will appear, but contract should be kept because it has been already described As professional developers we decided to define functions contract in comments. Lets see how our function is being invoked when contract has been described and applied: Maybe for someone code above looks OK, but I have strong feeling that in the example above we see handling result is done in two different ways Your function is now popular and widely reused, so a few months later someone decided to extend the function with some business-related calculation. Two new use cases appeared: when both dividend and divisor are 0, we should recognize that result as a special one, and when dividend is less than 0, we should recognize that case as a special one. Lets start to implement new requirements: Our final version is ready. Post-coffee considerations Final list of consequences of our implementation is quite interesting To make new result types (implemented as new two types of exception) compatible with already existing result handlers, we had to inherit ArgumentsAreZeroException and DividentIsNegativeException from ArgumentException. Thanks to that decisions existing try catch handlers will catch new exceptions without code change Both: ArgumentsAreZeroException and DividentIsNegativeException are technically Exceptions but they are exceptions only to satisfy compiler and go to extra handlers defined via try catch clauses. It doesnt sound like model way of handling results of calculation Handling result of operation is messy actually our function has 4 types of results, where one of them is calculated in standard C way via function invocation, where the next three types are handled in exceptional way. The idea of family of types is not new and is already quite widely implemented. ![]() Implementation details: all types are grouped together as nested classes of DivideResult (marker container class). DivideResult class does not contain logic, does not allow creating more sub-classes via inheritance then already defined nested classes. That inheritance approach is technically similar to already implemented solutions in other mentioned languages. Using pattern matching switch expression from C 8 (preview) is the key part of increasing readability in our new multi-type code. Some of types contains internal state, and in normal C code we expose them explicitly as models properties. In code above we keep state private (is pointless to expose state right now), and allow reading internal state via deconstruction to use them in pattern matching. In coders life Exceptions should be used when situations is really exceptional. Calculating result of dividing does not seem to be place when really exceptional situation can occur, isnt it In our function we only have to decide: what action we should take( To Divide or Not to Divide ) and what type represents meaning of the action taken internally in the function.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |