r/programming • • Feb 22 '15

Java Makes Programmers Want To Do Absolutely Anything Else With Their Time

http://www.forbes.com/sites/quora/2015/02/21/java-makes-programmers-want-to-do-absolutely-anything-else-with-their-time/
35 Upvotes

636 comments sorted by

View all comments

1

u/zehydra Feb 22 '15

"HashMap<String, Set> depMap = new HashMap(); …I die a little on the inside. Because to me, that says, “depMap is a map, which is a map.”"

Sorta. It's setting a reference of type HashMap to a new HashMap.

24

u/[deleted] Feb 22 '15

Yes, Java can be more verbose, but that specific example is a very bad argument against it. That's basically how every other strongly typed language did (and some still do) things. Actually, that's not a very good argument either. So, here's more:

  • Polymorphism. The type on the left is what the following code expects to work with. The type on the right is the specific implementation. You could just as easily have Map<String, Set> on the left and new TreeMap<>() on the right. The lines that follow depend on depMap being a map, not a tree or hash map. This doesn't mean much for a silly example like this but in larger systems, it helps in more than one ways. Which brings me to,

  • Self-documenting. If you were able to do it without specifying the type, say something like depMap := getDependencies(), at a quick glance I can't tell what the type of depMap is. On the other hand, Map<String, Set> depMap = getDependncies() tells me exactly what's going on.

  • You don't really have to type it out manually. You'll be using an IDE. If you know how to use your IDE, you will type variable name equals to right hand side, press two extra keys, and let the IDE add the type -- selecting one if there are multiple viable options (because polymorphism).

8

u/ihcn Feb 22 '15

I think the argument is for compile time type inference, something every modern compiled language has, except java of course. Any IDE will tell you what the type is, without you having to type it out.

→ More replies (14)

15

u/depressiown Feb 22 '15

This bit of code annoys me for several reasons:

  • Unless you're using something specific to the implementation type, you could use the interface: Map<String,Set> depMap = new HashMap();

  • He's not using the diamond for the generic types: Map<String,Set> depMap = new HashMap<>();

  • He's not typing the Set: Map<String,Set<String>> depMap = HashMap<>();

  • Could use Guava to abstract the inner collection: Multimap<String,String> depMap = HashMultimap.create();

Articles/comments like these constantly annoy me. They're written by people who really aren't that familiar with clean, modern Java. Lambdas, ORM, libraries of annotations... there's so much stuff out there to simply how much Java you write and make it much cleaner than all these examples people use of why Java is bad.

→ More replies (4)

4

u/apotheotical Feb 22 '15

First, you forgot the diamond ("new HashMap<>()"). Second, you should never assign to concrete types if possible. Always use an interface or abstract where possible (simply Map in this case). This makes it easier to swap out implementations later!

→ More replies (7)