How Java needs to become cleaner
Reading lots and lots of Java code made me realize there is too much noise in there. Not that I agree with the Railists cry of ceremony – they just don’t get it – but too much noise because of purely bad choices for defaults and missing syntactic sugar for often used cases.
Why would we need less noise, how is less noise helpfull? Sometimes it’s hard to see what happens in methods – and it’s not helped by the fact that many Java developers don’t do the best Java can do, see my post on “Next generation Java programming style”.
The cry for a less noisy Java are limited by the actual developers, all changes would need to be easy enough that they do not change the language. We do not want another Lisp (because we would use Clojure then) but a language for the same target audience as Java. A language should support best practices – those we’ve learned over the last 15 years of using Java – and not make it hard and noisy to use them.
So here I go:
- Not dropping explicit type declarations (see my “Explicit Static Types are not for the Compiler, but for the Developer – Duh”), but infer types more often (see Scala), Java could right now infer more types, without the need for advanced algorithms (e.g. start with the Return type). As James writes:
In Java I “only” have to annotate types when I want to assign to a variable or return from a method. Which, come to think of it, is an odd restriction. The compiler obviously knows the type, so why do I have to tell it yet again?
Keep the possibility to declare types for narrowing down scope.
- Drop the public modifier for methods, everything is public, until it is declared private or protected.
- Every field is private by default.
- Support for properties, no need for setter and getters, all properties are public unless declared private (how to annotate properties?). Getters and setters override internal property accessors.
- Sugar for creating lists and maps,  and [:] – hopefully nicer than Scala does it, Groovy does this in a consistent way.
- And just add Tuples, which are sometimes useful, or at least map them to a list and add “extractors”
val a,b = returnsTuple();. As Java Pitstop writes:
When I was coding in Java I used to build Classes just to return multpile values and also sometimes used pass by reference (by means of using Objects). I really missed a permanent solution [...]
This doesn’t mean you shouldn’t think if the return type justifies a real type obviously.
- drop “;” it really only adds noise
- Final by default: Final is your new love, all variables, attributes, parameters are final, see my All variables in Java must be final. If a variable, attribute or parameter isn’t final, declare it with “mutable”
Changes that would change Java significantly, all APIs and all thinking, are more controversial:
- Add Closures
- Or at least: Support for anonymous inner classes, where I do not have to write the boiler plate code when there is only one method in the interface
What would you do to reduce noise in Java, without making it a completly different language?