I’ve been doing some Java development lately and Java’s verboseness sent me looking for a way to shorten some of the monstrous generic type declarations. I know there is no
typedef or similar concept in Java, but hoped that maybe some Java or Design Patterns gurus invented some clever trick or pattern to do something similar. It’s better to use a
ThreeLevelHashMapOfStrings type instead of
HashMap<String, HashMap<String, HashMap<String, String>>>, right?
Wrong. Google for ‘Java typedef’ and you will find out that many people encountered the same problem, but there is no approved solution for this problem. In fact, Java people tend to think that the problem is not the lack of keyword, but the people asking for it (as most of them admit that they have other languages background). Ask for ‘Java typedef’ on a Java forum and you will bashed by “true Java” advocates with ridiculous arguments.
My favorites are that “Java’s beauty was in its simplicity” (and apparently it loses some part of it with every new language feature, generics being the main offender), “if you don’t like it go back to your C++” (no comments) and that “typedef would require the other programmers to look up the definition to find out what it really is“.
The last one is repeated so often that I suppose some people really believe in it. If I understand it correctly those people also oppose defining new classes and new methods (because you would have to look them up to see what they do). This is ridiculous because, in a way, programming is all about defining new names for things (classes) and actions (methods) and then using them to define even more new names. I might be wrong but most of the modern languages (at least those I can think of right now) allow programmers to create some sort of type aliases (
typedef in C/C++,
type in Haskell, just assigning a constant in Ruby).
But “all others are doing this” is not the only argument for typedef in Java. The three main arguments are:
- shortening the monstrous generic type definitions,
- improving readability of variable declarations etc.,
- preserving DRY and adding flexibility (typedef would allow you to change the underlying type in one place instead of every place).
This is when the fun starts because trolls join the discussion and start convincing you that the desire to shorten the code is evil (Java people are used to having 30+ character class names), that giving a name to a complicated type definition will not increase readability (because someone somewhere might come up with a bad name, apparently) and that for flexibility you should use a design pattern and define some interface or something like that. And why would you need to change a type, anyway?? Blub programmers in the wild.
I especially like the fragment when some of the typedef proposers uses
Iterator<Container<Entity, Comparator<Entity>>> in his examples and a typedef opposer uses
List<String> in his response. Internet trolling at its best.
This got me thinking about how the language design and principles influence the users and the communities. Java was created so “you can’t shoot yourself in the foot” and that’s the main reason why some C++ features (operator overloading, multiple inheritance, etc) were deliberately not included in Java. On the other hand, Ruby was created so programmers can enjoy programming. That’s why it has plenty of features that make programming easier and more enjoyable.
Somehow, this influences the mindsets of people using the languages. Or maybe it’s the mindset that makes person prefer Java or Ruby?
My impression from interacting with Java people (both at work and in the Internet) is that they generally don’t trust the other programmer, they tend to think that most other programmers are mediocre at best and the language should mainly be a tool that doesn’t allow those inferior beings to do much harm.
Ruby people are generally much friendlier to each other, they praise each other’s work (to the point of hype sometimes) and they treat the language as a tool to create fun things. If the tool is lacking, they look for the ways to improve it (hence DSLs, meta-programming and monkey patching). I also noticed that flames and trolling are much less frequent on Ruby forums than on Java forums.
And before you start flaming me, Java trolls, please note the use of “my impression”, “generally”, and “mostly” :)