• ZOMGWTF (unregistered)

    Or would that be a bitwise-OR to encode?

  • Marcan (unregistered)

    So if the app has more than 32 errors there is a FOURBILLIONTWOHUNDREDNINETYFOURMILLIONNINEHUNDREDSIXTYSEVENTHOUSANDTWOHUNDREDNINETYSIX?

    shudders

    Job security anyone?

  • nEUrOO (unregistered) in reply to ZOMGWTF

    In old times :D we used to do that in order to save space...

  • sobani (unregistered) in reply to nEUrOO

    Could you please explain to me how you save space by writing a number down, instead of using the number itself?

    At least if I understand correct, you use THOUSANDTWENTYFOUR instead of 1024 to save space, right?

  • (cs)
    <obligatory> But this is essential in case they ever change the value of one thousand twenty four or two thousand forty eight. </obligatory> --Rank
  • Josh (unregistered)

    From way back in high school computer science, they would teach us never to use "magic" numbers. That is, every value should be represented by a constant or a variable.

    Of course, the idea was that you were supposed to document the purpose of each value.

    But I can't help but vividly recall a homework assignment that involved some kind of calculations, and just about everyone's program had a constant named some variant of SQRT_3_OVER_2.

  • Jason (unregistered) in reply to sobani
    sobani:
    Could you please explain to me how you save space by writing a number down, instead of using the number itself?

    At least if I understand correct, you use THOUSANDTWENTYFOUR instead of 1024 to save space, right?

    I believe he was referring to returning a single interger consisting of a bitwise OR to determine which errors occurred.

  • (cs)

    ZOMGNINTENDOSIXTYFOOOOOOOOOOOOUUUURRRRRRR

  • Anonymouse (unregistered)

    All of that trouble and the original developer still spelled FORTY wrong. Why didn't Thomas P just do a find/replace in order to introduce the more sane constant names?

    waffles suck

  • O Thieu Choi (unregistered)

    Insufficiently evil, if you ask me. A properly insane coder would have used Roman numerals:

    private constant static int MMXLVIII = 2048;

  • (cs) in reply to Josh
    Josh:
    From way back in high school computer science, they would teach us never to use "magic" numbers. That is, every value should be represented by a constant or a variable.

    Of course, the idea was that you were supposed to document the purpose of each value.

    But I can't help but vividly recall a homework assignment that involved some kind of calculations, and just about everyone's program had a constant named some variant of SQRT_3_OVER_2.

    I would maintain that using a constant named SQRT_3_OVER_2 is indeed good practice rather than using 0.866024503 many places in the code. Using SQRT_3_OVER_2 does document the purpose of the number (albeit a low-level meaning) and has the side benefit of avoiding typographical errors such as inadvertently reversing digits.

  • Médinoc (unregistered)

    Reminds me of this: http://mindprod.com/jgloss/unmainotherpeople.html (see bottom of page)

  • el jaybird (unregistered) in reply to Josh
    Josh:
    But I can't help but vividly recall a homework assignment that involved some kind of calculations, and just about everyone's program had a constant named some variant of SQRT_3_OVER_2.

    Well, that might not be so terrible if the code was calculating a function that involved (root 3 over 2) a lot. I've forgotten all my university math (sigh) but I know that was a number that showed up a lot, along with pi and e.

  • phil (unregistered) in reply to Anonymouse

    Err.. no they didn't. They just used the English spelling, as opposed to the American English spelling.

    (I live in England. We speak English. There is no such thing as "British English" - that would be like "European French". It is the other variants which should be named differently.)

  • (cs) in reply to O Thieu Choi
    O Thieu Choi:
    Insufficiently evil, if you ask me. A properly insane coder would have used Roman numerals:

    private constant static int MMXLVIII = 2048;

    But we aren't doing Roman numeral operations in this example, we're doing binary operations. I'd suggest:

    private constant static int B1000000000 = 512; private constant static int B10000000000 = 1024; private constant static int B100000000000 = 2048;

  • bambuti (unregistered) in reply to phil
    phil:
    Err.. no they didn't. They just used the English spelling, as opposed to the American English spelling.

    Maybe they used your spelling, but it's certainly not the "English" spelling, nor indeed that of any English-speaking country in Britain or elsewhere. It's just plain wrong. Forty is forty.

  • Rune (unregistered) in reply to phil

    Not sure if you're referring to the fourty part. But even in English, it's still forty.

    Forty-Four.

    The 'u' gets dropped.

  • (cs)

    Once again I call shenanigans. If this isn't a joke, I demand the submitter to reveal more of the code in order to prove it's from a real program.

  • (cs) in reply to nEUrOO
    nEUrOO:
    In old times :D we used to do that in order to save space...
    6809 Assembler (Tandy Color Computer). Ah, those were fun times.
  • Trevel (unregistered) in reply to Licky Lindsay

    I don't know -- this is exactly what I'd expect to see from someone who had been told to replace numbers with constants but was never told why.

    And besides, if you use a descriptive ERROR_FILE_NOT_FOUND sort of constant, then you can't reuse it anywhere else -- it would be misleading! ONETHOUSANDTWENTYFOUR is very clear what it's for and where it should be used -- anywhere you want to type in 1024 but can't because some idiot says you can't put 'magic numbers' in the file.

  • (cs) in reply to phil
    phil:
    Err.. no they didn't. They just used the English spelling, as opposed to the American English spelling.

    (I live in England. We speak English. There is no such thing as "British English" - that would be like "European French". It is the other variants which should be named differently.)

    And you're a stupid troll, who obviosuly does not in live the UK but seems to think they spell everything with an extra "U".

  • (cs)

    At least error onethousandtwentyfour would never go unnoticed.

  • gur (unregistered)

    Ooof, Phil, you need to keep a low profile for a while after that howler...

    Captcha: ewww - indeed!!!

  • (cs) in reply to Josh
    Josh:
    But I can't help but vividly recall a homework assignment that involved some kind of calculations, and just about everyone's program had a constant named some variant of SQRT_3_OVER_2.

    THEORETICALLY this is actually not a WTF, except that you can't tell if they mean sqrt(3 / 2) or sqrt(3) / 2 (I'll assume the latter).

    Now if SQRT_3_OVER_2 is defined as 0.5773503 it's almost as valid as defining a PI. Almost. Because there IS a sqrt() function in any competent language and the computation only takes a few cycles, so why hard code it? The only reason you'd need to is if it was a bit more computationally hard, and ran several hundred times (or around there) a second in some time-critical code (ie a game rendering loop or something).

  • Anonymously Yours (unregistered) in reply to Licky Lindsay
    Licky Lindsay:
    Once again I call shenanigans. If this isn't a joke, I demand the submitter to reveal more of the code in order to prove it's from a real program.
    I have to agree. Doing it the way this programmer did is clearly anti-lazy (not to be confused with "making an effort"), which goes against the very nature of programming. Who would choose to type that much extra?
  • MikeCD (unregistered) in reply to el jaybird
    el jaybird:
    I've forgotten all my university math (sigh) but I know that was a number that showed up a lot, along with pi and e.
    Yep, it's probably best known as sin(60˚)
  • tsmith (unregistered)
    Errors were encoded into a single integer rather than using a Boolean for each error that occurred. A nice way to overcomplicate things, but fine.

    It's actually a common coding idiom in systems programming.

    By using constants, you'd expect to see something like this:
        private int error; // The current error code.
        // Error masks
        ...
        private static final int ERROR_INVALID_FILENAME = 1024;
        private static final int ERROR_INVALID_USERNAME = 2048;
        private static final int ERROR_INVALID_PASSWORD = 4096;
        ...
    

    Actually, I'd expect to see something like this:

        private int error; // The current error code.
        // Error masks
        ...
        private static final int ERROR_INVALID_FILENAME = (1 << 10);
        private static final int ERROR_INVALID_USERNAME = (1 << 11);
        private static final int ERROR_INVALID_PASSWORD = (1 << 12);
        ...
    
  • Chris (unregistered)

    At least they used constants for it, so any half decent Editor can rename it to a more useful name. If they used 1024 and you tried a Find & Replace, you'd run into problems when you get lines like:

    if( iMenuState == 231024 ){

    So bravo to the original programer! Well done on adapting your Hello World app into a real program! ;)

  • SomeCoder (unregistered) in reply to The MAZZTer
    The MAZZTer:
    Josh:
    But I can't help but vividly recall a homework assignment that involved some kind of calculations, and just about everyone's program had a constant named some variant of SQRT_3_OVER_2.

    THEORETICALLY this is actually not a WTF, except that you can't tell if they mean sqrt(3 / 2) or sqrt(3) / 2 (I'll assume the latter).

    Now if SQRT_3_OVER_2 is defined as 0.5773503 it's almost as valid as defining a PI. Almost. Because there IS a sqrt() function in any competent language and the computation only takes a few cycles, so why hard code it? The only reason you'd need to is if it was a bit more computationally hard, and ran several hundred times (or around there) a second in some time-critical code (ie a game rendering loop or something).

    Actually, power functions (and of course we all know that square root is a power function) are incredibly taxing for most systems to perform. Granted, we don't know what the original code was really supposed to do, but if speed was a factor, then you would want to avoid all built in power functions as much as possible (as you alluded to).

    This code looks like Java though, so I'm going to assume that speed isn't that important (I'm not saying that Java is slow, just that the kind of optimization you would get from not using power functions would most likely be less important in Java than say C++).

  • Tp (unregistered)

    At least he didn't use acronyms! OTTF + OTTF = TTFE

    captcha: scooter! How much is the FISH!

  • (cs) in reply to sibtrag
    sibtrag:
    O Thieu Choi:
    Insufficiently evil, if you ask me. A properly insane coder would have used Roman numerals:

    private constant static int MMXLVIII = 2048;

    But we aren't doing Roman numeral operations in this example, we're doing binary operations. I'd suggest:

    private constant static int B1000000000 = 512; private constant static int B10000000000 = 1024; private constant static int B100000000000 = 2048;

    Whooooosh ....

    I'd suggest:

    B001000000000 = 512; B010000000000 = 1024; B100000000000 = 2048;

    But that's just me being tidy. (Although it does sort of provide some measure of code-review friendliness.)

    I think you miss the point about Roman numerals. (In fact, I know you miss the point.) We're not dealing with floating-point arithmetic here. (O lord, that would be insane ... particularly since the Roman numeric system has no concept of zero. Perhaps we could map:

    I => 0 II => 1 ... XI => 10

    etc. Well, it worked for C arrays the other way around, didn't it?)

    In fact, replacing the supposed "magic numbers" with Roman numerals would be much, much more readable. Would you rather type FOURTHOUSANDANDNINETYSIX, or MMMMIVC? Easy to get your boss, the documenters, the testers, and the maintenance programmers on your side. And, even better, it enhances job security. Who the fuck knows what MMMMIVC represents?

    The tiny problem with this is that there is no common accepted Roman numeral system that deals with integers > 5000.

    Still, this is alright if you're working on a twelve-bit architecture.

    With a two-bit programmer.

    Off topic: Alex might want to examine http://www.wtfmagazine.co.uk/ for potential copyright violation.

  • Bored Bystander (unregistered) in reply to SomeCoder
    SomeCoder:
    The MAZZTer:
    Josh:
    But I can't help but vividly recall a homework assignment that involved some kind of calculations, and just about everyone's program had a constant named some variant of SQRT_3_OVER_2.

    THEORETICALLY this is actually not a WTF, except that you can't tell if they mean sqrt(3 / 2) or sqrt(3) / 2 (I'll assume the latter).

    Now if SQRT_3_OVER_2 is defined as 0.5773503 it's almost as valid as defining a PI. Almost. Because there IS a sqrt() function in any competent language and the computation only takes a few cycles, so why hard code it? The only reason you'd need to is if it was a bit more computationally hard, and ran several hundred times (or around there) a second in some time-critical code (ie a game rendering loop or something).

    Actually, power functions (and of course we all know that square root is a power function) are incredibly taxing for most systems to perform. Granted, we don't know what the original code was really supposed to do, but if speed was a factor, then you would want to avoid all built in power functions as much as possible (as you alluded to).

    This code looks like Java though, so I'm going to assume that speed isn't that important (I'm not saying that Java is slow, just that the kind of optimization you would get from not using power functions would most likely be less important in Java than say C++).

    I'd like to know why they didn't call it COS_30 or SIN_60 since that's probably where it came from.

  • (cs) in reply to phil
    phil:
    Err.. no they didn't. They just used the English spelling, as opposed to the American English spelling.

    (I live in England. We speak English. There is no such thing as "British English" - that would be like "European French". It is the other variants which should be named differently.)

    Ahh, now I see why the gov't introduced compulsory key skills classes...

  • Cloak (unregistered) in reply to O Thieu Choi
    O Thieu Choi:
    Insufficiently evil, if you ask me. A properly insane coder would have used Roman numerals:

    private constant static int MMXLVIII = 2048;

    Or even better use the Klingonian base 13 numbering system

  • Cloak (unregistered) in reply to O Thieu Choi
    O Thieu Choi:
    Insufficiently evil, if you ask me. A properly insane coder would have used Roman numerals:

    private constant static int MMXLVIII = 2048;

    Or even better use the Klingonian base 13 numbering system

  • (cs) in reply to SomeCoder
    SomeCoder:
    The MAZZTer:
    Josh:
    But I can't help but vividly recall a homework assignment that involved some kind of calculations, and just about everyone's program had a constant named some variant of SQRT_3_OVER_2.

    THEORETICALLY this is actually not a WTF, except that you can't tell if they mean sqrt(3 / 2) or sqrt(3) / 2 (I'll assume the latter).

    Now if SQRT_3_OVER_2 is defined as 0.5773503 it's almost as valid as defining a PI. Almost. Because there IS a sqrt() function in any competent language and the computation only takes a few cycles, so why hard code it? The only reason you'd need to is if it was a bit more computationally hard, and ran several hundred times (or around there) a second in some time-critical code (ie a game rendering loop or something).

    Actually, power functions (and of course we all know that square root is a power function) are incredibly taxing for most systems to perform. Granted, we don't know what the original code was really supposed to do, but if speed was a factor, then you would want to avoid all built in power functions as much as possible (as you alluded to).

    This code looks like Java though, so I'm going to assume that speed isn't that important (I'm not saying that Java is slow, just that the kind of optimization you would get from not using power functions would most likely be less important in Java than say C++).

    Ummm, unless you think the value of sqrt(3.0)/2.0 might change, you don't really need to recalculate it every time round the loop. Once at start-of-day would be enough.

    And in fact pretty much every compiler for pretty much every language will calculate it once at compile-time and then replace it by a constant throughout your code for you. So it makes a whole bunch of sense for both accuracy and efficiency and good design and code clarity and maintainability to have a const variable somewhere in your code statically initialised with the actual formula rather than a hard-coded constant.

  • db (unregistered) in reply to The MAZZTer
    The MaZZTer:
    Now if SQRT_3_OVER_2 is defined as 0.5773503 it's almost as valid as defining a PI. Almost. Because there IS a sqrt() function in any competent language and the computation only takes a few cycles, so why hard code it? The only reason you'd need to is if it was a bit more computationally hard, and ran several hundred times (or around there) a second in some time-critical code (ie a game rendering loop or something).

    Any competent optimizing compiler would evaluate sqrt(3)/2 or sqrt(3/2) into constants.

  • (cs) in reply to Cloak
    Cloak:
    O Thieu Choi:
    Insufficiently evil, if you ask me. A properly insane coder would have used Roman numerals:

    private constant static int MMXLVIII = 2048;

    Or even better use the Klingonian base 13 numbering system

    Such a great joke, it was worth telling twice!

  • (cs) in reply to Licky Lindsay
    Licky Lindsay:
    Once again I call shenanigans. If this isn't a joke, I demand the submitter to reveal more of the code in order to prove it's from a real program.
    Oh, I find it entirely believable. I got a program where 0, 1, 2, and 3 were rolled into constants called ZERO, ONE, TWO, and THREE (plus or minus some system Hungarian for good^H^H^H^Hbad measure). And then, you should see the number of lines in this code such as:

    counter = counter + ONE

    Seriously. I really wish I was making this up.

  • Cloak (unregistered) in reply to The MAZZTer
    The MAZZTer:
    Josh:
    But I can't help but vividly recall a homework assignment that involved some kind of calculations, and just about everyone's program had a constant named some variant of SQRT_3_OVER_2.

    THEORETICALLY this is actually not a WTF, except that you can't tell if they mean sqrt(3 / 2) or sqrt(3) / 2 (I'll assume the latter).

    Now if SQRT_3_OVER_2 is defined as 0.5773503 it's almost as valid as defining a PI. Almost. Because there IS a sqrt() function in any competent language and the computation only takes a few cycles, so why hard code it? The only reason you'd need to is if it was a bit more computationally hard, and ran several hundred times (or around there) a second in some time-critical code (ie a game rendering loop or something).

    SQRT_3_OVER_2: sqrt(3)/2 = 0,86602540378443864676372317075294

    SQRT_3_OVER_2: sqrt(3/2) = 0,61237243569579452454932101867647

  • (cs) in reply to The MAZZTer
    The MAZZTer:
    Now if SQRT_3_OVER_2 is defined as 0.5773503 it's almost as valid as defining a PI. Almost. Because there IS a sqrt() function in any competent language and the computation only takes a few cycles, so why hard code it? The only reason you'd need to is if it was a bit more computationally hard, and ran several hundred times (or around there) a second in some time-critical code (ie a game rendering loop or something).

    If it's used enough that it could be stored as a constant, why on earth would anyone calculate the same value on every iteration of a loop? Calculate it once and assign the value to a variable that can be re-used. The meaningless few clock cycles you lose by calculating it once are worth making your code readable.

  • Reversed Engineer (unregistered)

    I would expect this kind of thing from an application that had been de-compiled and had gone through one or more steps of refactoring to a more usable form.

    For example, the code might decompile like this:

    if(i2 == 1024) { s5 = s4; s4++; }

    Then in the first step, you might do something like this:

    public static final int ONETHOUSANDTWENTYFOUR = 1024;

    if(i2 == ONETHOUSANDTWENTYFOUR) { s5 = s4; s4++; }

    It's a very small baby step toward refactoring it. You know what was there before (1024) and it's fairly easy to rename the constant later, like so:

    if(errorCode == FILE_NOT_FOUND) { s5 = s4; s4++; }

    I wouldn't call it a WTF.

    CAPTCHA: ninjas (<hand signs> reverse engineer jutsu!)

  • (cs) in reply to Cloak
    Cloak:
    The MAZZTer:
    Josh:
    But I can't help but vividly recall a homework assignment that involved some kind of calculations, and just about everyone's program had a constant named some variant of SQRT_3_OVER_2.

    THEORETICALLY this is actually not a WTF, except that you can't tell if they mean sqrt(3 / 2) or sqrt(3) / 2 (I'll assume the latter).

    Now if SQRT_3_OVER_2 is defined as 0.5773503 it's almost as valid as defining a PI. Almost. Because there IS a sqrt() function in any competent language and the computation only takes a few cycles, so why hard code it? The only reason you'd need to is if it was a bit more computationally hard, and ran several hundred times (or around there) a second in some time-critical code (ie a game rendering loop or something).

    SQRT_3_OVER_2: sqrt(3)/2 = 0,86602540378443864676372317075294

    SQRT_3_OVER_2: sqrt(3/2) = 0,61237243569579452454932101867647

    Ooh, impressive. You're obviously not using one of the OMGWTF contest calculators, are you?

  • mudkip (unregistered) in reply to phil
    phil:
    Err.. no they didn't. They just used the English spelling, as opposed to the American English spelling.

    (I live in England. We speak English. There is no such thing as "British English" - that would be like "European French". It is the other variants which should be named differently.)

    Why bother treating your English differently? No English is better than any other.

  • (cs) in reply to SomeCoder
    SomeCoder:
    Actually, power functions (and of course we all know that square root is a power function) are incredibly taxing for most systems to perform. Granted, we don't know what the original code was really supposed to do, but if speed was a factor, then you would want to avoid all built in power functions as much as possible (as you alluded to).

    This code looks like Java though, so I'm going to assume that speed isn't that important (I'm not saying that Java is slow, just that the kind of optimization you would get from not using power functions would most likely be less important in Java than say C++).

    I was always of the impression that any decent math library calculates the square-root of an integer uses a table lookup method, not any sort of taxing algorithm (okay, it is certainly more taxing than bitwise operations, but it's about the same as division in terms of complexity).

  • TallGuy (unregistered) in reply to AssimilatedByBorg

    Heh, I'm working with a Fortran application that does the same. However, there is a good reason because Fortran preserves type across math operations, so 1/2 and 1.0/2.0 are different (though 1/2 = 1/2.0). Even knowing why, I hate seeing things like v = (val + ONE) * TWOTHIRDS, though.

  • TallGuy (unregistered) in reply to TallGuy
    TallGuy:
    Heh, I'm working with a Fortran application that does the same. However, there is a good reason because Fortran preserves type across math operations, so 1/2 and 1.0/2.0 are different (though 1/2 = 1/2.0). Even knowing why, I hate seeing things like v = (val + ONE) * TWOTHIRDS, though.

    re:

    AssimilatedByBorg:
    Oh, I find it entirely believable. I got a program where 0, 1, 2, and 3 were rolled into constants called ZERO, ONE, TWO, and THREE (plus or minus some system Hungarian for good^H^H^H^Hbad measure). And then, you should see the number of lines in this code such as:

    counter = counter + ONE

    Seriously. I really wish I was making this up.

  • M Cassidy (unregistered)

    The code was probably the result of reverse engineering, ie using Reflector or similar. And what else, would the reflection tool name the enumerations?

  • Velko (unregistered) in reply to AssimilatedByBorg
    AssimilatedByBorg:
    Oh, I find it entirely believable. I got a program where 0, 1, 2, and 3 were rolled into constants called ZERO, ONE, TWO, and THREE...
    Well, I have seen similar thing in database. There's table:

    create table ZERO_ONE(number INTEGER); insert into ZERO_ONE(number) values (0); insert into ZERO_ONE(number) values (1);

    Table contents were loaded into dropdown box to provide choice for obvious yes/no question.

    I replaced it with checkbox, of course.

  • Martini (unregistered)

    Oh dear.

    I hate to burst all your bubbles, but in C, C++, C# (and probably Java too) sqrt(3/2) is 1. sqrt((double)3/2) or sqrt(3/(double)2) or the float or decimal equivalents.. now that's another story.

Leave a comment on “Constant Extensibility”

Log In or post as a guest

Replying to comment #156095:

« Return to Article