J
JKop
Take a class like the following:
class Finger
{
public:
double length;
}
I'm writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:
unsigned double length;
But ofcourse doesn't compile.
So... I've come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will:
A1) Throw an exception
A2) Return a false boolean value indicating failure
B) Just document that the code will have undefined behaviour if length is
set to a negative number.
Which would you suggest? Any other suggestions?
I myself am leaning toward B.
Thanks
-JKop
class Finger
{
public:
double length;
}
I'm writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:
unsigned double length;
But ofcourse doesn't compile.
So... I've come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will:
A1) Throw an exception
A2) Return a false boolean value indicating failure
B) Just document that the code will have undefined behaviour if length is
set to a negative number.
Which would you suggest? Any other suggestions?
I myself am leaning toward B.
Thanks
-JKop