• np (unregistered)

    So they were using the code like: StringUtils.string30("frist") ?

  • (cs)

    TRWTF is not using "static final" for the number constants

  • Nobulate (unregistered)

    For once I am honestly speechless!

  • (cs)

    This demonstrates what a great metric lines-of-code is. Never mind the quality, feel the length!

  • TopTension (unregistered)

    At least they are on the safe side, should the value of any of the numbers between 0 and 50 ever change!

  • ben (unregistered)

    Nice. One question: We have seen number-constants many times; under what circumstances do they make sense? (I dont mean e.g. E and PI but ONE, TWO, ...) I know the first answer everyone thinks of is "if they ever change the value of 1 and 2". But my question is meant semi-seriously. (for code-generation?)

  • (cs) in reply to ben
    ben:
    Nice. One question: We have seen number-constants many times; under what circumstances do they make sense? (I dont mean e.g. E and PI but ONE, TWO, ...) I know the first answer everyone thinks of is "if they ever change the value of 1 and 2". But my question is meant semi-seriously. (for code-generation?)
    They're sometimes handy for non-trivial types (e.g. big integers).
  • Pista (unregistered)

    The Highly Paid Whatevers (TM) didn't screw up. They were obfuscating the code.

  • (cs) in reply to ben
    ben:
    Nice. One question: We have seen number-constants many times; under what circumstances do they make sense? (I dont mean e.g. E and PI but ONE, TWO, ...) I know the first answer everyone thinks of is "if they ever change the value of 1 and 2". But my question is meant semi-seriously. (for code-generation?)
    It's actually a way to give lip service to a "no magic numbers" policy that mandates the use of named constants. Usually in such policies you are supposed to give meaningful names to constants such as SURNAME_COLUMN_WIDTH or SSN_BUFFER_LENGTH (or in extreme cases, NUMBER_OF_PI_IN_CIRCLE), possibly using several constants with the same value. Sometimes someone will be lazy and just use a constant named after the number they wanted. This completely defeats the purpose of the policy, it's just as bad as using the member directly.
  • Indifferent (unregistered) in reply to pjt33
    pjt33:
    ben:
    Nice. One question: We have seen number-constants many times; under what circumstances do they make sense? (I dont mean e.g. E and PI but ONE, TWO, ...) I know the first answer everyone thinks of is "if they ever change the value of 1 and 2". But my question is meant semi-seriously. (for code-generation?)
    They're sometimes handy for non-trivial types (e.g. big integers).

    Yes- if your big constant is MAX_PRODUCTION_RUNS=1000000000 But not ONE_BILLION=1000000000 surely?

  • Jeff Grigg (unregistered)

    I was on a project where TWO of those Highly Paid Consultants couldn't figure out why their "string copy" function, written in C, wasn't working. They were using an assignment statement (the "=" operator) to assign one function parameter to the other. The debugger seemed to be showing them that their destination parameter had the right string value. They just couldn't figure out why it kept "disappearing" when their "string copy" function returned. I gave them some hints, but they were not listening. Eventually, after a few hours, they figured it out. :-/

  • Kabi (unregistered) in reply to Jeff Grigg
    Jeff Grigg:
    I was on a project where TWO of those Highly Paid Consultants couldn't figure out why their "string copy" function, written in C, wasn't working. [...] Eventually, after a few hours, they figured it out. :-/
    Unfortunately, based on my limited experience, that means that they WERE pretty good for Highly Paid Consultants.
  • QJo (unregistered) in reply to Roby McAndrew
    Roby McAndrew:
    This demonstrates what a great metric lines-of-code is. Never mind the quality, feel the length!

    More like: feel the thickness.

  • np (unregistered) in reply to Kabi
    Kabi:
    Jeff Grigg:
    I was on a project where TWO of those Highly Paid Consultants couldn't figure out why their "string copy" function, written in C, wasn't working. [...] Eventually, after a few hours, they figured it out. :-/
    Unfortunately, based on my limited experience, that means that they WERE pretty good for Highly Paid Consultants.

    Or they used stackoverflow and posted their code, asked why it doesn't work and someone responded. A few hours of mucking around and then 2 minutes to get a response sounds about right.

  • faoileag (unregistered)

    Actually, I am a bit dissapointed. I half expected the leaked MtGox code to be todays wtf...

  • Anon (unregistered)

    This is really unbelievable. I mean i think its fake or exaggerated. How can someone use a TWENTYNINNE instead of the number.

  • Prime (unregistered) in reply to Indifferent
    Indifferent:
    pjt33:
    ben:
    Nice. One question: We have seen number-constants many times; under what circumstances do they make sense? (I dont mean e.g. E and PI but ONE, TWO, ...) I know the first answer everyone thinks of is "if they ever change the value of 1 and 2". But my question is meant semi-seriously. (for code-generation?)
    They're sometimes handy for non-trivial types (e.g. big integers).

    Yes- if your big constant is MAX_PRODUCTION_RUNS=1000000000 But not ONE_BILLION=1000000000 surely?

    There's good reasons for simd/vector types as well (especially on consoles where transferring data from float to vector registers is expensive, Xbox 360 for example) - I'm coming from a game dev background.

    Vector1(1.0f);

    will give you a 1D vector of value 1, but as the compiler may not understand the vector type it'll load it into a float register, then into the vector one which causes some complex stalls.

    Vector1::One();

    Returns a reference to a static thing that's already a vector. The difference in subtle, but in some loops on some platforms - it does have an effect.

  • faoileag (unregistered) in reply to Roby McAndrew
    Roby McAndrew:
    This demonstrates what a great metric lines-of-code is. Never mind the quality, feel the length!
    Lines-of-code is one of those pseudo-metrics that basically exist to make management happy.

    They are measuring something, they are keeping logs of their metrics, so they feel that they are actually managing something.

    Also, adding lines of code looks more productive than, say, deleting lines of code by refactoring. Especially to managers without a software development background.

    After all in most of manufactoring the number of units per worker is a good metric for productivity, so why should it be different for programmers?

  • faoileag (unregistered) in reply to Anon
    Anon:
    This is really unbelievable. I mean i think its fake or exaggerated. How can someone use a TWENTYNINNE instead of the number.
    I agree. No-one would use a TWENTYNINNE instead of 29. TWENTYNINE however...

    SCNR

  • faoileag (unregistered) in reply to Indifferent
    Indifferent:
    pjt33:
    They're sometimes handy for non-trivial types (e.g. big integers).
    Yes- if your big constant is MAX_PRODUCTION_RUNS=1000000000 But not ONE_BILLION=1000000000 surely?
    Actually, why not if ONE_BILLION is the best description for the magic number?

    I had a peek at some code today and it was littered with the magic number "100000000". Searching for that number revealed at least one occurrence in the code where 1000000000 is used.

    Question: typo (one zero to many) or intention?

    If HUNDRED_MILLION would have been used instead of 100000000 that question would not arise.

  • Jay (unregistered) in reply to ben
    ben:
    Nice. One question: We have seen number-constants many times; under what circumstances do they make sense? (I dont mean e.g. E and PI but ONE, TWO, ...) I know the first answer everyone thinks of is "if they ever change the value of 1 and 2". But my question is meant semi-seriously. (for code-generation?)
    Similar to what @Prime said, it can make your code run faster in certain environments. When you hit a literal in code it needs to copy that into memory as a temporary variable, do the calc and then clean up; so 1+2 needs to have two temporary variables created, but ONE+TWO doesn't as they're already in memory. Depends on how the language/compiler treats literals. (It's been many many many years since I did that!)
  • e1532234 (unregistered)

    This is very bad. They should have created 21 subclasses of a common superclass, each containing an object with common function name but different truncation functionality.

    Then they could instantiate a subclass object each time a truncation is needed and abandon it for garbage collection immediately afterwards. Using a factory, of course.

    It's not really object oriented if you do it otherwise.

  • faoileag (unregistered) in reply to e1532234
    e1532234:
    Using a factory, of course.

    It's not really object oriented if you do it otherwise.

    I like the idea...

    IStringTrimmerFactory factory = new StringTrimmerFactory();
    IStringTrimmer strTrimmer42 = factory.createStringTrimmer(FORTYTWO);
    String strW42Chars = strTrimmer42.trim('The quick brown fox jumped over the lazy dolor sit amet');
    // do something with strW42Chars...
    

    Could probably be improved with XML and regular expressions, but I'll leave that to someone else.

  • gehho (unregistered)
    public class StringUtils {
      public String stringPrefix(String in, int maxLen){ 
        if (in == null) {
           return null;
        }
        if (maxLen < 1) {
           return "";
        }
        return in.length() <= maxLen
                 ? in
                 : in.substring(0,maxLen);
      } 
    }

    Aaargh, you simply don't get it! Magic numbers are evil! Should have been:

    public class StringUtils {
      private object NULL = null;
      private int ZERO = 0;
      private int ONE = 1;
      private int TWO = 2; // might be needed later
      public String stringPrefix(String in, int maxLen){ 
        if (in == NULL) {
           return NULL;
        }
        if (maxLen < ONE) {
           return "";
        }
        return in.length() <= maxLen
                 ? in
                 : in.substring(ZERO, maxLen);
      } 
    }

    Aaah, much safer!

  • Trello (unregistered) in reply to Jay

    Even the most naive compiler would be at least as quick doing 1+2 as ONE+TWO - if not it's trivial to identify literals and produce code like ONE=1, TWO=2 and use those instead during parsing.

    Worse, in the code above the values are neither static (so copied across instances) nor final (single reference to let the compiler inline the lookup). In this code THREE + FOUR would entail at least two lookups at runtime.

    Captcha: consequat - the consequat of using pointless constants is confused code and a spot on TDWTF

  • William (unregistered)

    How do I get in to this Highly Paid Consultant racket? I apparently have the qualifications already, as in I can use a keyboard to type in words.

  • faoileag (unregistered) in reply to Trello
    Trello:
    in the code above the values are neither static (so copied across instances) nor final (single reference to let the compiler inline the lookup).
    I think it might be safe to assume that the consultant responsible for the code had hitherto no exposure to the idea behind "static".

    After all, the methods (string30(...) etc) are also not static, meaning that whoever wants to use them has to instantiate StringUtils.

  • gnasher729 (unregistered) in reply to Indifferent
    Indifferent:
    Yes- if your big constant is MAX_PRODUCTION_RUNS=1000000000 But not ONE_BILLION=1000000000 surely?
    For ONE_BILLION it starts making sense. There's a good chance of a fatal typo if you repeat that constant. Would be nice if underscore characters were allowed in numbers; 1_000_000_000 is much more likely to be correct than 10000000000.
  • (cs) in reply to gehho
    gehho:
    ...
      private int TWO = 2; // might be needed later
    ...

    Love this :D

  • Rupee Everet (unregistered) in reply to Prime
    Prime:
    Indifferent:
    pjt33:
    ben:
    Nice. One question: We have seen number-constants many times; under what circumstances do they make sense? (I dont mean e.g. E and PI but ONE, TWO, ...) I know the first answer everyone thinks of is "if they ever change the value of 1 and 2". But my question is meant semi-seriously. (for code-generation?)
    They're sometimes handy for non-trivial types (e.g. big integers).

    Yes- if your big constant is MAX_PRODUCTION_RUNS=1000000000 But not ONE_BILLION=1000000000 surely?

    There's good reasons for simd/vector types as well (especially on consoles where transferring data from float to vector registers is expensive, Xbox 360 for example) - I'm coming from a game dev background.

    Vector1(1.0f);

    will give you a 1D vector of value 1, but as the compiler may not understand the vector type it'll load it into a float register, then into the vector one which causes some complex stalls.

    Vector1::One();

    Returns a reference to a static thing that's already a vector. The difference in subtle, but in some loops on some platforms - it does have an effect.

    That's not a constant ... that's a method with intentional additional logic for optimisation that doesn't return an integral value.

    CAPTCHA: bene - you've bene on the hooch again haven't you?

  • Pock Suppet (unregistered) in reply to gnasher729
    gnasher729:
    Indifferent:
    Yes- if your big constant is MAX_PRODUCTION_RUNS=1000000000 But not ONE_BILLION=1000000000 surely?
    For ONE_BILLION it starts making sense. There's a good chance of a fatal typo if you repeat that constant. Would be nice if underscore characters were allowed in numbers; 1_000_000_000 is much more likely to be correct than 10000000000.
    No, it doesn't. There has to be a reason you're using 1 billion instead of 10 billion or 100 million; work that into your constant name.

    MAX_AIRSPEED_OF_AN_UNLADEN_SWALLOW_IN_MICROMETERS_PER_HALF_HOUR=1000000000

  • Balu (unregistered) in reply to Roby McAndrew
    Roby McAndrew:
    Never mind the quality, feel the length!
    For a change: That's what HE said!
  • (cs) in reply to ben
    ben:
    Nice. One question: We have seen number-constants many times; under what circumstances do they make sense? (I dont mean e.g. E and PI but ONE, TWO, ...) I know the first answer everyone thinks of is "if they ever change the value of 1 and 2". But my question is meant semi-seriously. (for code-generation?)

    I've implemented numerical code where the data objects satisfied the Ring properties.

    In that case I would always implement two constants, Foo.ZERO and Foo.ONE which contained static objects that represented the appropriate values for the multiplicative and additive identities, i.e. foo.times(Foo.ONE).equals(foo)

  • Balu (unregistered) in reply to Pock Suppet
    Pock Suppet:
    MAX_AIRSPEED_OF_AN_UNLADEN_SWALLOW_IN_MICROMETERS_PER_HALF_HOUR=1000000000
    What do you mean, an African or European Swallow?
  • Charles F. (unregistered)
    It's not like they could have written:
    Nope. They could not have written that function. In the unlikely event that they had decent unit tests, that function would not pass them. The authors of this wretched code obviously didn't realize that the second parameter of substring is exclusive.

    Take, for example, string30. If you pass in a String of length 0-29 characters, you get it returned. If you pass a String of length 31 or more characters, you get back the first 29 characters. If you pass a String of exactly 30 characters, you get that String back.

    The suggested replacement code is not bug-compatible.

  • QJo (unregistered) in reply to William
    William:
    How do I get in to this Highly Paid Consultant racket? I apparently have the qualifications already, as in I can use a keyboard to type in words.

    You need to be the CEO's nephew.

    Or to be married to the president's daughter.

  • QJo (unregistered) in reply to faoileag
    faoileag:
    Trello:
    in the code above the values are neither static (so copied across instances) nor final (single reference to let the compiler inline the lookup).
    I think it might be safe to assume that the consultant responsible for the code had hitherto no exposure to the idea behind "static".

    After all, the methods (string30(...) etc) are also not static, meaning that whoever wants to use them has to instantiate StringUtils.

    Probably an engineer of the old school, to whom "static" was what caused certain unpleasant noises to emerge from the loudspeaker of his wireless set, and therefore something to be avoided whenever possible.

  • jEDI (unregistered) in reply to Pock Suppet
    Pock Suppet:
    No, it doesn't. There has to be a reason you're using 1 billion instead of 10 billion or 100 million; work that into your constant name.

    MAX_AIRSPEED_OF_AN_UNLADEN_SWALLOW_IN_MICROMETERS_PER_HALF_HOUR=1000000000

    Sometimes the reason for using a billion is because the number you want is a billion?

    ONE_BILLION=1000000000 uk_gdp_in_billions = uk_gdp / ONE_BILLION

  • Meep (unregistered)

    "I'm not even going to go into what would happen if someone passed in a null pointer."

    I'm going to take a wild assed guess that it would throw an NPE.

    TRWTF is strongly typed languages that include values you have to check for at runtime.

  • (cs) in reply to gnasher729
    gnasher729:
    Indifferent:
    Yes- if your big constant is MAX_PRODUCTION_RUNS=1000000000 But not ONE_BILLION=1000000000 surely?
    For ONE_BILLION it starts making sense. There's a good chance of a fatal typo if you repeat that constant. Would be nice if underscore characters were allowed in numbers; 1_000_000_000 is much more likely to be correct than 10000000000.
    Ah, you want FORTRAN, then.

    The peculiarities of FORTRAN parsing mean that

          I=1000000000

    and

          I = 1 000 000 000

    are the same thing...

  • (cs) in reply to lscharen
    lscharen:
    In that case I would always implement two constants, Foo.ZERO and Foo.ONE which contained static objects that represented the appropriate values for the multiplicative and additive identities, i.e. foo.times(Foo.ONE).equals(foo)
    If they are the add/mult identities, then ffs call them that, not ZERO and ONE. Otherwise the poor schmuck who is maintaining your code will wonder why you used ONE in a situation, while in "foo.times(Foo.MULT_IDENT).equals(foo)" the "times" and "MULT" should be the clue that you are doing something sane.
  • (cs)

    It is all well and good to use every type of construction in the programming language, but I find it easier to just stick to the basics sometimes:

    public class StringUtils {
      public String stringPrefix(String in, int maxLen){ 
        if (in == null)
           return in;
        else if (maxLen < 1)
           return "";
        else if (in.length() <= maxLen)
           return in;
        else
           return in.substring(0, maxLen);
      } 
    }
    

    A little easier to read and I think the optimizer can do a little more with this because two lines are actually the same.

  • Herr Holle (unregistered) in reply to Balu
    Balu:
    Pock Suppet:
    MAX_AIRSPEED_OF_AN_UNLADEN_SWALLOW_IN_MICROMETERS_PER_HALF_HOUR=1000000000
    What do you mean, an African or European Swallow?
    This is defined by a subclass with the advantage of being able to represent swallows from other continents as well.
  • IDK Yaaaaaa! (unregistered) in reply to Balu
    Balu:
    Pock Suppet:
    MAX_AIRSPEED_OF_AN_UNLADEN_SWALLOW_IN_MICROMETERS_PER_HALF_HOUR=1000000000
    What do you mean, an African or European Swallow?

    segfault

  • (cs) in reply to faoileag
    faoileag:
    Roby McAndrew:
    This demonstrates what a great metric lines-of-code is. Never mind the quality, feel the length!
    Lines-of-code is one of those pseudo-metrics that basically exist to make management happy.

    They are measuring something, they are keeping logs of their metrics, so they feel that they are actually managing something.

    Also, adding lines of code looks more productive than, say, deleting lines of code by refactoring. Especially to managers without a software development background.

    After all in most of manufactoring the number of units per worker is a good metric for productivity, so why should it be different for programmers?

    About 20 years ago, Bill Gates was quoted comparing the use of lines of code to measure code with using weight to measure aircraft quality.

  • Jay (unregistered) in reply to Trello
    Trello:
    Even the most naive compiler would be at least as quick doing 1+2 as ONE+TWO - if not it's trivial to identify literals and produce code like ONE=1, TWO=2 and use those instead during parsing.
    You'd think so.

    And then there's interpreters.

  • (cs) in reply to jEDI
    jEDI:
    Pock Suppet:
    No, it doesn't. There has to be a reason you're using 1 billion instead of 10 billion or 100 million; work that into your constant name.

    MAX_AIRSPEED_OF_AN_UNLADEN_SWALLOW_IN_MICROMETERS_PER_HALF_HOUR=1000000000

    Sometimes the reason for using a billion is because the number you want is a billion?

    ONE_BILLION=1000000000 uk_gdp_in_billions = uk_gdp / ONE_BILLION

    Then you have to call it BILLON_CONVERSION_CONSTANT.
  • Balu (unregistered) in reply to Medinoc
    Medinoc:
    Then you have to call it BILLON_CONVERSION_CONSTANT.

    Brillant!

    Captcha: Validus - this comment must be right.

  • (cs) in reply to faoileag
    faoileag:
    Actually, I am a bit dissapointed. I half expected the leaked MtGox code to be today's wtf...
    It is still being anonymous-ized for presentation tomorrow.
  • JP (unregistered)
    For those who don't know Java, someString.substring(0,29) returns the characters at indices 0..28. This means that those functions will return the entire string for inputs <= the given length

    Seriously? If the input length is less than the substring end index, it throws java.lang.StringIndexOutOfBoundsException.

    For example:

    String s = "adfadsfadsfadfs"; System.out.println(s.substring(0, 55));

Leave a comment on “Screwing Up the Screw Up”

Log In or post as a guest

Replying to comment #:

« Return to Article