The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

The reason for redundant code

Last post 10-08-2008 3:34 AM by XIU. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 10-04-2008 9:50 AM

    The reason for redundant code

    It saves you from having to learn those pesky boolean operators... 

    if( ((input1 < '1') && (input1 > '9')) ||
    ((input2 < '1') && (input2 > '9')) )
    return INPUT_ERROR;

    input1 -= 48;
    input2 -= 48;

    if( ((input1 < 1) || (input1 > 9)) ||
    ((input2 < 1) || (input2 > 9)) )
    return INPUT_ERROR;

     

  • 10-04-2008 4:10 PM In reply to

    • Zecc
    • Top 75 Contributor
    • Joined on 06-12-2007
    • Posts 376

    Re: The reason for redundant code

    Hey! do you have something against code reuse?
    If mixed metaphors were illegal, I'd be having an indigestion.
  • 10-05-2008 5:18 AM In reply to

    • TGV
    • Top 500 Contributor
    • Joined on 10-09-2005
    • Posts 90

    Re: The reason for redundant code

    If you read carefully, you'll see that the code was not exactly reused. The condition in the first if-statement always fails.
  • 10-05-2008 8:22 AM In reply to

    Re: The reason for redundant code

    TGV:
    If you read carefully, you'll see that the code was not exactly reused. The condition in the first if-statement always fails.
    Not always.  What if it's double threaded and those two values change between the evaluation of whether they're less than 1 and greater than 9?

    SpectateSwamp: I can see you. You don't have to hide anymore. C'mon out and play!
  • 10-05-2008 10:50 AM In reply to

    Re: The reason for redundant code

    belgariontheking:
    What if it's double threaded and those two values change between the evaluation of whether they're less than 1 and greater than 9?
    A good compiler should optimize away the second memory read (or the complete comparison), i.e. you have to declare the variables volatile for that to happen. Or you can overload the operator.
  • 10-05-2008 12:08 PM In reply to

    Re: The reason for redundant code

    strcmp:
    you have to declare the variables volatile for that to happen
    How do you know they aren't declared volatile in that snippet?
    ╩юфют√ь ёЄЁрэшЎрь яюЁр эр яхэёш■.

    Visit #TDWTF @ SlashNET - the semi-official WTF IRC channel.
  • 10-05-2008 3:33 PM In reply to

    Re: The reason for redundant code

    Spectre:
    strcmp:
    you have to declare the variables volatile for that to happen
    How do you know they aren't declared volatile in that snippet?
    Easy:
    Spectre:
    they aren't declared volatile in that snippet
  • 10-06-2008 12:16 PM In reply to

    Re: The reason for redundant code

    strcmp:
    Spectre:
    strcmp:
    you have to declare the variables volatile for that to happen
    How do you know they aren't declared volatile in that snippet?
    Easy:
    Spectre:
    they aren't declared volatile in that snippet
    They aren't declared at all in the snippet.
    : IF COMPILE ?-GOTO COMPILE-HERE ; IMMEDIATE
    : THEN HERE SWAP ! ; IMMEDIATE
    : ELSE COMPILE GOTO COMPILE-HERE SWAP HERE SWAP ! ; IMMEDIATE
  • 10-06-2008 1:26 PM In reply to

    • AlpineR
    • Top 500 Contributor
    • Joined on 11-17-2005
    • Washington, DC
    • Posts 63

    Re: The reason for redundant code

    operator<( const InputType& i, const char& c ) { return rand() < int(c); }

    operator<( const InputType& i, const int& n ) { return int(i) < n; }

     

  • 10-08-2008 3:34 AM In reply to

    • XIU
    • Top 200 Contributor
    • Joined on 01-08-2007
    • Posts 139

    Re: The reason for redundant code

    dear sir, the rand() keeps changing, how can i fix this?
Page 1 of 1 (10 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems