EDIT:
Sometimes it happens that our problem is kind of not in code but in our way of judging what is right and wrong. I am writing my problem through an example as it would help to explain it. The problem is in short “should our function handle each exception in iteself. Through the example i showded it sometimes become confusing and more like writing same code everywhere.
When we write code in C its kind of flowing procedural language. So we handle situations as they come. For example, given (a,b,c) to find which triangle type it is (NOT_POSSIBLE, Equilateral, Isosceles, Scalar). We input these and go on checking first equilateral then isosceles and then scalar, etc.
In C++ we make functions to check these conditions (so isEuilateral() and isScalar() are two functions that checks it).
Problem is:
now isScalar() function “should” check whether it is equilateral or not and then its own condition, or just simply its conditions.
Is it the headache of isScalar() function to check whether it is equilateral triangle or not, that is:
if(isEquilateral())
return false;
else if(a==b)
return true;
else if (b==c)
return true;
...
Or start from itself:
if(a==b) return true; // not checking equilateral
....
Or it should do its work only and provide functions. It would be programmers duty to check object.isEquilateral() before object.isScalar().
so I devloped this class api Triangle and send it to you. U know only function name like isEquilateral() and isIsosceles() like… so you are assigned job to make an application that inputs 3sides of triangle a,b,c and tells what kind of triangle is this. .. ok.. input is 3,3,3, your program then should check about equilateral condition before isoceles condition and calling isIsoceles() without before calling isEquilateral() is not safe for this output. . isIsoceles () is not an independent function now.. NOt complete or is it not a bug. what to do?
Well actually this is an example which does not contain all of my confusion.
So question is should each function handle each exception explicitly. Would not that be redundancy and duplication of code.
Note that we cannot make a function for checking all exceptions because some exceptions here would not be exceptions at some other place.
The simpler approach I would use is a
classifyTrianglefunction, that returns anenumof triangle types. Then, any subsequent code can just consult the return value to act according to the triangle type.Alternatively, I believe that each function should concentrate on checking its own condition, regardless of other triangle types.
Edit: Now that I’ve re-read the question, I believe you’re asking about precedence. Meaning, a triangle can be equilateral and isosceles, but you want it to be classified as equilateral only. If you have a validation function for each type, then you want the user to use your predetermined order of call. This is resolved by using a
classifyTrianlgefunction, where you can decide on the order you wish.