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.

1

u/[deleted] Feb 22 '15

I mean it's not like it's technically infeasible to infer the type at compile time. The current approach is just a lot clearer (see the second point).

For example, a quick glance while scrolling through the code in a web-based code viewer shows you what type you're dealing with. You don't need to go into an IDE or follow the source code to figure this out. Basically what I'm getting at is that it's a feature, not a deficiency.

Also, IDEs kinda give you coding-time type inference and add the types to the source code for you. So you don't generally have to type it out.

2

u/ihcn Feb 22 '15

I strongly disagree that it's a feature. Java seems to have the mentality that it doesn't give you enough rope to hang yourself with, so it takes away all sorts of crucial features. The ostensible tradeoff is that your code is easier to understand as a result.

But in my experience with enterprise Java, bad developers will write awful code no matter how much you take away from them. The worst codebase I've ever seen, by far, was Java.

2

u/cmiles74 Feb 22 '15

I'm not convinced, I believe there are simply more people writing Java thus we see more poor quality Java code.

1

u/[deleted] Feb 22 '15

Agreed. Bad developers will write bad code regardless. I'm not saying it prevents bad code.

I think it's a feature because of the self-documenting nature of it.

In the end it's just a matter of personal opinion. I won't insist on attempting to change yours.

0

u/giuseppemag Feb 22 '15

If only we could do something to avoid this kind of lack of clarity. Like naming variables something descriptive that somehow makes the type obvious... :)

0

u/contantofaz Feb 22 '15

Java has thousands of libraries packaged up in jar files. Not many languages have that. Languages sometimes expose libraries in source-code format. With a binary-like format, information could be lost. So languages like Java that make declaring types extremely redundant may help the IDEs with figuring out the types of those variables. :-)

Also, one of the goals they may have had for Java was to make competing implementations compatible. Dumbing down the language may have been an effort to make that less difficult. It seems that even new languages are willing to go in that direction so they can help with multiple implementation goals.

Languages that do a great deal of type inference may end up with only their reference implementations. Although perhaps we shouldn't have blamed the type inferencing as the whole cause of it. I think languages willing to break backward compatibility with every other release may not be too enticing for mainstream use. Hence they ending up with just their reference implementations.

5

u/giuseppemag Feb 22 '15

What sort of argument against type inference is this?!?

Type inference just requires signatures, which should well be present in any precompiled library (linking is otherwise impossible). In principle Java has nothing that would prevent type inference, precisely like var in C#.

-1

u/[deleted] Feb 22 '15

What sort of argument against type inference is this?!?

I'd argue that IDEA's struggle to provide correct and meaningful Scala type information (the best form of type checking can often yield false positives) is a good argument against it.

1

u/giuseppemag Feb 28 '15

F# or Haskell have no such struggle. It just means that this specific implementation is of no such good quality.

→ More replies (5)

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.

-4

u/[deleted] Feb 22 '15

There needs to be a Poe's Law for Java, because it is impossible for me to tell when people who comment about the 'right way' to express Java aren't just trolling with things like Map<String,Set<String>> depMap = HashMap<>();

Sort of the same way there's very little difference between code in this library: https://github.com/goldmansachs/gs-collections

And this one: https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

2

u/[deleted] Feb 22 '15

Remember that this

Map<String, Set<String>> depMap = new HashMap<>();

Replaces this

Map<String, Set<String>> depMap = new HashMap<String, Set<String>>();

So it is obviously perceived as an improvement.

2

u/uprislng Feb 22 '15

looking at that FizzBuzzEnterpriseEdition repo gave me cancer, thanks a lot.

I know plenty of people like it, but I think I'd rather find another profession if I could only ever program in Java for the rest of my life.

1

u/s73v3r Feb 22 '15

Keep in mind that the Enterprise FizzBuzz is a product of a certain environment, which does happen to favor Java (and C#). There are plenty of other environments which do use these languages without needing that huge, over engineered cruft.

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!

0

u/[deleted] Feb 22 '15

[deleted]

1

u/zehydra Feb 23 '15

I'm quoting the author.

-6

u/[deleted] Feb 22 '15

new HashMap();

new HashMap <String, Set>();

5

u/[deleted] Feb 22 '15

new HashMap();

new HashMap <String, Set>();

new HashMap<>();

1

u/Pragmataraxia Feb 22 '15

Some time ago, they allowed the template (or whatever Java calls them) variables to be inferred from the variable declaration, so you no longer have to type that part out.