• more randomer than you (unregistered) in reply to Really Great Cod3r
    Really Great Cod3r:
    I am a senior software desiger at a MAJOR corporation, and I require ALL my employees to code this way. It makes it easy to set up an offset later if we need to. The zeros are placeholders for the offset that will someday go there.

    Besides, it took mathematicians thousands of years to invent the number zero, which exists for precisely this function: to serve as a placeholder.

    TRWTF is that eveyone doesn't do this!

    surely a mroe appropriate placeholder would have been:

    rect.X1 = min(rect.X1, rect.X1 - rachelle);

  • AfluentHobo (unregistered)

    Recently while making a game for a mobile phone I changed "somevar - x" where x = 20.0 to "somevar - 20.0" and it made this little artifact kinda thing go away.

  • laughable (unregistered) in reply to dpm
    dpm:
    Jeff:
    The permanent and more-correct fix would be to remove the line altogether
    I beg to differ. The *correct* action, regardless of whatever change you want to make to the code, is to COMMENT the damned thing so that future maintainers will understand why you're doing something so unobvious.

    No, Comments here would clearly be inane. Where there is clearly no good reason why the code should be there in the first place, it should be removed instead of cluttering it any further with comments.

  • (cs) in reply to Calm Mint
    Calm Mint:
    Jeff:
    Looks a lot like there used to be constants in there instead of 0. Perhaps whatever reason they were adding and subtracting constants went away, so a developer swapped the constants out for zeroes.

    The permanent and more-correct fix would be to remove the line altogether, but perhaps the developer thought he'd be going right back in and changing the constants again?

    Is this code any LESS worthless? And yet somehow it seems more appropriate...

    #define X_OFFSET 0
    rect.X1 = min(rect.X1, rect.X1 - X_OFFSET);
    

    You're halfway there. You've shown the next person that X_OFFSET may occasionally change, and happens to be zero for now.

    To finish your fix, just take that special symbol and add it to the other lines, this kinda ties them together in a group to show that they're all part of the same idea:

     #define X_OFFSET 0
     #this.rect.X1 = Math.Min(this.rect.X1, this.rect.X1 - 0);
     #this.rect.X2 = Math.Max(this.rect.X2, this.rect.X2 + 0);
     #this.rect.Y1 = Math.Min(this.rect.Y1, this.rect.Y1 - 0);
     #this.rect.Y2 = Math.Max(this.rect.Y2, this.rect.Y2 + 0);
    

    Supposing that The Cast Theory is wrong as "this.rect.Y2" does not cast, whereas "this.rect.Y2 + 0" does a cast (eventually).

    With the assumption that 0 replaces a variable (and not a constant) what I see is:

    1. Either leave X1 (of a window?) where it is or push it to the left
    2. Either leave the width as it is or make (the window?) bigger
    3. Either leave the top where it is or push it up
    4. Either leave the height the same or make it bigger

    Can anybody tell me where this makes sense?

  • smartbutt (unregistered) in reply to Eevee
    Eevee:
    JoJo:
    And it's also incorrect as putting an apostrophe _before_ every letter doesn't solve all cases as Socrates' cat will tell you ;-)
    That should be "Socrates's". Only plurals ending in an 's' get a bare apostrophe at the end.

    Ha ha you're wrong.

    http://www.ucalgary.ca/uofc/eduweb/grammar/course/punctuation/3_7.htm

    Muphry's Law strikes again!

  • AboutToSuckLess (unregistered) in reply to AboutToFly

    You and Carolyn suck fist.

    Rachelle.

  • Where's Topp CodeHeir? (unregistered) in reply to more randomer than you

    T0pC0d3r? Was that you?

    Alex, let him post for crissake!!!

    doubleplusunc3nsorm3!!

  • Feminazis4Rachelle (unregistered) in reply to AboutToFly
    AboutToFly:
    RogerC:
    Playit Safe:
    'T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e!
    Thats just ridiculous. Never ever ever use an apostrophe and you will be right a much greater percentage of the time than if you put one before every letter.
    You left a necessary apostrophe out of that first sentence.
    And you left a necessary sense of humor out of rachelle.
  • (cs) in reply to Guest Dev
    Guest Dev:
    My.Reply = Math.Min('T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e! + '0); My.Reply = Math.Min('T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e! + 0'); My.Reply = Math.Max('T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e! + '0); My.Reply = Math.Max('T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e! - '0);

    FTFY

  • Giv3M3Tr0lls0rGiv3M3aB3tt3rSite3 (unregistered) in reply to more randomer than you
    more randomer than you:
    Really Great Cod3r:
    I am a senior software desiger at a MAJOR corporation, and I require ALL my employees to code this way. It makes it easy to set up an offset later if we need to. The zeros are placeholders for the offset that will someday go there.

    Besides, it took mathematicians thousands of years to invent the number zero, which exists for precisely this function: to serve as a placeholder.

    TRWTF is that eveyone doesn't do this!

    surely a mroe appropriate placeholder would have been:

    rect.X1 = min(rect.X1, rect.X1 - rachelle);

    TRWTF is that Alex is still banning T0pC0d3r.

  • space (unregistered)

    Can anyone say potential precision loss due to casting?

  • Jay (unregistered) in reply to sadwings
    sadwings:
    javabrain:
    oh, you mean: (x==null ? "" : x.toString())

    somehow ""+x seems simpler...

    I prefer self-explanatory to simpler.

    being clever < being clear

    I don't know what language this example is, but in Java

    int n=getInt();
    String s=""+n;
    

    gives the same results as

    int n=getInt();
    String s=String.valueOf(n);
    

    But in a quick test I did, the second method took 1/4 the time of the first method.

    Look at the generated bytecode and you will see that the first method creates a StringBuilder, converts n to a String, appends this String to the StringBuilder, and then converts the result to a String. The compiler is smart enough to optimize out appending a null string, but not to optimize out creating the StringBuilder in the first place. Oh, and it uses String.valueOf to do the int-to-string conversion, so I'm hard pressed to think of any situation where the first method would be superior.

    Of course the difference in time is a fraction of a millisecond ... if you only do it once in your program. But if it's inside a loop and is executed thousands or millions of times, this sort of inefficiency could add up. If you are the sort of programmer who routinely says, "Oh, what's the big deal, it's only a fraction of a millisecond", then you could have thousands of such inefficiencies in your code, and they add up.

    Of course there are plenty of times when I'd choose clarity and maintainability over efficiency, but it seems to me that the second example is clearer and more maintainable too.

  • (cs) in reply to Jay
    Jay:
    sadwings:
    javabrain:
    oh, you mean: (x==null ? "" : x.toString())

    somehow ""+x seems simpler...

    I prefer self-explanatory to simpler.

    being clever < being clear

    I don't know what language this example is, but in Java

    int n=getInt();
    String s=""+n;
    

    gives the same results as

    int n=getInt();
    String s=String.valueOf(n);
    

    Look at the generated bytecode and you will see that the first method creates a StringBuilder, converts n to a String, appends this String to the StringBuilder, and then converts the result to a String.

    That's why Java, and OOP in general, sucks.

  • Alucard (unregistered)

    He probably started off with +1 and -1 to compensate for the boundary cases. And then he/she simply changed to 1's to 0's when they discovered that it was not necessary.

  • tetha (unregistered) in reply to MM

    This is not correct if your integers wrap or throw UnderflowExceptions and rect.X1 is sufficiently small.

    Assume that the datatype of rect.X1 has the minimum value MINVAL, then the correct transformation is:

    if rect.X1 > MINVAL + X_OFFSET: rect.X1 -= X_OFFSET; else: rect.X1 = handle_underflow(rect.X1, X_OFFSET)

    handle_underflow is language-, platform- and implementationspecific.

    • If the integers just wrap on underflows, then the overall result will be rect.X1 = rect.X1, unless the values underflows enough to result in a new value below rect.X1, so you get something complicated which involves simulating potentially multiple underflows.

    • if under- and overflows are handled by setting the variable to an arbitrary platformspecific value, anything might happen.

    • If underflows are handled with exceptions, well, an exception will be thrown.

    The point is: The transformation is not that simple, unless you have unbounded integers.

  • Jamie (unregistered)

    I've written almost identical code before actually. This looks like it's setting up a rectangle "fuzz factor" - making a hotspot on a control or image a bit larger, but the extra border to be included has been reduced to zero. So originally all the zeroes would have been some larger value, but have been changed here to zero and noone's bothered to remove the now-redundant code. :)

  • (cs) in reply to ClaudeSuck.de
    ClaudeSuck.de:
    Jay:
    sadwings:
    javabrain:
    oh, you mean: (x==null ? "" : x.toString())

    somehow ""+x seems simpler...

    I prefer self-explanatory to simpler.

    being clever < being clear

    I don't know what language this example is, but in Java

    int n=getInt();
    String s=""+n;
    

    gives the same results as

    int n=getInt();
    String s=String.valueOf(n);
    

    Look at the generated bytecode and you will see that the first method creates a StringBuilder, converts n to a String, appends this String to the StringBuilder, and then converts the result to a String.

    That's why Java, and OOP in general, sucks.

    Please stop coding. We hate to maintain it
  • Wolf (unregistered) in reply to dpm

    You guys who all comment on something like this are just sad

  • Tom (unregistered) in reply to Sigh

    In case there's someone who doesn't know what this is about, see: [image]

  • Paulus (unregistered) in reply to Playit Safe
    Playit Safe:
    'T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e!

    Even a broken clock is right twice a day ...

  • JoJo (unregistered) in reply to Paulus
    Paulus:
    Playit Safe:
    'T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e!

    Even a broken clock is right twice a day ...

    Mine's not, it's one of those 24 hour ones...

  • Stephen (unregistered) in reply to dpm
    dpm:
    Jeff:
    The permanent and more-correct fix would be to remove the line altogether
    I beg to differ. The *correct* action, regardless of whatever change you want to make to the code, is to COMMENT the damned thing so that future maintainers will understand why you're doing something so unobvious.

    Agreed. This is just an example of code rot but it needs commenting. Not really a WTF at all to be honest.

    C'mon guys.. Some WTFs!

    Stephen

  • Lemming (unregistered) in reply to Playit Safe

    'I' 'c'o'u'l'd'n't' 'a'g'r'e'e' 'm'o'r'e'.

  • PB (unregistered) in reply to Wolf
    Wolf:
    You guys who all comment on something like this are just sad
    ...and you guys that comment on those guys that comment on something like that are just brillant!
  • Wyrd (unregistered)

    In my mind, I imagine all of this code having been copy/pasted from somewhere else and that, at an earlier time, there were numbers other than 0 there. The .Min and .Max would have been a sloppy, but functional way of ensuring that adding really made it bigger and subtracting really made it smaller (absolute value would be the not-sloppy method).

    Since we've already established (in my made-up theory of what happened here) that the programmer is not exactly churning out perfect code, it should not be surprising that, at a later time, when this coder decides the values for X1/2, Y1/2 don't need to change after all, he just changed the offsets to 0 rather than rewriting the line appropriately.

    If the compiler or interpreter has good optimization, this silliness won't affect the final code any, anyway. So then it's just us fellow humans that suffer from hard-to-understand code.


    It reminds me of this project for which I had a partner in one of my Computer Science classes. I remember realizing we were totally boned and that there was no way we were going to finish when I stared at this: for(j=1;j<=k;j++) { <one statement> } After a few minutes of squinting at the code, I saw that k was basically a program constant with a value of one, so this 'for' loop was guaranteed to always execute exactly one time. So this 'for' loop was completely meaningless. The poor guy had no idea what he was doing. My part of the project was working, more or less. His wasn't working and was probably never going to work. We had a third part of the project, the main part, still to go and that was in a language that neither of us were familiar with, and we only had about six hours to go.

    -- Furry cows moo and decompress.

  • Micke A. (unregistered)

    Sounds like Cargo cult programming http://en.wikipedia.org/wiki/Cargo_cult_programming

  • dave (unregistered) in reply to Ken
    Ken:
    REMOVING the semicolon at the end of the KdPrint line results in it compiling properly: KdPrint(("argm->p[3]: %I64x\n", argm->p[3])) ULONGLONG returnval = (argm->p[3] >> 5) & 0xFFFFFLL;

    If I swap the lines, it compiles WITH the semicolon. Note that the double parentheses on KdPrint are required as stated by msdn - either their compiler sucks at dealing with preprocessor macros or whoever wrote the macro in the first place was smoking something.

    Considering variadic macros only came into existence in C99 and MS doesn't implement C99, the double-paren trick is required.

  • Eulex (unregistered) in reply to Playit Safe

    Or incorrect all the time.

  • Ole (unregistered) in reply to Playit Safe

    Bob the Angry Flower has a few things to say about the apostrophe: http://www.angryflower.com/aposter.html

  • Ole (unregistered) in reply to Ole

    Oops, I hadn't noticed Toms earlier comment. Sorry.

  • John Hardin (unregistered) in reply to Playit Safe
    Playit Safe:
    'T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e!

    'D'o'n''t' 'f'o'r'g'e't' 'a'f't'e'r' 'e'v'e'r'y' 'l'e't't'e'r', 't'o'o' - 'j'u's't' 't'o' 'b'e' 's'a'f'e'.

  • andhapp (unregistered) in reply to dpm

    Well if you really want to make it easy for the next developer who comes along then make the method descriptive by naming the constants and variables to make the intent absolutely clear... read Robert Martin's Clean Code.

  • kont (unregistered) in reply to Kevin
    Kevin:
    Probably it's the easiest way to convert to INT!

    Not the first to say it (and I'm probably not the first to reply, but too lazy to read other responses right now)...:

    Isn't the min/max redundant if the purpose is to force a cast? Won't x+=0 ensure x is an int? (certainly x/=1 will....)

  • Gerard (unregistered)

    Looks to me like he was removing rounding errors. I assume that the original x's and y's had been the result of some kind of calculation, and he'd possibly found out that moving something away, and then back, didn't return it to exactly the same place as before. Or perhaps he didn't know how to do a Floor. I doubt it was quite as ill-conceived as it might appear, though it may well have been badly implemented.

  • believer (unregistered) in reply to dpm
    dpm:
    Jeff:
    The permanent and more-correct fix would be to remove the line altogether
    I beg to differ. The *correct* action, regardless of whatever change you want to make to the code, is to COMMENT the damned thing so that future maintainers will understand why you're doing something so unobvious.
    The more test-infected would insist on not to comment any test because it's self explanatory (otherwise the test is to complex and needs some more simpler tests). Remove it!

    (some punctuation, just in case i missed them ..,,,'''---"",,´´: :)

  • Dan (unregistered)

    My guess is the zeros used to be something (ie adding a margin/buffer to the bounding box). He/she probably removed the margin but kept the -/+ in-case they needed to be added back in. No biggie.

  • dave (unregistered) in reply to Sigh

    I quite agree, there are so many people that have no idea where and when to use apostrophes (or simply can't type). It's most irritating. Still one should bear in mind that whoever wrote the text may be writing English as a foreign language. (My Russian/Mandarin/French/Spanish/... isn't so strong, either, you know!)

  • fun stuff (unregistered) in reply to Playit Safe
    Playit Safe:
    'T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e!

    What I find interesting here it the inclusion of the apostrophe where it should be, complete with an extra one before it. Nice touch.

  • CW (unregistered) in reply to Playit Safe

    printf("%s'", "'R'i'g'h't' 'u'p' 'u'n't'i'l' 't'h'e' 'a'p'o's't'r'o'p'h'e' 'g'o'e's' 'a'f't'e'r' 'a'n' 's' 'a't' 't'h'e' 'e'n'd' 'o'f' 'a' 'w'o'r'd',' 's'o' 'i'n's't'e'a'd' 't'h'e'y' 'g'o' 'b'e'f'o'r'e' 'e'v'e'r'y' 'c'h'a'r'a'c't'e'r'.' 'A'n'd' 'a't' 't'h'e' 'e'n'd' 'o'f' 't'h'e' 's't'r'i'n'g'.");

  • mliu (unregistered)

    I think this implementation is reasonable. What probably happened is someone used a global replace on the source file, and didn't bother to check it over later.

Leave a comment on “Math.Min(x, x - 0) = ?”

Log In or post as a guest

Replying to comment #:

« Return to Article