• Robert (unregistered) in reply to Matt R
    Matt R:
    Markp:
    Moitah:
    i'd say the guy wanted to keep a trace of each time he had to increase the reducing value.

    Exactly! Who needs CVS?

    Anyway, there's nothing wrong with writing out what should be a constant in an expression to make it's semantics clear. When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    So from reading the expression, it's pretty obvious what the semantics of that decrement are.

    Or gasp you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    There are those of us who believe code should be self-documenting. I tend to favor Markp's approach.

  • Kuli (unregistered) in reply to Kuli
    Or he should have written (44>>1)+1.

    Aarrgh! I mean (44<<1)+1, of course.

    Captcha: Still damnum.

  • loco (unregistered) in reply to Matt R

    But then you end up with head scratchers like

    final long ms = 5000; /* 5 minutes worth of milliseconds */

  • Maarten (unregistered) in reply to Matt R
    Matt R:
    Markp:
    Moitah:
    i'd say the guy wanted to keep a trace of each time he had to increase the reducing value.

    Exactly! Who needs CVS?

    Anyway, there's nothing wrong with writing out what should be a constant in an expression to make it's semantics clear. When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    So from reading the expression, it's pretty obvious what the semantics of that decrement are.

    Or gasp you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    End of course, half a year later someone decides that what is really needed is 10 minutes, instead of just 5. The code will then read:

    final long ms = 600000; /* 5 minutes worth of milliseconds */

    And a new WTF is born.

  • (cs) in reply to Matt R
    Matt R:
    Or *gasp* you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    But the line with the comment is longer:

    final long ms = 5 * 60 * 1000; final long ms = 300000; /* 5 minutes worth of milliseconds */

    Talk about source code bloat.

  • Sean (unregistered) in reply to Matt R
    Matt R:
    Markp:
    Moitah:
    i'd say the guy wanted to keep a trace of each time he had to increase the reducing value.

    Exactly! Who needs CVS?

    Anyway, there's nothing wrong with writing out what should be a constant in an expression to make it's semantics clear. When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    So from reading the expression, it's pretty obvious what the semantics of that decrement are.

    Or gasp you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    I'd have to agree with MarkP here, the line:

    final long ms = 5 * 60 * 1000;

    is far more obvious to see that the original code writer did it right as opposed to:

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    At first glance it's hard to tell the difference between these two lines:

    final long ms = 300000; /* 5 minutes worth of milliseconds / final long ms = 3000000; / 5 minutes worth of milliseconds */

    Note that the second one has the extra zero (so it's really 50 minutes worth of millisecs), but with the comment there you (as the developer trying to understand why the code isn't working) might easily skim past that, without taking the time to do the math yourself AND then count to make sure there are the same number of zeros.

  • frustrati (unregistered) in reply to SuperousOxide
    SuperousOxide:
    APH:
    Actually, I would combine all these suggestions:

    final long TIME_IN_MILLISECONDS = 5 * 60 * 1000; // 5 minutes in milliseconds

    I know myself, and I would definitely omit or add an extra zero.

    Or you could go further

    final long TIME_IN_MILLISECONDS = 5 * SECONDS_IN_MINUTE * MILLISECONDS_IN_SECOND;

    You never know when someone will redefine the minute or millisecond.

    You clearly misunderstand the purpose of defining constants.

    final long FIVE_MINUTES = FIVE * SIXTY * ONE_THOUSAND;

  • hapless maintenance programmer (unregistered) in reply to Matt R
    Or *gasp* you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".
    final long ms = 300000; /* 5 minutes worth of milliseconds */

    oh. good idea.

    final long ms = 20000; /* 5 minutes worth of milliseconds */

    ... oops. forgot to update the comment.

    I usually just do this:

    // imported from somewhere:
    static final long second = 1000; // basic time unit: ms
    static final long minute = 60 * second;
    
    final long ms = 5 * minute // no comment needed
    
  • (cs) in reply to Mark
    Mark:
    You might choose death by herpes; but do a google search for "tree man" for a look at what life with herpes looks like.
    Perhaps we could perform a public service and get tree man and goatse together?
  • Sussman (unregistered) in reply to Dan

    Lisp isn't all about parenthesis, young wizard: (+ 40 1 1 1 1 1 1 1 1 1 1 10 1 (ascdiff "Z" "A") 1 1 1)

  • Hermit (unregistered) in reply to Matt R
    Matt R:
    Markp:
    Moitah:
    i'd say the guy wanted to keep a trace of each time he had to increase the reducing value.

    Exactly! Who needs CVS?

    Anyway, there's nothing wrong with writing out what should be a constant in an expression to make it's semantics clear. When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    So from reading the expression, it's pretty obvious what the semantics of that decrement are.

    Or gasp you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    Actually I am going to have to gasp agree with the former comment. Even with your commented method, if I want to modify the number, I have to do math in my head. With his example, I can see which set of numbers is seconds and which is minutes and then modify the appropriate one with less effort. And if you think his method is overly verbose, you're just being a picky slashdot fanboy

  • (cs)
    const int MILLISECONDS = 1000;
    const int SECONDS = 60;
    const int MINUTES = 5;
    ...
    int milliseconds = MINUTES * SECONDS * MILLISECONDS;

    Why are you guys using longs? Can't Java fit 30000 into an integer?

  • Loren Pechtel (unregistered)

    I'm going to guess it's processed output recycled. I think the original didn't contain all those crazy +1's, but rather was a legitimate case of building up the value by math on constants as many on here have mentioned.

    However, the constants were already evaluated to make the offending line of code.

    As for the broken down thing of

    Time = 5 * Seconds_In_Minute * Milliseconds_In_Second

    I've done things very similar to this. I wouldn't do it ONLY for this line of code but if you're going to use those constants elsewhere anyway, why not? Sure, they can never change but they're still magic values. By using constants instead you make it clearer what the code means.

  • Anon (unregistered) in reply to Robert
    Robert:
    There are those of us who believe code should be self-documenting. I tend to favor Markp's approach.

    90% of the people who think code should be self-documenting, can't actually write self-documenting code.

  • HonoredMule (unregistered) in reply to Matt R
    Matt R:
    Markp:
    Moitah:
    i'd say the guy wanted to keep a trace of each time he had to increase the reducing value.

    Exactly! Who needs CVS?

    Anyway, there's nothing wrong with writing out what should be a constant in an expression to make it's semantics clear. When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    So from reading the expression, it's pretty obvious what the semantics of that decrement are.

    Or gasp you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    Keeping the value broken into unit-relative factors offers a fair bit of ease in adjusting it to other meaningful values while impacting performance so little that only a kernel programmer would (or should) care. Also, it's cleaner and more concise than commenting what could be completely obvious anyway.

  • Sigh (unregistered) in reply to Matt R

    Yeah, you can spend an extra minute pulling up a calculator and copy pasting the result (number dyslexic here), and then commenting it.

    Or you can just do the multiplication which eliminates possibility of error and saves time, and is self documenting.

    And a hell of a lot easier to modify in the future.

    Captcha: Damn um

  • bobbyG (unregistered)

    The code gets a different answer in EBCDIC (on a mainframe).

  • PhysicsPhil (unregistered) in reply to wm.annis
    wm.annis:
    Dan:
    LISP would have been better suited to this problem. change patno -= ((((((((((((((((40+1)+1)+1)+1)+1)+1)+1)+1)+1)+1)+10)+1)+('Z'-'A'))+1)+1)+1);

    to

    (+(+(+(+(+(+(+(+(+(+(+(+(+(+(+(+(+ 40 1)1)1)1)1)1)1)1)1)1)1)10)1)(- Z A))1)1)1)

    Honestly.

    (1+ (1+ (1+ (+
                 (1+ (1+ 
                      (+
                       (1+ 
                        (1+ 
                         (1+ 
                          (1+ 
                           (1+
                            (1+ 
                             (1+ 
                              (1+ 
                               (+ 40 1))))))))))))
                 10
                 (apply #'- (mapcar #'char-int (list #\Z #\A)))))))
    
    But you forgot the set/setq/let expression to actually set patno
  • Anon Fred (unregistered) in reply to Markp
    Markp:
    When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    #define SECONDS_PER_DAY 86400 #define FIVE_DAYS 5 * SECONDS_PER_DAY

  • Sgeo (unregistered) in reply to Markp

    In LSL, you might do

    integer ms = 300000;

    instead of

    integer ms = 5 * 60 * 1000;

    for speed reasons... yep, LSL, though it is a compiled language, does NOT optimize that..

  • rleibman (unregistered) in reply to Matt R
    Matt R:
    Markp:
    Moitah:
    i'd say the guy wanted to keep a trace of each time he had to increase the reducing value.

    Exactly! Who needs CVS?

    Anyway, there's nothing wrong with writing out what should be a constant in an expression to make it's semantics clear. When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    So from reading the expression, it's pretty obvious what the semantics of that decrement are.

    Or gasp you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    Not as clear, plus if you change the time you also have to change the comment, so you could introduce a subtle bug by having the documentation and the code be incompatible. I like the original better, the name of the variable tells you that it is in milliseconds, so the calculation tells you everything you need. If the units in the variable are not clear from the name (or usage) you can always do:

    final long myTime = 5 * 60 * 1000; //in milliseconds

    The compiler is only going to calculate it once anyway, and may even optimize it.

  • Jimmy Jones (unregistered) in reply to Matt R
    Matt R:

    "final long ms = 5 * 60 * 1000;"

    Or gasp you could use a comment!

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    The first version is easier to understand/edit. eg. What if you wanted to change it to six minutes?

  • Michael (unregistered)

    The best part of this is the " 'Z' - 'A' //86" Part. I mean - that HAS to be intentional obfuscation

  • CynicalTyler (unregistered) in reply to Matt R
    Matt R:
    Or *gasp* you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    Why have a comment? Use descriptive variable names.

    final long fiveMinutesInMillisecondsEqualsThreeHundredThousand = 300000;

  • (cs) in reply to FredSaw
    FredSaw:
    const int MILLISECONDS = 1000;
    const int SECONDS = 60;
    const int MINUTES = 5;
    ...
    int milliseconds = MINUTES * SECONDS * MILLISECONDS;
    Why are you guys using longs? Can't Java fit 30000 into an integer?

    I'm trying to figure out if you wrote "30000" instead of "300000" just to add irony and prove my point.

    As for the long vs. int, my basic rationale for that was that System.currentTimeMillis() returns a long. But there's another reason: As far as I understand it, the "+" operation in Java doesn't promote two ints to longs before doing the operation.

    http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#170983

    So if you had something that was fairly likely to be in the upward range of an integer's capacity (milliseconds since the epoch obviously qualifies, as does any time in ms in the order of magnitude of a month), and wanted to add another similar value to it, you'd have integer overflow:

    final static int MINUTE = 60 * 1000;
    final static int WEEK = 7 * 24 * 60 * MINUTE;
    
    final long interval = 4*WEEK + 27*MINUTE;
    System.out.println(interval); //-1874147296; hardly what we want
    

    Anyway, my post was sarcastic...I stand behind using expressions to self-document code, but I was obviously joking when I said I could understand the semantics behind the OP line of code. It's just

    System.out.println(new StringBuilder()
       .append("u").append("s").append("e")
       .append("l").append("e").append("s")
       .append("s")
       .append((char)((('A' - 'Z') + 25) + 32))
       .append("d").append("r").append("i")
       .append("v").append("e").append("l")).toString();
    
  • bobbyG (unregistered)

    BigDecimal baby!

  • bobbyG (unregistered)

    BigDecimal patno = 40; patno = patno.add(1); patno = patno.add(1); patno = patno.add(1); patno = patno.add(1); patno = patno.add(1); patno = patno.add(1); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(10)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal('Z' - 'A')); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1));

  • bobbyG (unregistered)

    BigDecimal patno = new BigDecimal(40); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(10)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal('Z' - 'A')); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1)); patno = patno.add(new BigDecimal(1));

  • ollo (unregistered) in reply to Matt R
    Matt R:
    Markp:
    Anyway, there's nothing wrong with writing out what should be a constant in an expression to make it's semantics clear. When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    So from reading the expression, it's pretty obvious what the semantics of that decrement are.

    Or gasp you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    Yes, that's the correct approach, always write obfuscated code that will be hard/impossible to understand. Then add a comment. AFAIR there are some really good tutorials on TheDailyWTF.

  • rufio (unregistered) in reply to Matt R

    But why comment if the code itself can be self explanatory?

  • Bert (unregistered) in reply to bobbyG

    Here are a thoughts...

    First I would like to complain that since no language was defined it is really hard to know what else could be happening. If the language was C++, then the declaration of the variable is VERY important.

    process angle: Programmer thought he/she(Paula) would experiment with "iterative design."

    C++ angle: A programmer that hates regular expressions builds their own pattern language using overloaded expressions.

    o A numeric value means any character for that field width. o A character value assumes a scale factor of 'A' and allows the next maxCharacterOffset characters to match via patno::operator+(char maxCharacterOffset = ('A'-'Z')).

    CVS/diff angle: I once had a diff tool that would not compare file contents if the file lengths were the same. This technique would ensure that the file size changed.

    MACRO angle: good possibility. Since you never know where macros will be used, you always include an extra set of parentheses. Create a macro that combines a few macros that combined a few macros, etc. and this is quite feasible.

    Code generator: The sky is the limit!

  • Anon. (unregistered) in reply to Anon Fred
    Anon Fred:
    Markp:
    When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    #define SECONDS_PER_DAY 86400 #define FIVE_DAYS 5 * SECONDS_PER_DAY

    // Just in case the number of seconds per day is changed. #define SECONDS_PER_DAY 86400 // Just in case they change the multiplication sign #define MULTIPLIED_BY * // Just in case five is not represented as '5' #define FIVE 5

    #define FIVE_DAY FIVE MULTIPLIED_BY SECONDS_PER_DAY

  • (cs) in reply to rufio
    rufio:
    But why comment if the code itself can be self explanatory?
    One programmer's self-explanation is another programmer's obfuscation.
  • (cs) in reply to Markp
    Markp:
    I'm trying to figure out if you wrote "30000" instead of "300000" just to add irony and prove my point.
    No, it's an honest typo. Which, I suppose, serves even better to prove your point than if I had done it on purpose. :) </drivel>
  • (cs) in reply to BLs
    BLs:
    Ok, the first theory is right out... 40+40+10-1 would have been much shorter. It's gotta be just a purely evil developer.

    patno-=0141+010

  • Steve (unregistered)

    This looks suspiciously like autogenerated code to me.

    I was particularly impressed with this little gem from some generated code:

    if ((((((((((((((((((((0)))))))))))))))))))) { //approximately 1500 lines }

  • wm.annis (unregistered) in reply to PhysicsPhil
    PhysicsPhil:
    But you forgot the set/setq/let expression to actually set patno

    Oy. Even when making fun of bad programming I still tend to a functional style. ;)

    It occurred to me later that for greater lisp obfuscation yet all those (1+ ...) calls could be replaced with (+ (*) ...).

  • Matt Briggs (unregistered) in reply to Matt R

    I disagree, even the stupidest of compilers will optimize the operation away, and if code is clear enough, comments become redundant. The 5-6 extra keystrokes are well worth the clarity and readability in this case

  • 34rl (unregistered) in reply to Matt R

    Ok, but then let say we want to have it be 10 minutes worth of milliseconds, the value gets changed, the comment remains, and it ends up being another wtf:

    final long ms = 600000; /* 5 minutes worth of milliseconds */

  • Max (unregistered) in reply to Matt R

    Or, gasp, you could use a macro.

    final long ms= minutes_to_milliseconds(5);

    Max

  • Tyler Szabo (unregistered)

    It looks like he was trying to convert A through Z to 1 through 26. The additions line up nicely with a shifted ascii table.

  • TopicSlayer (unregistered) in reply to SuperousOxide
    SuperousOxide:
    APH:
    Actually, I would combine all these suggestions:

    final long TIME_IN_MILLISECONDS = 5 * 60 * 1000; // 5 minutes in milliseconds

    I know myself, and I would definitely omit or add an extra zero.

    Or you could go further

    final long TIME_IN_MILLISECONDS = 5 * SECONDS_IN_MINUTE * MILLISECONDS_IN_SECOND;

    You never know when someone will redefine the minute or millisecond.

    How about:

    final long TIME_IN_MILS = DoItAllUnitConverter.convert(5, DoItAllUnitConverter.MINUTES, DoItAllUnitConverter.MILISECONDS);

    Or the more enterprisey:

    final UnitConverter minuteToMilisecondConverter = UnitConverterFactory.getConverter(UnitConverterFactory.TIME).setInputUnits(UnitConverterFactory.MINUTES).setOutputUnits(UnitConvertFactory.MILISECONDS);

    final long TIME_IN_MILS = minuteToMilisecondConverter.convert(5);

    Certainly the best solution!

  • TopicSlayer (unregistered) in reply to Max
    Max:
    Or, gasp, you could use a macro.

    final long ms= minutes_to_milliseconds(5);

    Max

    Not generic enough, how about:

    final long TIME_IN_MILS = DoItAllUnitConverter.convert(5, DoItAllUnitConverter.MINUTES, DoItAllUnitConverter.MILISECONDS);

    Or the more enterprisey:

    final UnitConverter minuteToMilisecondConverter = UnitConverterFactory.getConverter(UnitConverterFactory.TIME).setInputUnits(UnitConverterFactory.MINUTES).setOutputUnits(UnitConvertFactory.MILISECONDS);

    final long TIME_IN_MILS = minuteToMilisecondConverter.convert(5);

    Certainly the best solution!

  • TopicSlayer (unregistered)

    Grrrrrrr...my first post doesn't appear after several page refreshs. Then, I see a better post to respond to and think, "splendid, this is an even better post to respond to...I'm glad the forum didn't take my first post." Then I post it, both appear, and I look like a jack-ass.

    THANKS FORUM SOFTWARE... <3 --'-,-<@ :-*

  • Matt (unregistered) in reply to jk
    jk:

    and wasn't it the ancient Irish who decided the alphabet should be in alphabetical order?

    You just blew my mind man. By definition wouldn't the alphabet be in alphabetical order?

  • Mirko (unregistered)

    Sorry, AdBlock stays on: I disabled it for a few seconds and clicked a few links - pure distraction.

    I'd much rather paypal-Tip authors of interesting content that I read regularly.

  • grahamsw (unregistered) in reply to Matt R

    explicit code is better than a comment. Anything more complicated than this should be an explicit function (let minutesToMilliseconds minutes = minutes * 60 * 1000)

    This is actually an example in "Elements of Programming Style". Kernighan votes for explicit code. Let the compiler do the work.

    My theory for the code is that it reflects some underlying data - probably text layout: 4 lines of header, then a whole bunch of lines that kept changing, then A through Z, etc...

  • mh (unregistered) in reply to Anon Fred
    Anon Fred:
    #define SECONDS_PER_DAY 86400 #define FIVE_DAYS 5 * SECONDS_PER_DAY
    #define SECONDS_PER_DAY 86400 #define FIVE_DAYS (5 * SECONDS_PER_DAY)

    ;)

  • Gpa Hill (unregistered) in reply to Matt R
    Matt R:
    Markp:
    Moitah:
    i'd say the guy wanted to keep a trace of each time he had to increase the reducing value.

    Exactly! Who needs CVS?

    Anyway, there's nothing wrong with writing out what should be a constant in an expression to make it's semantics clear. When I want to specify 5 minutes in ms, I do this:

    final long ms = 5 * 60 * 1000;

    not this:

    final long ms = 300000;

    So from reading the expression, it's pretty obvious what the semantics of that decrement are.

    Or gasp you could use a comment! Then you can remove the assumption that the moron who will be reading/maintaining your code will see the "obvious".

    final long ms = 300000; /* 5 minutes worth of milliseconds */

    Or gasp, stop using comments to hide smells, and name the constant FIVE_MINUTES_IN_MS.

    Hill

  • IV (unregistered)

    I am on the fence as to whether this is a good line. On one hand, it uses hardcoded values. Lots of them. On the other, it looks like it is easier to make the appropriate change. I mean, I assume originally all of those were 1's, but someone had to go back and make one a 10. Can you imagine trying to figure out how changing a 1 to a 10 would work if it weren't broken down like this?

    captcha: vindico

Leave a comment on “A Dubious Honor”

Log In or post as a guest

Replying to comment #:

« Return to Article