• Quite (unregistered)

    TRWTF is "alphaBet" in camelCase, yes?

  • Alistair (unregistered)

    That is how you do currying in Java.

    Also, he might be using a version of Java that does not allow switching on strings.

  • Pavithra (unregistered)

    Trying too hard to use the knowledge on Reflection!!!

  • ray10k (unregistered) in reply to Alistair

    Maybe, but since from the looks of it he's only dealing with the first letter, he could pass it as a char or even a char cast to an int. For that matter, if the output is just one string that differs only a small amount, it might be easier to just build the string manually at runtime.

    In any case, this code just looks wrong, as if the original programmer decided to use a sledgehammer to drive a small nail through a piece of plywood.

  • Bert (unregistered) in reply to Alistair

    Currying is perfectly doable without reflection, using either lambda expressions or an approximation thereof. I don't even think this is currying.

  • snoofle (unregistered)

    What's wrong with that? Imagine if he'd tried to use all of the Unicode characters?

  • LCrawford (unregistered)

    TRWTF is that Java didn't support strings in a switch statement before Java 7, resulting in an ugly set of if ... then ... else if ... else if . That created habits used by the hapless reflector-writing dev in this WTF.

  • isthisunique (unregistered)

    Does Javascript let you use UTF8 in method names? I hope it does.

  • (nodebb) in reply to LCrawford

    Or you could use something more fun, the switch on hashCode(): http://thedailywtf.com/articles/How_Not_to_Parse_Command_Line_Arguments

  • poniponiponi (unregistered)

    I'm gonna need some convincing why - modulo performance concerns - this is so horrible if the only alternative is a 50+ line switch statement.

    No sense having all these fancy tools if you're never going to use them.

  • Falco20019 (unregistered)

    At least use a Map if you want avoid switches or some char-arethmetic to map it into an lookup-array... But not reflection...

  • v (unregistered)

    Also, he might be using a version of Java that does not allow switching on strings. But the statement of the dev he'd need to use a switch statements is still just as WTF-y as the current implementation...

  • guest (unregistered)

    You wouldn't use a switch statement, you'd store all the strings in a Map keyed off the input parameter.

  • v (unregistered)

    yay, the quote markup worked.... now if only it could have seen the CRLF and stopped at it...

  • Judy (unregistered)

    I would guess that they didn't even need a switch statement - if they can build the string for lookup using the parameter. The internals aren't shown, but if anything besides the one character changes in each lookup string that's an even bigger WTF.

  • ApoY2k (unregistered)

    Maybe I'm missing something blindingly obvious, but shouldn't this be as easy as

    getCustomerListFor(string letter)

    and then using

    getCustomerListFor(alphaBet)

    ??

  • Jerepp (unregistered)

    Please tell me the submitter heard a 'ba-dump-bump' right after the last line there...

  • EatenByAGrue (unregistered)

    And actually, you wouldn't hardcode your Map<char, string> either if you had any sense. No need to recompile that every time it changes when you can store it in a configuration file or database somewhere instead.

  • Aaron (unregistered)

    How much you want to bet that the method just ran a query anyway, so the only difference in the methods was like 'A%' like 'B%' like 'C%' etc.?

  • _that_guy_ (unregistered) in reply to EatenByAGrue

    you can store it in a XML configuration file

    FTFY

  • isthisunique (unregistered)

    Maybe I'm missing something blindingly obvious, but shouldn't this be as easy as

    Exactly. Your alphabet might come from an Intl library if you're good, can find one and then the whole thing ports like a dream. It's classic inability to roll syndrome but unrolling up a scope/context into methods.

    The particular usage here is clearly based on a failure to understand parameters, recursion, constants, literal arrays, loops, etc. It's the sort of person who probably passed tests more on account of memorisation or cheating than understanding. It's possibly otherwise an attempt to be clever to make more terse yet human readable methods.

    Sometimes an origin in scripting languages can pervert people towards using this pattern appropriately because in scripting languages it is normal to be able to compose object and method names at run time. Dynamic languages basically are so because they tend to do and allow more in runtime as standard. Things like this are actually normal in more dynamic languages and you don't usually need to pull in reflection just as much (although it is the standard for OO).

    The ultimate result however will be several times more code usage. Even if you wanted functions like this for convenience, you would want to be be generating them from an array anyway rather than hard coding them all in. How do you get the alpabet now? Use reflection on the class and grep for the prefix method name?

    In rare cases, you can want to unroll methods is for performance but it's hard to keep DRY that way, you need precompilation normally. It's actually not that difficult but most languages don't provide any direct support for it. However if you've ever built an SQL string or anything like that, you're basically doing it there. A funny WTF would be someone using one scripting language to generate another such as PHP to generate all their Java. The intention is with that is where you have a parent function calling many other complex processing functions, to create several variants of the parent and called functions that remove conditional statements based on an initial set of conditions. Some compilers and systems can probably do that automatically to some extent it's basically one possible but harder to do automatically optimisation (especially in more dynamic language where more is determinate in runtime than at compiletime), but it's rarely really needed to go as far as. As always, it can also harm the effectiveness of the cache since you basically end up with more code in play (trading CPU cycles for amount of instruction code).

  • PITA (unregistered)

    I hope they went to a mirror before applying palm to face

  • Herby (unregistered)

    Of course, the aversion to switch statements might be that they add CONSIDERABLY to the Cyclomatic Complexity, and having that much complexity was not a "good thing".

    Of course, it could be worse. The then and else clauses of an if statement could be the same.

  • foxyshadis (unregistered) in reply to ray10k

    The A, B, C, D, etc sounds like anonymization, I wouldn't be surprised if the actual method names were getCustomerListForInitrode(), getCustomerListForContoso(), getCustomerListForWayneEnterprises(), getCustomerListForUmbrellaCorp(), etc, each of which just added a hard-coded LIKE "%Initrode%" or WHERE customerID = 2674 to the query. The fact that other dev thinks he needs a switch and a new function for each is TRWTF.

  • Ron Fox (google)

    Remy - you missed reasona 0-9 why this was dumb.

  • Chris Hennick (google)

    I would've answered, "Explain to me again why you need a switch statement to do

    PreparedQuery selectCustomerListForInitial = someDbEngineConnection.prepareQuery("select lastname, firstname, whatever from customer where firstname >= ? and firstname < ?");

    and

    ArrayList<Customer> getCustomerListForInitial(Character initial) throws SomeDbEngineException { SomeDbEngineResultSet results = selectCustomerListForInitial.execute(Character.toUpperCase(initial)); ArrayList<Customer> customerArrayList = new ArrayList<>(); for (SomeDbEngineRow row : results.rowIterable()) { /* ... */ } return customerArrayList; } "

  • Rusty Nail (unregistered)

    I'm sure all the Methods contain the same C&P snippet of code:

    String startingWith = Thread.currentThread().getStackTrace()[1].getMethodName().replaceFirst("getCustomerListFor", ""); return arrayListOfCustomers(startingWith);

  • (nodebb) in reply to isthisunique

    Does Javascript let you use UTF8 in method names?

    Yes, because method names are just the names of keys in a dictionary. The . syntax for accessing them is a bit more restricted though; those names have to start with a unicode letter (or one of a small set of symbols, notably _) and subsequent characters can be from that set plus (I think) any unicode digit. If you want something exotic, you have to use the other syntax: abc["•∞¶€"] = function() { ... }

  • Belzebub (unregistered) in reply to Quite

    Well, betaBet is comming soon and in no time, full featured release Bet 1.0 will be available for all customers.

  • Belzebub (unregistered)

    to Remy: I think you're confusing two independent things - Statically typed vs dynamicaly typed languages - and - Weakly typed vs. strongly typed languages.

    You can have dynamicaly typed and strongly typed languages (e.g. python).

  • Jack (unregistered) in reply to Belzebub
    I think you're confusing two independent things - Statically typed vs dynamicaly typed languages - and - Weakly typed vs. strongly typed languages.
    You can have dynamicaly typed and strongly typed languages (e.g. python).

    This gets me thinking - is it even possible for a language to be statically typed and weakly typed?

  • Ben (unregistered) in reply to Jack

    Sure it's possible. C is statically typed and weakly typed.

  • Belzebub (unregistered) in reply to Ben

    can't tell if trolling or stupid... but anyway - C is most definitely STRONGLY typed.

  • Luiz Monad (unregistered)

    C is not strongly typed. C Is weakly typed, go learn Typing before. http://blog.steveklabnik.com/posts/2010-07-17-what-to-know-before-debating-type-systems

Leave a comment on “Take a Moment to Reflect”

Log In or post as a guest

Replying to comment #:

« Return to Article