• (cs)
    Alex Papadimoulis:
    <FONT color=#0000ff>function</FONT> isNegative( n ) {
      <FONT color=#0000ff>return</FONT> n != 0 && n / <FONT color=#000080>Math</FONT>.<FONT color=#0000ff>abs</FONT>( n ) == -1
    }
    

    This is great!  Almost as good as:

             return   !(n == Math.abs( n ))

    <FONT face="Times New Roman">And I assume that the n!=0 is because of Division by Zero errors^H^H^H^Hexceptions that were originally generated by that function.</FONT>

  • (cs)
    <font size="3">function isNegative( n ) {
    </font><font size="3">return n != 0 && n / </font><font size="3">Math.</font><font size="3">abs( n ) == -1
    }</font>

    Did anyone else notice that there is no type specified for the n?  Did Alex Papadimoulopolopoulis goof,
    or is my knowledge of Java fading?  My favorite part about this (well, besides the coder's complete 
    and utter lack of computer programming skill) is the fact that he/she checks to make sure it's not 
    zero first.  They were aware of divide-by-zero issues, but not aware of the "less than" operator.  

    On a sexist note, could this be a sign of an increasing number of women in the tech fields? First
    isNegative, then you start seeing whiteout on computer screens.

    BTW, don't blame me for this post looking like $h!7. This BB software is wack!
  • Steven (unregistered) in reply to bobday

    It's javascript, so some sins must really be forgiven.

    I've often had to do things I'm not proud of to work around some obscure problem that some browser somewhere had, esp in the 'old days' of  Netscape 4 and IE 3.

    BTW, I think IT workers should have evolved past sexism right?

  • Dave (unregistered) in reply to dubwai

    Actually I prefer this:

    <FONT face="Courier New">function isNegative( n ) {
       return n != Math.abs(n);
    }</FONT>

    (Or equivalent)

  • Dave (unregistered) in reply to Dave

    Darn, I should've read all the other posts first before posting that gem...WTF is wrong with me?

  • Dave (unregistered) in reply to dubwai

    n is a complex number or quarternion?

    Pity the type info isn't there.

  • josh (unregistered) in reply to vDave420

    !=, when used to compare booleans, is exclusive or.  That is,
    (A != B) == ((A || B) && !(A && B))

    So this expression requires exactly one of them to be an empty string.  That just seems like an incredibly odd requirement, I expect || would do what's intended.  Perhaps, though, both empty is allowed for a silent no-op or something.

  • (cs) in reply to Rank Amateur

    This thread has made me want to yell Read The Damn Thread First a lot more than usual, like some old codger. But I'm in an irritable mood today.

    Rank Amateur:
    Alex Papadimoulis:
    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }

    Is there anything keeping n from being a float or double? If so, think we got a great heisenbug in here:

    n / Math.abs( n ) = -1.00000000000001.

     


    Thank god someone else here understands floating point arithmetic beyond an 8th-grade algebra level. (VBScript, being the 'friendly' half the MS scripting language family, would fix up the miss. JScript has pretentions to being IEEE-754 compliant.) In fact, I can't remember offhand, but JScript/ECMAScript might promote int->float for division, and then you've got a real mess on your hands.

    Okay, that's the first time I've ever had a 'freetextbox not installed' error. This is getting like bingo, whoever sees all the board problems first wins. =D
  • (cs) in reply to Rank Amateur

    This thread has made me want to yell Read The Damn Thread First a lot more than usual, like some old codger. But I'm in an irritable mood today.

    Rank Amateur:
    Alex Papadimoulis:
    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }

    Is there anything keeping n from being a float or double? If so, think we got a great heisenbug in here:

    n / Math.abs( n ) = -1.00000000000001.

     


    Thank god someone else here understands floating point arithmetic beyond an 8th-grade algebra level. (VBScript, being the 'friendly' half the MS scripting language family, would fix up the miss. JScript has pretentions to being IEEE-754 compliant.) In fact, I can't remember offhand, but JScript/ECMAScript might promote int->float for division, and then you've got a real mess on your hands.

    Okay, that's the first time I've ever had a 'freetextbox not installed' error. This is getting like bingo, whoever sees all the board problems first wins. =D
  • PO8 (unregistered)

    So to summarize the IMHO best responses so far:

    (2)  is not really a WTF.  It's probably broken if used with floating point, and awkward, because it could have been written better.   But its original genesis was to work around < and > bugs in implementations of JavaScript embedded in HTML.

    (3) is not really a WTF.  It's probably C# code, so the comparisons should work properly.  The != is the desired operator, because the search should fail iff exactly one of FirstName and LastName is supplied.

    (1) is a colossal WTF.

    All in all, though, a subpar day for WTF.

  • Romain Guy (unregistered) in reply to WebCudgel

    Actually this is either VB.NET or C# code :)

  • Fabian (unregistered) in reply to Maurits
    Maurits:
    Alex Papadimoulis:
    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }


        return n != Math.abs(n);
    isn't ugly enough?


    Ugly enough alright, but it also assumes 0 is a negative number...
    So it may be a little less ugly, but as opposed to the original WTF it's actually wrong.

    Fabian
  • Fabian (unregistered) in reply to Fabian

    Ehr, where I said said 'negative' I meant 'positive'.

    Fabian

  • (cs) in reply to dubwai
    dubwai:
    Alex Papadimoulis:
     

    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }

    I'm still waiting on someone to explain why this is good way to check for a negative number.  Don't let me down, people.



    You see, the problem is that, as unknown to many people, "if (n < 0)..." might not work for those negative numbers that are either greater than zero, or completely off the number line.

    (How'd I do? :)

        dZ.

  • Zatanix (unregistered) in reply to DZ-Jay
    DZ-Jay:
    dubwai:
    Alex Papadimoulis:
     

    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }

    I'm still waiting on someone to explain why this is good way to check for a negative number.  Don't let me down, people.



    You see, the problem is that, as unknown to many people, "if (n < 0)..." might not work for those negative numbers that are either greater than zero, or completely off the number line.

    (How'd I do? :)

        dZ.



    Excatly, and as someone pointed out earlier, n is probably a complex number, and there is no "<" and ">" ordering of complex numbers. Math.abs(n) is must be the norm of n. So if you view complex numbers as vectors in the complex-plane, he is making sure it isn't the zero-vector (to avoid divide by 0) and that the vector has a specific direction. Trivial stuff, i'm surprised so many of you missed that.
  • nonDev (unregistered) in reply to Maurits
    Maurits:
    Alex Papadimoulis:
    <FONT color=#0000ff>function</FONT> isNegative( n ) {
    <FONT color=#0000ff>return</FONT> n != 0 && n / <FONT color=#000080>Math</FONT>.<FONT color=#0000ff>abs</FONT>( n ) == -1
    }



        return n != Math.abs(n);
    isn't ugly enough?

    No, no, no. We had this many times before. I think we agreed that the best way was defined by wajo:

    bool IsNegative(double d)
    {
    int n = Math.Ceiling(d);
    try
    {
    Object[] o = new Object[n];
    }
    catch (ArgumentOutOfRangeException e)
    {
    return true;
    }
    return false;
    }

  • (cs) in reply to Zatanix
    Anonymous:
    DZ-Jay:
    dubwai:
    Alex Papadimoulis:
     

    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }

    I'm still waiting on someone to explain why this is good way to check for a negative number.  Don't let me down, people.



    You see, the problem is that, as unknown to many people, "if (n < 0)..." might not work for those negative numbers that are either greater than zero, or completely off the number line.

    (How'd I do? :)

        dZ.



    Excatly, and as someone pointed out earlier, n is probably a complex number, and there is no "<" and ">" ordering of complex numbers. Math.abs(n) is must be the norm of n. So if you view complex numbers as vectors in the complex-plane, he is making sure it isn't the zero-vector (to avoid divide by 0) and that the vector has a specific direction. Trivial stuff, i'm surprised so many of you missed that.


    Why would you even write a function called isNegative for complex numbers?  Negativity has no meaning for complex numbers.  If what he really wanted was to check that the imaginary part was zero, and the real part was < zero, then, well, maybe he should have done that.  It would be more efficient, and would avoid ugly floating point rounding errors.  Trivial stuff, I'm surprised you missed that. 
  • (cs) in reply to bobday
    bobday:
    Anonymous:
    DZ-Jay:
    dubwai:
    Alex Papadimoulis:
     

    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }

    I'm still waiting on someone to explain why this is good way to check for a negative number.  Don't let me down, people.



    You see, the problem is that, as unknown to many people, "if (n < 0)..." might not work for those negative numbers that are either greater than zero, or completely off the number line.

    (How'd I do? :)

        dZ.



    Excatly, and as someone pointed out earlier, n is probably a complex number, and there is no "<" and ">" ordering of complex numbers. Math.abs(n) is must be the norm of n. So if you view complex numbers as vectors in the complex-plane, he is making sure it isn't the zero-vector (to avoid divide by 0) and that the vector has a specific direction. Trivial stuff, i'm surprised so many of you missed that.


    Why would you even write a function called isNegative for complex numbers?  Negativity has no meaning for complex numbers.  If what he really wanted was to check that the imaginary part was zero, and the real part was < zero, then, well, maybe he should have done that.  It would be more efficient, and would avoid ugly floating point rounding errors.  Trivial stuff, I'm surprised you missed that. 


    um...I think that was a joke...
  • (cs) in reply to PO8

    Anonymous:

    All in all, though, a subpar day for WTF.

    I've got a great Java WTF that I just haven't had time to submit.  Fear not!

  • bp (unregistered) in reply to spacey
    Anonymous:
    These have to be cases of "Paid by the Hour" versus "Paid by the Line" :)

    Why the hell else would anyone have spent so much time being so damn useless......

    -space


    I see a lot of "Paid by the Hour" in this forum; however I've come to the conclusion now that it's not that these people are paid by the hour... I recently come across a lot of this code and have even been able to speak to some of the perpitrators.

    What I've discovered is pure cluelessness... they actually think this is a better way to do it.

    sigh...
  • bp (unregistered) in reply to A Wizard A True Star
    A Wizard A True Star:
    Alex Papadimoulis:
    <font color="#0000ff">public static final String</font> TRUE = <font color="#ff0000">"true"</font>;
    <font color="#0000ff">public static final String</font> FALSE = <font color="#ff0000">"not true"</font>;

    I don't know Java. Does it really not have a built-in "false" constant?

     



    Thats steam-powered when it comes to modern languages. It didn't have one until the last version when some dumbass added the keywords "true" and "false" and the object java.lang.Boolean.

    Of course, now this fellows code is broken, because they didn't consult him/her and now false != "not true".

    And for those that were actually thinking of flaming this post at the first paragraph, new word for you: sarcasm
  • gab (unregistered) in reply to Jonathan Pryor
    Anonymous:
    <font color="#0000ff">if</font> ((<font color="#0000ff">this</font>.txtLastName.Text=="")!=(<font color="#0000ff">this</font>.txtFirstName.Text==""))
    {
    boolIsValid = <font color="#0000ff">false</font>;
    vldSearch.ErrorMessage = <font color="#ff0000">"The First and Last names are required to conduct a search."</font>;
    }

    Given the naming convention (Hungarian-style prefixes, Pascal-cased property names), this is probably C#, not Java.  In which case <font color="#0000ff">this</font>.txtLastName.Text=="" is correct.

    hungarian? why that?
  • (cs) in reply to gab

    "hungarian? why that?"

    You have heard of Hungarian Notation, haven't you?

  • ding (unregistered)

    one more error:

    function isNegative( n ) {
      <FONT size=+0>return</FONT> n != 0 && n / <FONT size=+0>Math</FONT>.<FONT size=+0>abs</FONT>( n ) == -1 <FONT color=#0000ff>; // he forgot the semicolon</FONT>
    }

    [<:o)]

     

  • (cs) in reply to A Wizard A True Star
    A Wizard A True Star:
    Alex Papadimoulis:
    <font color="#0000ff">public static final String</font> TRUE = <font color="#ff0000">"true"</font>;
    <font color="#0000ff">public static final String</font> FALSE = <font color="#ff0000">"not true"</font>;

    I don't know Java. Does it really not have a built-in "false" constant?


    Nope, it wouldn't be portable.

  • (cs) in reply to WebCudgel
    WebCudgel:
    Alex Papadimoulis:
    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }



    I'll be surprised if they didn't also created isPositive() just to cover all the bases (and I wonder if a zero would be considered positive in that respect).


    No, of course not... zero has it's own case...

    function isZero(n) {
        return (isNegative(n) == isPositive(n));
    }
  • (cs) in reply to ding
    Anonymous:

    one more error:

    function isNegative( n ) {
      <FONT size=+0>return</FONT> n != 0 && n / <FONT size=+0>Math</FONT>.<FONT size=+0>abs</FONT>( n ) == -1 <FONT color=#0000ff>; // he forgot the semicolon</FONT>
    }

    [<:o)]

     

    // Cos JavaScript does not require them (although it is neater to add them)

    Drak

  • (cs)
    Alex Papadimoulis:

    Let's take a quick tour of certain programmers' remarkable abilities to take things so simple and turn them into WTF ...

    First up, and I must say this one takes the cake, is from Billy. As some background, these constants were defined in every class in the Java project. These were used throughout the system in place of their primitive boolean counterparts ...

    <font color="#0000ff">public static final String</font> TRUE = <font color="#ff0000">"true"</font>;
    <font color="#0000ff">public static final String</font> FALSE = <font color="#ff0000">"not true"</font>;

    I see a lot of strange attempts at answering the perplexing question of whether or not a given number is negative. This particular solution that Bob came across while debugging code has to be most creative ...

    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }

    And finally, David Grant shares with this quirky piece of validation code ...

    <font color="#0000ff">if</font> ((<font color="#0000ff">this</font>.txtLastName.Text=="")!=(<font color="#0000ff">this</font>.txtFirstName.Text==""))
    {
    boolIsValid = <font color="#0000ff">false</font>;
    vldSearch.ErrorMessage = <font color="#ff0000">"The First and Last names are required to conduct a search."</font>;
    }



    The first is a "why should I use build in features": the toString() method of a boolean returns its value as a word.

    The second one is a real nice one. I thought about it for a while, because it irritated me. Why didn't he use a simple "< 0"? Hmm... we'll never find out.

    The last one is a typical validating problem. I would've done it like this:
    <font color="#0000ff">    if</font> ((<font color="#0000ff">this</font>.txtLastName.Text.Trim().Length < 1) || (<font color="#0000ff">this</font>.txtFirstName.Text.Trim().Length < 1))
    I usually try to avoid "==". Don't ask why, it's a tradition for me :-)

    Strange enough: I've seen similar code snipplets more than once.
  • BogusDude (unregistered) in reply to rogthefrog
    rogthefrog:
    Alex Papadimoulis:

    And finally, David Grant shares with this quirky piece of validation code ...

    <FONT color=#0000ff>if</FONT> ((<FONT color=#0000ff>this</FONT>.txtLastName.Text=="")!=(<FONT color=#0000ff>this</FONT>.txtFirstName.Text==""))
    {
      boolIsValid = <FONT color=#0000ff>false</FONT>;
      vldSearch.ErrorMessage = <FONT color=#ff0000>"The First and Last names are required to conduct a search."</FONT>;
    }
    

    I've done vaguely similar things to test whether any of several numerical fields were 0 and flag that situation.

    Assume a, b, c, d, e are the fields in question.

    <FONT face="Courier New">bool isValid = (a * b * c * d * e != 0);</FONT>

    Of course I put a comment explaining WTF I was doing that.

    You are better off doing && instead of *. Multiplication is slooooooow.

  • BogusDude (unregistered) in reply to Steve Wahl
    Anonymous:
    Charles Nadolski:

    Also, that first gem not only would risk overflow, but I guarantee that 5 multiplies and a compare will always be slower than just 5 compare statements ;)


    Not necessarily.  Compares represent branches, which can cause pipline stalls and restarts on modern processors.  And, for example, x86 comes out with a compare and jump instruction for each compare, while the multiply version just has a multiply for each followed by a single compare / jump pair.  That's 10 vs. 7 instructions for five vars.  (Determined by looking at  compiler (gcc) output for the following C code:)

    main(int ac, char **av)
    {
        int a, b, c, d, e ;
        int res1 , res2 ;

        a = 16384 ; b = 32768 ;    c = 2 ;    d = 2 ;    e = 2 ;

        res1 = ( (a != 0) && (b != 0) && (c != 0) && (d != 0) && (e != 0) ) ;
        res2 = ( a * b * c * d * e != 0 ) ;

        printf("res1 is %d, res2 is %d\n", res1, res2) ;
    }

    Which, for me, yields "res1 is 1, res2 is 0" (a 32-bit int machine).

    Branches are only a problem when they are unpredicted. That is why branch prediction is so important. Only unpredicted branches cause cache flushes and the like. Also you are forgetting that in order to multiply you likely have to do something like:

    push ax mov ax, 32 push bx mov bx, 2 mul ax, bx at this point, half your result is ax, and the other half is typically in dx. now you still have to do something with the result, which could include both ax and dx. eventually you have to pop ax and bx

    Not quite as simple as: and ax, 4 jge _wherever

  • (cs)

    I think that this:

    <font>if</font> ((<font>this</font>.txtLastName.Text=="")!=(<font>this</font>.txtFirstName.Text==""))
    {
    boolIsValid = <font>false</font>;
    vldSearch.ErrorMessage = <font>"The First and Last names are required to conduct a search."</font>;
    }
    should have been:
    <font>if</font> ((<font>this</font>.txtLastName.Text=="")!=(<font>this</font>.txtFirstName.Text==""))
    {
    boolIsValid = <font>false</font>;
    vldSearch.ErrorMessage = <font>"The First OR Last names are required to conduct a search."</font>;
    }

    The difference? Just the word 'OR'. He definitely wants the user to fill in either first name or the last name, but not both. And one of these fields must be filled. So this is just a spelling error... [:)]

  • (cs) in reply to Katja Bergman

    Katja: "The difference? Just the word 'OR'. He definitely wants the user to fill in either first name or the last name, but not both. And one of these fields must be filled. So this is just a spelling error..."

    Nope, no spelling mistake. This error condition will execute if (and only if) one or other of the two are blank.

    The interesting thing is that it doesn't trigger if both are blank. Is this because previous logic guarantees that at least one is not blank? Or does the following logic not mind if both are blank? Who knows?

    And Teddy (yes, that's his entire name) is still excluded.

  • (cs) in reply to Katja Bergman

    Katja Bergman:

    The difference? Just the word 'OR'. He definitely wants the user to fill in either first name or the last name, but not both. And one of these fields must be filled. So this is just a spelling error... [:)]

    No it would be XOR.  The number of people who have misinterpreted that if in this thread alone qualifies it as a WTF, regardless of whether it works as intended.

  • Bjorn (unregistered)

    What about this :)

    <font>public</font> String isNegative(int n ) {
    if (n != 0 && n / <font>Math</font>.<font>abs</font>( n ) == -1) {
    return TRUE;
    } else {
    return FALSE;
    }
    }
    <font>public</font> String isPositive(int n ) {
    String isNegative = isNegative(n);
    if (isNegative.equals(TRUE) {
    return FALSE;
    } else {
    return TRUE;
    }
    }

  • (cs) in reply to Steve Wahl
    Anonymous:
    Charles Nadolski:

    Also, that first gem not only would risk overflow, but I guarantee that 5 multiplies and a compare will always be slower than just 5 compare statements ;)


    Not necessarily.  Compares represent branches, which can cause pipline stalls and restarts on modern processors.  And, for example, x86 comes out with a compare and jump instruction for each compare, while the multiply version just has a multiply for each followed by a single compare / jump pair.  That's 10 vs. 7 instructions for five vars.  (Determined by looking at  compiler (gcc) output for the following C code:)

    main(int ac, char **av)
    {
        int a, b, c, d, e ;
        int res1 , res2 ;

        a = 16384 ; b = 32768 ;    c = 2 ;    d = 2 ;    e = 2 ;

        res1 = ( (a != 0) && (b != 0) && (c != 0) && (d != 0) && (e != 0) ) ;
        res2 = ( a * b * c * d * e != 0 ) ;

        printf("res1 is %d, res2 is %d\n", res1, res2) ;
    }

    Which, for me, yields "res1 is 1, res2 is 0" (a 32-bit int machine).



    Um, shouldn't res1 and res2 BOTH be equal to one?  Oh wait, you just proved problem #1 with the multiplication solution... OVERFLOW!

    Also, the multiplier won't optimize for the zero case, whereas the five branches will.  So on average, it will be 10/2 instructions compared to a constant  7.  Moreover, You forget that if the comparison is in a for loop, the branch prediction table will be able to properly pipeline all the branches, which would improve it further.
  • Hank Miller (unregistered) in reply to loneprogrammer
    loneprogrammer:
    dubwai:
    Alex Papadimoulis:
     

    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }

    I'm still waiting on someone to explain why this is good way to check for a negative number.  Don't let me down, people.


    I figured this is what happens when you let Math majors write code.  Points off for not including a Proof of Correctness in the comments.


    If the math major minored in physics he would be well aware that quantim effects can flip bits randomly.   There is no known proof of correctness in face of this problem, so such a math major would never write one.


  • (cs) in reply to Bellinghman
    Bellinghman:
    Katja: "The difference? Just the word 'OR'. He definitely wants the user to fill in either first name or the last name, but not both. And one of these fields must be filled. So this is just a spelling error..." Nope, no spelling mistake. This error condition will execute if (and only if) one or other of the two are blank. The interesting thing is that it doesn't trigger if both are blank. Is this because previous logic guarantees that at least one is not blank? Or does the following logic not mind if both are blank? Who knows? And Teddy (yes, that's his entire name) is still excluded.


    <font>if</font> ((<font>this</font>.txtLastName.Text=="")!=(<font>this</font>.txtFirstName.Text==""))
    {
    boolIsValid = <font>false</font>;
    vldSearch.ErrorMessage = <font>"The First XOR Last names are required to conduct a search."</font>;
    }

    :D :D :D :D

  • (cs) in reply to Charles Nadolski

    Charles Nadolski:

      vldSearch.ErrorMessage = <FONT size=+0>"The First XOR Last names are required to conduct a search."</FONT>;
    }

    Actually it should be:

    vldSearch.ErrorMessage = <FONT size=+0>"NOT First XOR Last names are required to conduct a search."</FONT>;

    My mistake.

  • Marc Brooks (unregistered) in reply to Jonathan Pryor

    Anonymous:

    Given the naming convention (Hungarian-style prefixes, Pascal-cased property names), this is probably C#, not Java.  In which case <FONT color=#0000ff>this</FONT>.txtLastName.Text=="" is correct.

    Actually,  (this.txtLastName.Text == null && this.txtLastName.Text.Length == 0) is the preferred way to do blank-string checks in the FCL languages.

  • (cs) in reply to dubwai
    dubwai:

    Katja Bergman:

    The difference? Just the word 'OR'. He definitely wants the user to fill in either first name or the last name, but not both. And one of these fields must be filled. So this is just a spelling error... [:)]

    No it would be XOR.  The number of people who have misinterpreted that if in this thread alone qualifies it as a WTF, regardless of whether it works as intended.


    Yeah, well. In normal English language you won't use the word XOR to make clear you only mean one option. The word OR should be clear enough for that. If you show just a message to the user, the word OR should just be clear enough.

  • (cs) in reply to Katja Bergman

    Katja Bergman:
    Yeah, well. In normal English language you won't use the word XOR to make clear you only mean one option. The word OR should be clear enough for that. If you show just a message to the user, the word OR should just be clear enough.

    Except that it's still not what it means.  The natural English explanation would be: "You must both first name and last name or neither."

  • (cs)
    Alex Papadimoulis:

    <font color="#0000ff">function</font> isNegative( n ) {
    <font color="#0000ff">return</font> n != 0 && n / <font color="#000080">Math</font>.<font color="#0000ff">abs</font>( n ) == -1
    }



    Since this is javascript (and supposably embedded in HTML), maybe the programmer wanted to avoid the < and > operators since they would mess the syntax highlighting of his IDE.
  • monkey (unregistered) in reply to dubwai
    dubwai:

    Except that it's still not what it means.  The natural English explanation would be: "You must both first name and last name or neither."



    dubwai: maybe your natural English.

    Katja Bergman:
    Yeah, well. In normal English language you won't use the word XOR to make clear you only mean one option. The word OR should be clear enough for that. If you show just a message to the user, the word OR should just be clear enough.


    this whole XOR/OR/AND stuff gets confussing due to language we are all used to || in JavaScript etc meaning the same as OR in VB.

    In english OR generally means XOR ie 'do you want tea OR coffee' yes some people do answer 'yes I want tea or coffee' but generally they don't get made anything for being 'annoying'

    come on if english used the same AND and OR as logic does then user requirements would start to mean the same thing to developers and users and what help would that be?
  • (cs) in reply to monkey
    Anonymous:
    dubwai:

    Except that it's still not what it means.  The natural English explanation would be: "You must both first name and last name or neither."



    dubwai: maybe your natural English.

    And another person here demonstrates that he/she doesn't understand what that code does.

  • (cs) in reply to dubwai
    dubwai:

    Charles Nadolski:

      vldSearch.ErrorMessage = <font size="+0">"The First XOR Last names are required to conduct a search."</font>;
    }

    Actually it should be:

    vldSearch.ErrorMessage = <font size="+0">"NOT First XOR Last names are required to conduct a search."</font>;

    My mistake.



    To continue down this path of sillyness...

    <font>if</font> ((<font>this</font>.txtLastName.Text=="")!=(<font>this</font>.txtFirstName.Text==""))
    {
    boolIsValid = <font>false</font>;
    vldSearch.ErrorMessage = <font>"The First XNOR Last names are required to conduct a search."</font>;
    }


  • PokeyProgrammer (unregistered) in reply to Jonathan Pryor

    I agree.  Especially considering that Java has no "bool" data type (it's "boolean"), and neither does JavaScript (it's "var").

  • PokeyProgrammer (unregistered) in reply to Jonathan Pryor
    Jonathan Pryor:
    <font color="#0000ff">if</font> ((<font color="#0000ff">this</font>.txtLastName.Text=="")!=(<font color="#0000ff">this</font>.txtFirstName.Text==""))
    {
    bool IsValid = <font color="#0000ff">false</font>;
    vldSearch.ErrorMessage = <font color="#ff0000">"The First and Last names are required to conduct a search."</font>;
    }

    Given the naming convention (Hungarian-style prefixes, Pascal-cased property names), this is probably C#, not Java.  In which case <font color="#0000ff">this</font>.txtLastName.Text=="" is correct.


    Doh!  My last post was supposed to include this...
  • (cs)
    Alex Papadimoulis:
    <FONT color=#0000ff>function</FONT> isNegative( n ) {
      <FONT color=#0000ff>return</FONT> n != 0 && n / <FONT color=#000080>Math</FONT>.<FONT color=#0000ff>abs</FONT>( n ) == -1
    }

    I haven't seen the most obvious:

    <FONT color=#0000ff>function</FONT> isNegative( n ) {
      <FONT color=#0000ff>return</FONT> <FONT color=#000080>Math</FONT>.signum( n ) == -1
    }

  • (cs) in reply to redtetrahedron
    redtetrahedron:
    Alex Papadimoulis:
    <FONT color=#0000ff>function</FONT> isNegative( n ) {
      <FONT color=#0000ff>return</FONT> n != 0 && n / <FONT color=#000080>Math</FONT>.<FONT color=#0000ff>abs</FONT>( n ) == -1
    }

    I haven't seen the most obvious:

    <FONT color=#0000ff>function</FONT> isNegative( n ) {
      <FONT color=#0000ff>return</FONT> <FONT color=#000080>Math</FONT>.signum( n ) == -1
    }

    Scratch that... [:S]

  • (cs) in reply to foxyshadis
    foxyshadis:

    Rank Amateur:

    Is there anything keeping n from being a float or double? If so, think we got a great heisenbug in here:

    n / Math.abs( n ) = -1.00000000000001.

     


    Thank god someone else here understands floating point arithmetic beyond an 8th-grade algebra level. (VBScript, being the 'friendly' half the MS scripting language family, would fix up the miss. JScript has pretentions to being IEEE-754 compliant.) In fact, I can't remember offhand, but JScript/ECMAScript might promote int->float for division, and then you've got a real mess on your hands.



    I'm not sure if JScript doesn't have some additional features above and beyond what JavaScript/ECMAScript has, but in the latter all numbers are of type "number", and are floating point.  So JavaScript doesn't promote int to float, because it doesn't have any ints to begin with.

    Since IEEE floating point has a sign bit, there's no way that n / Math.abs(n) can end up with a rounding error.  I think it even does the right thing with infinities (haven't tested it).  It doesn't detect -0.0 as negative, but you could argue that that's a feature.  The interesting cases, to me, are:

    If only the code had been written "!= 1" instead of "== -1", then NaN or a string value such as "abc" that would be converted to NaN would be detected as a negative number.

    If only the code was written in Java or C# or some language that has fixed-size two's-complement integers, and doesn't automatically promote them to bignums like Smalltalk and python and ruby, then -2^31 (or whatever the value of INT_MIN is) would NOT be detected to be negative.

    (Well shut my mouth!  I just tried it, and Math.Abs(int.MinValue) throws System.OverflowException in C#.  Whereas Math.abs(Integer.MIN_VALUE) just returns Integer.MIN_VALUE in Java.  Take that, you MSFT bashers!)

Leave a comment on “WTF Simplicity”

Log In or post as a guest

Replying to comment #:

« Return to Article