The reason for redundant code
-
It saves you from having to learn those pesky boolean operators...
if( ((input1 < '1') && (input1 > '9')) ||
((input2 < '1') && (input2 > '9')) )
return INPUT_ERROR;
input1 -= 48;
input2 -= 48;
if( ((input1 < 1) || (input1 > 9)) ||
((input2 < 1) || (input2 > 9)) )
return INPUT_ERROR;
-
Hey! do you have something against code reuse?
-
If you read carefully, you'll see that the code was not exactly reused. The condition in the first if-statement always fails.
-
@TGV said:
If you read carefully, you'll see that the code was not exactly reused. The condition in the first if-statement always fails.
Not always. What if it's double threaded and those two values change between the evaluation of whether they're less than 1 and greater than 9?
-
@belgariontheking said:
What if it's double threaded and those two values change between the evaluation of whether they're less than 1 and greater than 9?
A good compiler should optimize away the second memory read (or the complete comparison), i.e. you have to declare the variables volatile for that to happen. Or you can overload the operator.
-
@strcmp said:
you have to declare the variables volatile for that to happen
How do you know they aren't declared volatile in that snippet?
-
@Spectre said:
@strcmp said:
you have to declare the variables volatile for that to happen
How do you know they aren't declared volatile in that snippet?
Easy:
@Spectre said:
they aren't declared volatile in that snippet
-
@strcmp said:
@Spectre said:
@strcmp said:
you have to declare the variables volatile for that to happen
How do you know they aren't declared volatile in that snippet?
Easy:
@Spectre said:
they aren't declared volatile in that snippet
They aren't declared at all in the snippet.
-
operator<( const InputType& i, const char& c ) { return rand() < int(c); }
operator<( const InputType& i, const int& n ) { return int(i) < n; }
-
dear sir, the rand() keeps changing, how can i fix this?