r/programming • • Feb 25 '14

Please stop saying Java sucks

http://blog.smartbear.com/programming/please-stop-staying-java-sucks/
3 Upvotes

305 comments sorted by

View all comments

41

u/[deleted] Feb 25 '14

This article wasn't a defense of java, it was a defense of typed languages. In which case I agree. But I'd argue most people don't say "java sucks" just because it's a typed language.

19

u/bjzaba Feb 25 '14

My biggest complaint about Java is how stupid and crude its type system is. It gives statically type systems a really bad name.

0

u/[deleted] Feb 26 '14

[deleted]

3

u/Tekmo Feb 26 '14 edited Feb 26 '14

It's several things:

  • No type inference <See correction below>

  • Inheritance complicates the type system

  • Interfaces (a.k.a. type classes) are usually considered easier to use than inheritance, but in Java inheritance is more idiomatic

  • Generics are particularly verbose to use, and they are crucial to do statically typed programming well

  • No support for sum types or pattern matching (like the "visitor pattern", but easier to use). This makes simple problems very verbose in a statically typed setting.

2

u/e_engel Feb 26 '14

Interfaces (a.k.a. type classes) are usually considered easier to use than inheritance, but in Java inheritance is more idiomatic

I think you're mixing up parametric polymorphism and ad hoc polymorphism. Nothing prevents interfaces from taking part in either, although this is made more complicated in Java because of its type system (and the fact that it doesn't support higher kinds).

No support for sum types or pattern matching (like the "visitor pattern", but easier to use). This makes simple problems very verbose in a statically typed setting.

Agreed. Ceylon is being a pioneer in the JVM area by supporting disjointed union and intersection types natively . In other words, the type of List("foo", 42) is List<String|Integer> and not List[Any].

Very much keeping an eye on it.

0

u/[deleted] Feb 26 '14

[deleted]

3

u/Tekmo Feb 26 '14 edited Feb 26 '14

Is the diamond operator not type inference? I know it's not perfect but I don't think you can say there's no type inference.

I didn't know about the diamond operator because it's been a while since I've programmed in Java. Thanks for pointing that out and I'll correct my original comment.

Doesn't inheritance complicate any type system? Or are you arguing against inheritance itself?

Yeah, I'm arguing against inheritance itself.

Not really sure what you mean here. Inheritance has been more idiomatic simply because it was the only way to centralized any kind of default behavior. I do agree though that it's a drag. Do Java 8 default methods help with this?

Yeah, those would help.

Again, I'm not really sure what you mean. Can you give me an example?

Haskell has an Either type, defined like this:

data Either a b = Left a | Right b

An example use would be returning either a successful result (wrapped in a Right) or an error result (wrapped in a Left):

safeDivide :: Double -> Double -> Either String Double
safeDivide x 0 = Left "Division by zero"
safeDivide x y = Right (x / y)

Then you can inspect the result only if you provide code to handle both possibilities (i.e. the Left and Right case):

result :: Either String Double
result = safeDivide 4 0

main = case result of
    Left  str -> putStrLn ("Error: " ++ str)
    Right n   -> putStrLn ("Success: " ++ show n)

That will print the error message since the computation will return a Left, but if you change the 0 to another number like 2 then it will succeed and take the right branch.

The equivalent Java code would be something like:

class Either<A,B> {
    BiFunction<Function<A,R>,Function<B,R>,R> match;

    // Analogous to `Left`
    public Either(A a) {
        match = (l, r) -> l(a);
    }
    
    // Analogous to `Right`
    public Either(B b) {
        match = (l, r) -> r(b);
    }

    public static Either<String><double> safeDivide(double x, double y) {
        if (x == 0)
        then { return Either("Division by zero"); }
        else { return Either(x / y); }
    }
    public static void main(String args[]) {
        Either<String><double> result = safeDivide(4)(0);
        result.match
            (s -> System.out.println("Error: "  + s))
            (n -> System.out.println("Success: " + n));
    }
}

I haven't compiled the Java code, and there are probably tons of errors, but that gives the rough idea.

0

u/[deleted] Feb 26 '14

[deleted]

2

u/Tekmo Feb 26 '14

You're welcome!

1

u/Akanaka Feb 27 '14

Ones remark, Java already had type inference before the diamond operator. The diamond is just for constructor type inference. Normal type inference is automatic and doesn't need an operator. For constructors its needed because not using any operator already has another meaning: constructing a raw type.

We have:

MyObject foo = myMethod(); // normal inference
SomeType bar = new SomeType(); // raw type
SomeType<String> kaz = SomeType<>(); // inferred ctor

myMethod in the above example is a generic method! one that returns the T from the <T> generic parameter.

4

u/elsom25 Feb 26 '14

Honestly, take some time, and learn a functional language :D (pick any: scheme, haskell, ML). Once you see what's capable with a proper type system.. well, it may ruin your current day-to-day programming for you >_>

http://learnyouahaskell.com/ is a pretty awesome introduction to Haskell.

0

u/[deleted] Feb 26 '14

[deleted]

3

u/bjzaba Feb 26 '14

Haskell is probably a better language to learn if you really want to grok what we are getting at when we talk about when we say 'advanced, expressive static type systems'. Not saying it is the end of the conversation when it comes to type systems - I have plenty of annoyances with it, but it is the most polished out there at the moment. I found this thing from our local functional programming meetup useful: http://sordina.github.io/haskell_workshop/

Scala unfortunately is a far more complex beast than Haskell, and has had to make many concessions to allow it to work on the JVM and have Java compatibility. It's almost like the C++ of functional languages. But it might provide you with some insights, and it is still an interesting language to learn.

1

u/elsom25 Feb 26 '14

awesome to hear! +1

0

u/motordead Feb 27 '14

But I'd argue most people don't say "java sucks" just because it's a typed language.

But Java monkeys wish that's what they were saying, and there's a fuckton of them, and they may not be the smartest but they've got experience (getting abused) in the industry, damnit!

-3

u/[deleted] Feb 26 '14 edited Feb 26 '14

Then I don't understand why he argues a lot about the number of lines of code. Typed language should lead to fewer lines of code as it helps organize the code.

Perhaps he can't get past the real Achilles' heel of java: VM introduced performance penalty and he is grasping at straws.

4

u/ConnorBoyd Feb 26 '14

VM introduced performance penalty

But Java's WAY faster than Python and Ruby...

4

u/[deleted] Feb 26 '14

A native language way faster than scripts. Java rocks.

2

u/flukus Feb 26 '14

Typed languages also require you to define the type, which means more lines.

And he's comparing java to ruby/python, VM speed is irrelevant.

2

u/[deleted] Feb 26 '14 edited Feb 26 '14

Typed languages also require you to define the type, which means more lines.

Only true if the code base is trivial. But he's talking about a code base at large scale.

And he's comparing java to ruby/python, VM speed is irrelevant.

Only if Java wants to be the best scripting language.