|
Yes, things like that are really a pain to find.
You didn't have to post that much code to get the point across though.
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
|
jp nz, {++}
jp {+}
++:
I might be missing something, but why isn't that just
jr z, {+}
|
|
|
|
|
I think I'll take CIDiv's advice next time....you're probably not missing something...
|
|
|
|
|
That's NOT what she said!!!
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
So, today I came across this gem:
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
} Could we not do this?
if (value <= 0)
|
|
|
|
|
Depends on the language and implementation. Perhaps the operators are implemented differently (if they are overloaded). For example, sometimes it makes sense to compare two butterflies for equality, but you can't say one is greater than another. Or maybe only one operator was overloaded, but another wasn't (a poor practice in itself).
That being said, whoever wrote that was probably just being a doofus.
|
|
|
|
|
To put in context - it's a C# int .
|
|
|
|
|
When I see code like you quoted, I go looking for things like
unsigned short value;
just in case the comparison *might* be intentionally smart.
[OT, but you can see the thought process]
Java's lack of unsigned integer types is:
(a) a PITA
(b) a lifesaver
(c) both
(d) none of the above.
Discuss.
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
e) A symptom of scripting languages.
|
|
|
|
|
Unsignedness would not excuse the author (value == 0 ), floats may have (NaN would behave differently)
|
|
|
|
|
Pete O'Hanlon wrote: if (!(value > 0)){ throw new ArgumentOutOfRangeException("....");}
Could we not do this?
if (value <= 0)
There could be a perfectly reasonable explanation: The code maybe like the following previously
<br />
if (!isValid(value)){ throw new ArgumentOutOfRangeException("....");}<br />
Then someone decided to simplify it by replacing isValid(value) with (value>0).
|
|
|
|
|
Sadly not. I know the person who wrote this - he's not that big on any form of refactoring.
|
|
|
|
|
It might just be a case of lazy programming. I've seen code where if statements were initially written in the opposite state of what was intended and then instead of fixing the whole statement, the ! was simply thrown in front.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
That is the most likely explanation.
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
|
I think this is the case also. Every once in a while (once in a blue moon), I do it too. Not that I'm lazy...I just had a logical bug, decided to see if the opposite would work, it did work, so I move to the next problem thinking I'll come back and fix it later.
|
|
|
|
|
Sounds like it was written by a lawyer.
|
|
|
|
|
It may be that the person is using a general pattern of testing whether required input conditions are not met by throwing on their inverse. When preconditions are more complicated, it can sometimes be cleaner say something like:
if (!(foo || bar))
throw new InvalidArgumentException("Neither foo nor bar was valid");
Than to say:
if (!foo && !bar)
throw new InvalidArgumentException("Neither foo nor bar was valid");
If one habitually writes pre-checks based upon preconditions, code will probably read better if one consistently uses the same style. If there will be any need to test preconditions using throw-if-not-met logic, it may be best to do so consistently.
Also, btw, if one is going to be pasting code into something like a web-based forum, it may be helpful to write conditions so as to avoid the less-than sign. My usual rewrite of a less-than-zero condition for web posting would typically be "0 > whatever" rather than "!(whatever >= 0)", but some people may prefer the latter style.
|
|
|
|
|
Of course you could use your form:
if (value <= 0)
But if values greater than zero are OK, and anything else should give an exception, then why not write it in the original form:
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}
This could be considered a more direct translation of the requirement, and therefore more understandable.
Of course, it may just have been a lack of thought, but I'd argue there are times when it would be beneficial not to simplify expressions, particularly when you have a compiler that will reduce it to the same form for you so there is no performance penalty.
|
|
|
|
|
I can top it off!
static void Main(string[] args)
{
var b = true;
if (!!!!!!!!b != false)
Console.WriteLine("hu?");
else
Console.WriteLine("ha!");
}
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
"hu?" is printed.
|
|
|
|
|
Indeed!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
I use less ! on my messenger
-
Bits and Bytes Rules!
10(jk)
|
|
|
|
|
I wouldn't really call that a "gem".
|
|
|
|
|
Another alternative that I didn't see mentioned in the replies. Was this code hand written or produced by a generator?
I've been playing with the CodeDOM and there are some structures that you can define that would likely generate exactly that code.
|
|
|
|
|