Comment On Very Specific Generics

"I'm as much a fan of Java Generics as the next guy," writes Jim Bethancourt, "why bother with writing all that type-specific code for common collections (or - gasp - losing type safety) when one can simply go  HashMap<String, SomeObject>." [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: Very Specific Generics

2008-05-08 08:03 • by drx (unregistered)
This code is a precursor to the data structures used in the Matrix.

Re: Very Specific Generics

2008-05-08 08:07 • by Sam (unregistered)
Holy crap. I've never seen such... dystopia.

Re: Very Specific Generics

2008-05-08 08:07 • by Kir Birger (unregistered)
That would have been fine if he'd had the sense to declare a special class for it.

Re: Very Specific Generics

2008-05-08 08:07 • by ParkinT
What a HASH mess!!

I like the 'superHashMap'. Does he wear a mask or a red cape?

Re: Very Specific Generics

2008-05-08 08:10 • by Bombe (unregistered)
There probably would have been a better (readable) way to store his data structures.

Re: Very Specific Generics

2008-05-08 08:12 • by yuumei
HashMap<Long, HashMap<Timestamp, Vector<HashMap<String, Object>>>>


So the long is the universe, the timestamp is the time, the vector is the dimension and the string is the key.

What's wrong with that?

Re: Very Specific Generics

2008-05-08 08:15 • by Treeki (unregistered)
This reminds me of C++ too badly.

Re: Very Specific Generics

2008-05-08 08:16 • by Ciaran McCreesh (unregistered)
Ah yes. No-one will ever need typedef, so let's remove it from the language! Also, operator overloading is clearly always evil, there are no legitimate uses of multiple inheritance, programmer-controllable memory management is pointless, stack allocation is a waste of time, const is silly, scope-controlled destruction doesn't help anyone, a common base class for everything is good and treating programmers like idiots is the best idea ever.

Re: Very Specific Generics

2008-05-08 08:19 • by ClaudeSuck.de (unregistered)
Fast First, Frust!

Re: Very Specific Generics

2008-05-08 08:19 • by Volmarias
193686 in reply to 193683
This may not be my code, but I've left plenty of little nuggets like that in the codebase from my last job, and I'm more than ok with it. I'm being VERY explicit about what's going on, and it shuts the compiler up.

Addendum (2008-05-08 08:25):
It could/should probably be refactored a bit so that at least a chunk of that is it's own class, but without seeing the context I can't say.

Re: Very Specific Generics

2008-05-08 08:23 • by Volmarias
193687 in reply to 193684
Ciaran McCreesh:
Ah yes. No-one will ever need typedef, so let's remove it from the language! Also, operator overloading is clearly always evil, there are no legitimate uses of multiple inheritance, programmer-controllable memory management is pointless, stack allocation is a waste of time, const is silly, scope-controlled destruction doesn't help anyone, a common base class for everything is good and treating programmers like idiots is the best idea ever.


Sounds like your "brillant" code was scrapped in favor of something written with Java, and you're Not At All Bitter Not Even A Little Bit.

Frankly, I don't see why you're happy with the wastefulness of C++. Why aren't you writing in assembly? I mean, have you SEEN the overhead that compilers create? And I don't see why we need to be told that we can't use a floating point number JUST AS IT IS as a pointer! Damnit, I know exactly what I'm doing, just let me do it!

Re: Very Specific Generics

2008-05-08 08:23 • by MooseBrains (unregistered)
Now would be a good time to write your own fucking data structure

Re: Very Specific Generics

2008-05-08 08:38 • by Claxon
193689 in reply to 193686
Volmarias:
I'm being VERY explicit about what's going on, and it shuts the compiler up.




/**
* Warning! This class contains VERY explicit language,
* due to all of the compiler warnings I was getting...
* Sheesh!
*/

Re: Very Specific Generics

2008-05-08 08:40 • by Cope with IT (unregistered)
193690 in reply to 193688
MooseBrains:
Now would be a good time to write your own fucking data structure
...definition language.

No wait! We already have that: XML!

Re: Very Specific Generics

2008-05-08 08:42 • by Rich (unregistered)
Gah, introducing Vector usage in Java 5? Inexcusable. It's been ArrayList since 1.2 y'know...

Re: Very Specific Generics

2008-05-08 08:43 • by Joe (unregistered)
Fifteenth!

Re: Very Specific Generics

2008-05-08 08:45 • by The Java Inquisition (unregistered)
TRWTF is that he coded to the implementation rather than the interface. The line should read:

Map<Long, Map<Timestamp, List<Map<String, Object>>>> superHashMap =
new HashMap<Long, Map<Timestamp, List<Map<String, Object>>>>();

Much better.

Also, Vector is deprecated, except that it's not.

Re: Very Specific Generics

2008-05-08 08:46 • by Tonio (unregistered)
I get it. The WTF is that the problem here isn't Generics at all, but data structures being nested on the fly (instead of being refactored out into their own class), right?

Re: Very Specific Generics

2008-05-08 08:46 • by commenter (unregistered)
Hmmm. I suppose it's clearer or at least compile-time-safer than the equivalent non-generic code:

HashMap superHashmap = new Hashmap(); //of long to hashmap mapping from timestamp to vector of hashmaps mapping from string to object.

Re: Very Specific Generics

2008-05-08 08:48 • by Dirk (unregistered)
I know, he should be using interfaces. (And what is that: Vector?)

Map<Long, Map<Timestamp, ? extends List<Map<String, Object>>>> superHashMap =
new HashMap<Long, Map<Timestamp,? extends List<Map<String,Object>>>>();


Looks much better...

Re: Very Specific Generics

2008-05-08 08:49 • by robert (unregistered)
"

That's better, I can breathe now.

Re: Very Specific Generics

2008-05-08 08:51 • by Merijn Vogel (unregistered)
HashMap<Long, HashMap<Timestamp, Vector<HashMap<String, Object>>>> superHashMap =
new HashMap<Long, HashMap<Timestamp, Vector<HashMap<String, Object>>>>();

Be glad, very glad that this predecessor of yours used this notation. You wouldn't have none that this superHashMap was a seven-headed dragon if the definition read Map superHashMap = new HashMap();

Now at least you know you're in trouble; good luck though ;)

Re: Very Specific Generics

2008-05-08 08:52 • by Rich (unregistered)
193701 in reply to 193694
The Java Inquisition:
TRWTF is that he coded to the implementation rather than the interface. The line should read:

Map<Long, Map<Timestamp, List<Map<String, Object>>>> superHashMap =
new HashMap<Long, Map<Timestamp, List<Map<String, Object>>>>();

Much better.

Also, Vector is deprecated, except that it's not.

Hmmm, something tells me that elsewhere in this class we'll have methods defined thusly:

public void doStuff(Vector<HashMap<String, Object>> listMapThing)
{
// Do stuff here. Interface? What's an interface?
}

Nasty.

Re: Very Specific Generics

2008-05-08 08:52 • by prod (unregistered)
193702 in reply to 193687
Shush you, he's talking about extra features that are helpful. That is the opposite of going back to assembly. typedef would sort this crap code instantly

Re: Very Specific Generics

2008-05-08 08:53 • by Raedwald
Yeah, in C++ I'd introduce a typedef, but is this really so bad? By no means a Worse Than Failure. I guess we'll have an article about multi-dimension arrays next.

Re: Very Specific Generics

2008-05-08 09:01 • by notme (unregistered)
As a C++ programmer, this does not even faze me. Try using the STL a bit more and then look at how the types used end up being called by the compiler/linker at the end. The example in article is harmless.

Re: Very Specific Generics

2008-05-08 09:01 • by Volmarias
193705 in reply to 193702
prod:
Shush you, he's talking about extra features that are helpful. That is the opposite of going back to assembly. typedef would sort this crap code instantly


No he's not, he's just complaining about how Java isn't exactly C++ and C++ has all sorts of really good stuff and Java is for idiots and newbies and it may as well be visual basic and did I mention that C++ won the war of 1812 with operator overloading but YOU wouldn't know about that with your fancy pants language for idiots that doesn't even have typedefs! Sheesh!

Re: Very Specific Generics

2008-05-08 09:02 • by zombiekitty (unregistered)
I'm not so concerned with the "very specific generics". I've done stuff like that mainly to just shut the compiler up.

My concern is with the horrid data structure. I've done a hash of a hash before, but never a hash of a hash of a vector of a hash. Surely that could have been organized into a better set of objects.

Re: Very Specific Generics

2008-05-08 09:04 • by Smash (unregistered)
193708 in reply to 193703
Raedwald:
Yeah, in C++ I'd introduce a typedef, but is this really so bad? By no means a Worse Than Failure. I guess we'll have an article about multi-dimension arrays next.
I can't tell whether you're dead serious or ultimately sarcastic here. I hope for the latter, or I pity your cow-orkers

Re: Very Specific Generics

2008-05-08 09:12 • by Johan (unregistered)
Hmm, I can't find anything worse than
Map<String,Map<String,Map<String,Long>>>
on the class I'm viewing now. But somehow I don't really consider it WTF-worthy.. Must be a thin line I have somewhere, or I'm slowly becoming a lesser programmer.

Re: Very Specific Generics

2008-05-08 09:12 • by DKO (unregistered)
193710 in reply to 193687
Volmarias:
Ciaran McCreesh:
Ah yes. No-one will ever need typedef (...)


Sounds like your "brillant" code was scrapped in favor of something written with Java, and you're Not At All Bitter Not Even A Little Bit.


Looks like you (and the submiter) missed the point. The WTF is not that the guy is using generics. There are actually 2 (maybe 3) WTFs:

1) There is no typedef. The code would be much more cleaner as
SuperHero marypoppins = new SuperHero();

Of course, in Java you just create Yet Another Class to mimic a typedef.

2) The guy is not sensitive to pain anymore. Anyone who would dare to write that much code just to create an object has probably sold his soul.

3) Did the submiter suggest not using generics would be better? If so, that's an even bigger WTF than the code. Languages have types for a reason.

The other characteristics pointed Volmarias are present in many other languages, not just C++; maybe it's you that is the disgrunted programmer with an agenda to criticize C++. I see Java programmers flaming C++ much more often than C++ programmers flaming Java.

And thank god I don't have to code in Java. It's mostly Python for me. It has typedefs, operator overloading, multiple inheritance, const (through a bit of hacking), scope-controlled deterministic destruction, and no common base class for everything. And it doesn't treat me like an idiot.

Re: Very Specific Generics

2008-05-08 09:13 • by Jon B (unregistered)
public void DoNothing<T>(T eyewear) where T : goggles {}

Re: Very Specific Generics

2008-05-08 09:14 • by TraumaPony (unregistered)
193712 in reply to 193708
Smash:
Raedwald:
Yeah, in C++ I'd introduce a typedef, but is this really so bad? By no means a Worse Than Failure. I guess we'll have an article about multi-dimension arrays next.
I can't tell whether you're dead serious or ultimately sarcastic here. I hope for the latter, or I pity your cow-orkers

Wow, how witty.

Re: Very Specific Generics

2008-05-08 09:19 • by prod (unregistered)
193713 in reply to 193705
Volmarias:

No he's not, he's just complaining about how Java isn't exactly C++ and C++ has all sorts of really good stuff and Java is for idiots and newbies and it may as well be visual basic and did I mention that C++ won the war of 1812 with operator overloading but YOU wouldn't know about that with your fancy pants language for idiots that doesn't even have typedefs! Sheesh!

Um.. ya... and? Your argument is the same and it's a non sequitur quite frankly. Those features you mention are helpful language constructs removed from Java purposefully, so in that sense Java is more like returning to assembly.

I would rather have typedef in Java thanks. Then we could shorten this and give it some obvious semantic meaning very quickly, instead of resorting to creating a completely new data structure or wrapper class for it like other people say. It's the opposite of returning to assembly.

"PhoneBook" is much easier to understand than "map< string, string>" (which could mean anything!).

Re: Very Specific Generics

2008-05-08 09:21 • by Jim T (unregistered)
It's kind of a shame how all that effort is put into making this very specific data structure, which at the end of the day, stores a load of Objects ...

Re: Very Specific Generics

2008-05-08 09:23 • by Ritchie (unregistered)
Have to say that the real WTF is the Java language itself. You can't typedef this, so you're stuck between writing out a long generic definition, getting RSI writing out a whole load of class definitions which makes the calling code smaller but the called code ten times more complicated and error prone, or creating a derived class which might look like the generic version some of the time, but will bite you in the ass when you discover it isn't.

On your left, a rock. On your right, a hard place.

Re: Very Specific Generics

2008-05-08 09:25 • by Robin Message (unregistered)
193716 in reply to 193713
class PhoneBook extends map<string,string> {}

It's slightly verbose, but it's basically a typedef. Plus it has the advantage of making the language more orthogonal.

Re: Very Specific Generics

2008-05-08 09:27 • by brazzy
193717 in reply to 193710
DKO:

1) There is no typedef. The code would be much more cleaner as
SuperHero marypoppins = new SuperHero();

Of course, in Java you just create Yet Another Class to mimic a typedef.

'mimic'?
Classes (and interfaces) are how you define types in Java. This is not a case of the language not having the necessary tool, it's a case of the language having the tool and the programmer not using it.

DKO:

The other characteristics pointed Volmarias are present in many other languages, not just C++;

And none of them have anything to do with the WTF at hand.

DKO:

maybe it's you that is the disgrunted programmer with an agenda to criticize C++. I see Java programmers flaming C++ much more often than C++ programmers flaming Java.

Where? I have never seen a Java programmer start a rant against C++ based on nothing at all, while the reverse is a daily occurence pretty much everywhere I've seen the two meet.

Re: Very Specific Generics

2008-05-08 09:27 • by h (unregistered)
193718 in reply to 193681
yuumei:
HashMap<Long, HashMap<Timestamp, Vector<HashMap<String, Object>>>>


So the long is the universe, the timestamp is the time, the vector is the dimension and the string is the key.

What's wrong with that?
It's still not superHashMap enough...

Re: Very Specific Generics

2008-05-08 09:29 • by Milivoj (unregistered)
193720 in reply to 193681
Would be nothing wrong with that if it was readable... doesn't Java have something along the lines of C++'s typedef?

Re: Very Specific Generics

2008-05-08 09:31 • by Smash (unregistered)
193721 in reply to 193712
TraumaPony:
Wow, how witty.
Great! Let the Flame Wars begin. And since it's my turn, I guess I'll throw an infamous yo'mama joke now.

Captcha: valetudo. That's a fighting style. Oh the irony...

Re: Very Specific Generics

2008-05-08 09:37 • by foo (unregistered)
193725 in reply to 193681


HashMap<Long, HashMap<Timestamp, Vector<HashMap<String, Object>>>>

So the long is the universe, the timestamp is the time, the vector is the dimension and the string is the key.

What ever problem it is that really, truly, honestly, _needs_ a solution like this... that needs to distinguish between objects created in the same thread and the same name inside the same millisecond... SHOULD NOT be solved with Java. The programming language to succinctly solve this problem does not exist... and is not intended for mortals. That would be a nightmare problem even for Haskel... or any language. Hell, I think staring into the face of Chluthlu for an hour would be easier. Defining the data structure is simple enough... working with it would drive a mortal to madness.

Re: Very Specific Generics

2008-05-08 09:38 • by brazzy
193726 in reply to 193715
Ritchie:
Have to say that the real WTF is the Java language itself. You can't typedef this, so you're stuck between writing out a long generic definition, getting RSI writing out a whole load of class definitions which makes the calling code smaller but the called code ten times more complicated and error prone, or creating a derived class which might look like the generic version some of the time, but will bite you in the ass when you discover it isn't.

On your left, a rock. On your right, a hard place.

In front of you: the correct ways to do it, which are either:

1) If you just want to search for entries that match a specific Long, Timestamp and String: collate them into a single class with appropriate equals() and hashCode() methods so that instances can be used straightforwardly as a key in a simple HashMap.

2) If you want to do flexible partial-match searches: use a friggin' DBMS.

With your wonderful typedef, you'd still be writing WTF code, it just wouldn't be as obvious.

Re: Very Specific Generics

2008-05-08 09:40 • by foo (unregistered)
193727 in reply to 193715
They need groovy: http://groovy.codehaus.org/
... many of those problems don't exist in hybrid Java/Groovy code.

Re: Very Specific Generics

2008-05-08 09:54 • by webrunner
I actually had to do some hashmaps of vectors in C++ and discovered if you just go
std::map<std::string, std::vector<std::string>>
it gives you an error for nesting <'s

Re: Very Specific Generics

2008-05-08 09:59 • by Tim Ward (unregistered)
Is this another way of saying:

"Why hasn't Java got typedef?"

Re: Very Specific Generics

2008-05-08 10:01 • by immitto (unregistered)
193732 in reply to 193726
finally someone gets the point.
A) the constructor is ugly (and there are proposals and many utils out there to mitigate the redundancy in the declaration)
B) the declaration should be "Map<Long, Map<Timestamp, List<Map<String, Object>>>>"
C) the structure is probably not the clearest even in that case

but think about the traversal. I'm omitting the Vector layer, since i can't figure out what that would be fore.

Long id = 1;
Timestamp time =...;
String key = "...";
Object value = superHashMap.get(id).get(time).get(key);

now make that null-safe

Object value = null;
Map<Timestamp, List<Map<String, Object>>> tmp = superHashMap.get(id);
if (tmp != null) {
Map<String, Object> tmp1 = tmp.get(time);
if (tmp1 != null) {
Object value = tmp1.get(key);
}
}

At least with generics the traversal is compile-time type-safe. But still a horrible mess compared with what the user wants (and would get if they made a class, and which a typedef does not provide) of

Object value = superMap.get(id, time, key)

A typedef will not give you the clean & nullsafe traversal machanics you get by defining a class. And the typedef:
A) does not give you compile-time type-check for one Map vs another Map typedef
B) does not give you convenient, semantically clear, and safe traversal
C) only adds clarity to the declarations, whhich also is better captured by the variable names. With "PhoneBook phonebook;... String phonenumber = phonebook.get(name);" the typedef doesn't change the later line at all. It's the variable names that make the meaning clear.

It is not the lack of typedef that makes this a WTF, it's the bad coder. Which is possible in any language by anyone at any time.

Re: Very Specific Generics

2008-05-08 10:06 • by Volmarias
193733 in reply to 193710
DKO:
I see Java programmers flaming C++ much more often than C++ programmers flaming Java.


And I never see it, really, ever.

I think you're missing the point; rather than dealing with the WTF at hand (which isn't much of a WTF for that matter), you point out a laundry list of things that other languages have that Java doesn't, a good deal of them on purpose.

DKO:
The other characteristics pointed Volmarias are present in many other languages, not just C++; maybe it's you that is the disgrunted programmer with an agenda to criticize C++.


Java doesn't have lambda functions as far as I understand it. Does that mean that Java programmers should lament their fate?


And thank god I don't have to code in Java. It's mostly Python for me. It has typedefs, operator overloading, multiple inheritance, const (through a bit of hacking), scope-controlled deterministic destruction, and no common base class for everything. And it doesn't treat me like an idiot.


I'm glad you're happy. Java doesn't treat me like an idiot either; I have yet to run into a problem that I couldn't solve just because of the language format. Java isn't C++, if you accept this fact you'll be fine programming in it. But, please, lets start a holy war over this because you don't like Java.

Re: Very Specific Generics

2008-05-08 10:07 • by XyC (unregistered)
193734 in reply to 193681
Looks like poo :) Ever heard of the adapter pattern? Allows you to hide even very complicated data structures and only a simple interface is public.

http://en.wikipedia.org/wiki/Adapter_pattern

Re: Very Specific Generics

2008-05-08 10:26 • by Val (unregistered)
193738 in reply to 193684
Best critic of Java, ever!
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment