It's not the most obvious piece of logic.
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
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.
-
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.
-
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
.I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
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
if (!isValid(value)){ throw new ArgumentOutOfRangeException("....");}
Then someone decided to simplify it by replacing isValid(value) with (value>0). -
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
if (!isValid(value)){ throw new ArgumentOutOfRangeException("....");}
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.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
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.
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
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.
-
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.
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
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:
/* Either foo or bar must be true */
if (!(foo || bar))
throw new InvalidArgumentException("Neither foo nor bar was valid");Than to say:
/* Either foo or bar must be true */
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.
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
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.
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
I can top it off! :laugh:
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.
-
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.
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.
-
I can top it off! :laugh:
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. :)
-
"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.
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
I wouldn't really call that a "gem".
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
It doesn't make much sense for an
int
, but it might make sense for anint?
(Nullable<Int32>
). The ordering operators (<, <=, >, >=) onNullable<T>
will always returnfalse
if either operand isnull
, so!(value > 0)
would be equivalent tovalue == null || value <= 0
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
So, today I came across this gem:
// If the value is NOT greater than zero, throw an exception.
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}Could we not do this?
if (value <= 0)
:rolleyes:
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
In Visual C++ MFC, there's a CString. And to check if a CString has values, it's "if (!str.IsEmpty()). So the QA person, who had a degree from Stevens Institute of Technology, sat there and argued with me for 2 HOURS, and brought it all the way up to the VP, because it wasn't the "obvious way to do the logical operation." Didn't matter at all that is was the way Microsoft implemented it, and we had to use it.
-
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.
Hand written, sadly.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
It doesn't make much sense for an
int
, but it might make sense for anint?
(Nullable<Int32>
). The ordering operators (<, <=, >, >=) onNullable<T>
will always returnfalse
if either operand isnull
, so!(value > 0)
would be equivalent tovalue == null || value <= 0
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
It's an int, nothing but the int, so help me glod.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads