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

    <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>;
    }



    Yeah, || would be more accurate to use in the place of != (or &&) there.  However, the developer also fails to check for the use of "spaces" in addition to it being blank (I knew a QA department that always tested to see if that was allowed).


    Huh?  Just because something makes you say WTF, don't refactor it to something semantically different.  || would not be "more accurate" than !=, or less accurate.  It would be completely different.  The code above tests for "if they gave one name, they have to give both", whereas || would test for "they always have to search by name; it's against the rules to search by customer number (or something)".

    I'll bet that the app this comes from has a search form where you can search by various criteria, and it's perfectly legal not to specify name AT ALL and just search by some other criterion, such as date or order number or what have you, but if you do give either a first or a last name, then you MUST give both.

    I'd change the error message, though, to something that made that more clear, such as "Both First and Last Name are required when searching by name."  (And then put soundex columns in the database so that Tie Wrecks would match Ty Rex.)

    Good point about spaces.  Change x == "" to isBlank(x) and keep the definition of that separate.
  • (cs) in reply to BogusDude
    Anonymous:
    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.



    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.


    But have you considered that, on a superpipelined RISC machine, using register windows to pass parameters, if the code fits in the instruction cache, then YOU ARE OPTIMIZING USER INPUT VALIDATION!  And arguing about optimization with no timing numbers anywhere in sight, and even if there were any it would be a microbenchmark, and what possible difference can it make if it takes one or two milliseconds to validate user input?

    All your unpredicted branches and cache flushes and pipeline stalls are saying is "I'm a bigger geek than you", "No, I'm a bigger geek."  Code bums!  That's what you are!  Code bums!
  • filjoe (unregistered) in reply to dubwai
    dubwai:
    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.



    Nope. It's just that you don't understand the other person's English. Or, perhaps you really have some funky English. C'mon what does "You must both first name and last name or neither." mean?
  • Anon (unregistered) in reply to dubwai
    Katja Bergman:
    Yeah, well. In normal English language you won't use the word XOR to make clear you only mean one option.

    That is what 'either...or' is for.

  • Björn (unregistered) in reply to Charles Nadolski

    Doesn't anyone here actually test the snippets?

    this.txtLastName.Text = "";
    this.txtFirstName.Text = "";
    bool b = (this.txtLastName.Text=="")!=(this.txtFirstName.Text=="");
    Console.WriteLine(b) //false
    
    this.txtLastName.Text = "afwfe";
    this.txtFirstName.Text = "";
    bool b = (this.txtLastName.Text=="")!=(this.txtFirstName.Text=="");
    Console.WriteLine(b) //true
    
    this.txtLastName.Text = "";
    this.txtFirstName.Text = "asdf";
    bool b = (this.txtLastName.Text=="")!=(this.txtFirstName.Text=="");
    Console.WriteLine(b) //true
    
    this.txtLastName.Text = "wdfc";
    this.txtFirstName.Text = "wefwe";
    bool b = (this.txtLastName.Text=="")!=(this.txtFirstName.Text=="");
    Console.WriteLine(b) //false
    

    EXACTLY as described here (XOR)...

Leave a comment on “WTF Simplicity”

Log In or post as a guest

Replying to comment #:

« Return to Article