When n<0 won’t do

I found this classic “WTF” example over at a site called The Daily WTF:

This isn’t the first time we’ve seen programmers having a difficult time gasping the intricacies of negative numbers. Heck, I’m sure we all had a little difficulty understanding at first, in the fifth grade. But you’d really think that someone able to program (am I giving too much credit here?), would have a little more sophistication than to distinguish positive and negative numbers with the “little line thing“ in prefixing the number.

//return whether a double is negative bool IsNegative(double n)
{
string nStr = n.ToString();
if (nstr.IndexOf('-', 0, 1)) return true;
return false;
}

I must say, I do appreciate the irony that many students shy away from Computer Science because they fear the complicated math. If only they knew.

I literally sat stunned for a full minute after seeing this.

Published
Categorized as tech

By Keith Survell

Geek, professional programmer, amateur photographer, crazy rabbit guy, only slightly obsessed with cute things.

2 comments

  1. That’s funny – happened on your site while searching for something else, but had to comment. Convert to a string and search for the “-“. Funny! I’m no genius, but here’s what I would do:

    //return whether a double is negative bool IsNegative(double n)
    {
    if (n

  2. There simply is no need for a function to determine if a number is negative. At worst it’s a waste of space and slows down the program; at best it’s just useless syntactic sugar. The only thing you need is:

    if (n < 0) And that's it. (For the non-technical readers out there, I bet the title of this post suddenly makes sense now.) No function; no returning of booleans, nothing like that. Making a function call for that is just a waste. It's not like the definition of a negative number is going to change. Sheesh.

Comments are closed.