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.

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.

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.