- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
TRWTF is "alphaBet" in camelCase, yes?
Admin
That is how you do currying in Java.
Also, he might be using a version of Java that does not allow switching on strings.
Admin
Trying too hard to use the knowledge on Reflection!!!
Admin
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.
Admin
Currying is perfectly doable without reflection, using either lambda expressions or an approximation thereof. I don't even think this is currying.
Admin
What's wrong with that? Imagine if he'd tried to use all of the Unicode characters?
Admin
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.
Admin
Does Javascript let you use UTF8 in method names? I hope it does.
Admin
Or you could use something more fun, the switch on hashCode(): http://thedailywtf.com/articles/How_Not_to_Parse_Command_Line_Arguments
Admin
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.
Admin
At least use a Map if you want avoid switches or some char-arethmetic to map it into an lookup-array... But not reflection...
Admin
Admin
You wouldn't use a switch statement, you'd store all the strings in a Map keyed off the input parameter.
Admin
yay, the quote markup worked.... now if only it could have seen the CRLF and stopped at it...
Admin
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.
Admin
Maybe I'm missing something blindingly obvious, but shouldn't this be as easy as
and then using
??
Admin
Please tell me the submitter heard a 'ba-dump-bump' right after the last line there...
Admin
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.
Admin
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.?
Admin
FTFY
Admin
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).
Admin
I hope they went to a mirror before applying palm to face
Admin
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.
Admin
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.
Admin
Remy - you missed reasona 0-9 why this was dumb.
Admin
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; } "
Admin
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);
Admin
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() { ... }
Admin
Well, betaBet is comming soon and in no time, full featured release Bet 1.0 will be available for all customers.
Admin
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).
Admin
This gets me thinking - is it even possible for a language to be statically typed and weakly typed?
Admin
Sure it's possible. C is statically typed and weakly typed.
Admin
can't tell if trolling or stupid... but anyway - C is most definitely STRONGLY typed.
Admin
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