the blog for developers

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:

  1. 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.

  2. Drop the public modifier for methods, everything is public, until it is declared private or protected.
  3. Every field is private by default.
  4. 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.
  5. Sugar for creating lists and maps, [] and [:] – hopefully nicer than Scala does it, Groovy does this in a consistent way.
  6. 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.

  7. drop “;” it really only adds noise
  8. 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:

  1. Add Closures
  2. 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?

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.

1 Comment 22 Tweets 2 Comments

Leave a reply.

Comments

Why not just use Scala or Groovy instead ?

MaggieL

Yeah, what was it you didn’t like about Scala again?

Cedric

I agree with your list, with my top priorities being type inference and support for properties.

I’m ambivalent about “public methods by default” and wondering if the default shouldn’t be private (one thing for sure: the current default is dumb).

On one hand, forcing developers to put a “public” in front of their methods will make them ponder whether it’s really what they want, on the other hand, maybe most methods tend to be public, so making this the default would reduce the noise.

I’m also a bit on the fence about tuples since I don’t need to return multiple values that often, but I think that as long as tuples are statically typed, there is probably little harm in supporting them.


Cedric

lbertrand

You are describing Fantom… See here http://fantom.org

josh

Rahul is right. With this list you’d probably be much happier using Scala or Groovy.

Shirish

IMO, Closures for Java are coming in JDK7
http://www.jroller.com/scolebourne/entry/closures_in_jdk_7

I go back and forth on tuples. However, and interesting point has been brought up on the Java Posse recently. If the overhead of creating basic data classes is lowered then it makes the need for tuples less since you can just create a quick, nicely named and typed data object. In Scala there is the Case Class and project Lombok is trying to do something similar in Java with @Data. In fact, if properties were added it would solve most of this (though hashcode and equals would still need to be attended to).

Mikhail

Looks almost like C#. Check it ourself: http://www.25hoursaday.com/CsharpVsJava.html

@Rahul: I’m using currently Scala for everything I start.

I estimate 80% of companies can’t or won’t switch from Java to Groovy or Scala for various reasons – including developers who don’t want to switch, remember the blogosphere is a miniscule part of developers. Often there is no managagment buy in or sometimes costs are too high. AND currently Groovy and Scala tools (IDE in particular, but code coverage, static code analysis etc) are sub-par.

If you want this and can switch, sure you can use Groovy or Scala.

@Mikhail: I know. Most won’t use C# because C# is Windows only.

@lbertrand: See what I said about Scala and Groovy. Otherwise I will take a look :-)

@Ben: I agree, and I still don’t follow the FP few of no-objects just lists/tuples. If needed, a class is always the better choice. But sometimes a tuple works. @Data might – as you’ve said – solved this too.

@MaggieL: See my comments about Scala / Groovy.

Mirko

Well, I use the default package protected scope in Java rather often for unit testing (tests are in the same package as the SUT). While I agree that public for methods could be a default, I do not want to loose the default scope, maybe a new keyword like “pprotected” would do the trick then.
Coming from a python background, tuples would be fine.

Regards
Mirko

Rick

Stephan, C# is obviously NOT Windows only.

@Rick: It isn’t? Perhaps things changed, but everytime I take a deeper look into alternative C# implemtations I read about slow and memory leaking GCs/VM, about bugs, about incomplete and incompatible APIs, about implementations that lag years behind MS C#. But as I’ve noted, things might have changed.

@Rick: But I really would be interested in experience reports of large scale web app deployments of C# on Linux.

Sounds a lot like Groovy. It shouldn’t be that hard to learn Groovy syntax for a Java Developer.

@Lars: As stated there might be reasons not to use Groovy, otherwise I agree with you (there are several Groovy posts from 2007 on this blog ;-)

And I assume quite a high percentage of Java developers do not want to change – they see no reason. The blogosphere is a niche.

@Lars: Oh and I love a lot of things about Groovy :-) (but use Scala)

There is a project called Project Lombok in which getter and setter methods are created through annotation on the member fields. If you use Eclipse as your favourite IDE, you can see right from the Outline view the Getter/Setter methods that were generated.

Funny that you don’t (and no one) insist on having final methods by default. We had a big discussion about this at work. “Effective Java” insists on “design for inheritance” otherwise mark your methods as final. I personally see little added value in doing this (whereas the sealed keyword in Scala adds lots of value for pattern matching). What’s your take on that?

Eric.

josh

> the FP few of no-objects just lists/tuples.

If you use Scala you should know it’s not about ‘no objects’. For shame!

@Rafael: See my Magical Code Post, I’ve mentioned Lambock there

http://codemonkeyism.com/beware-magical-code/

@Eric: I’m very careful with inheritance, so I’m not really needing final methods – they could help though (I rarely inherit, mostly compose)

@Josh: Scala is FP/OO and most Scala enthusiasts are too, otherwise they would use Clojure. So I didn’t mean Scala with “FP view”

The blog title should be “Visual Beginner features I’d like in a language that targets the JVM”

@Dan: Care to elaborate?

I agree with almost your entire list, except for the humble semicolon. As much as I like Scala, I do not like the games of “let’s see if we can omit a few more tokens here and there” and “oh look, if we remove this line ending, the meaning changes completely”.

Why do we use punctuation in English? You don’t speak commas and periods, but you still write them, and it helps us read faster.

THEN AGAIN THE ROMANS DIDN’T THEY JUST WROTE LIKE THIS WITHOUT ANY PUNCTUATION MARKS NOTHING WRONG WITH THAT OF COURSE YES I KNOW THEY WROTE IN LATIN BUT YOU GET THE IDEA CHEERS CAY

@Cay: But in Java we use newlines to denote new “sentences”. If we would write each sentence in a new line in English, we would not need “.”

[...] Creating Quality Code Every Day Getting a GWT Chat app with Comet running in less than 3 minutes How Java needs to become cleaner TDD: Consistent test structure 5 Ways to Think Wisely in Development Temporary Code, Sustainable [...]

Get rid of methods entirely and replace with fields and closures.

Leave a Reply

What people wrote somewhere else:

http://twitpic.com/19z6hv – 5 star?

This comment was originally posted on Twitter

New blog post “How Java needs to become cleaner” http://bit.ly/aNabXp – what do you think?

This comment was originally posted on Twitter

RT @codemonkeyism: New blog post “How Java needs to become cleaner” http://bit.ly/aNabXp – what do you think?

This comment was originally posted on Twitter

+1 Code Monkeyism: How Java needs to become cleaner http://bit.ly/aBaUtZ (Will we have to wait until java10 to see a cleaner java?)

This comment was originally posted on Twitter

RT @codemonkeyism Code Monkeyism: How Java needs to become cleaner http://bit.ly/aNabXp

This comment was originally posted on Twitter

RT @codemonkeyism Code Monkeyism: How Java needs to become cleaner http://bit.ly/aNabXp

This comment was originally posted on Twitter

How #Java needs to become cleaner http://bit.ly/c2W4hx

This comment was originally posted on Twitter

1 vote for properties and Tuples. RT @codemonkeyism: New blog post “How Java needs to become cleaner” http://bit.ly/aNabXp – what do you…

This comment was originally posted on Twitter

Nice java read #programming #dev | How #Java needs to become cleaner http://goo.gl/9xVv

This comment was originally posted on Twitter

RT @codemonkeyism New blog post “How Java needs to become cleaner” http://bit.ly/aNabXp – what do you think? +1

This comment was originally posted on Twitter

How Java needs to become cleaner – http://su.pr/2U0TFt

This comment was originally posted on Twitter

How Java needs to become cleaner http://codemonkeyism.com/java-cleaner/

This comment was originally posted on Twitter

How Java needs to become cleaner http://bit.ly/bdtmSy

This comment was originally posted on Twitter

RT @Tordf: Nice java read #programming #dev | How #Java needs to become cleaner http://goo.gl/9xVv

This comment was originally posted on Twitter

RT @codemonkeyism Code Monkeyism: How Java needs to become cleaner http://bit.ly/aNabXp

This comment was originally posted on Twitter

How Java needs to become cleaner: http://bit.ly/c2W4hx #programming #tech

This comment was originally posted on Twitter

http://bit.ly/c2W4hx Code Monkeyism: How Java needs to become cleaner: http://bit.ly/c2W4hx

This comment was originally posted on Twitter

RT @codemonkeyism: New blog post “How Java needs to become cleaner” http://bit.ly/aNabXp – what do you think?

This comment was originally posted on Twitter

How Java needs to become cleaner http://codemonkeyism.com/java-cleaner/

This comment was originally posted on Twitter

RT @codemonkeyism Code Monkeyism: How Java needs to become cleaner http://bit.ly/aNabXp

This comment was originally posted on Twitter

RT @codemonkeyism Code Monkeyism: How Java needs to become cleaner http://bit.ly/aNabXp

This comment was originally posted on Twitter

How Java needs to become cleaner – http://is.gd/b6WTK

This comment was originally posted on Twitter

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?