[Revised Opinion] JDK1.5 not so scary… but update those coding standards

# 2003-06-17 09:50:34 -0400 | Java | 3 Comments

In a previous entry, I issued a little doom-and-gloom regarding the increased complexity of JDK1.5.

In short, I’m saying “get ready”. Java is getting harder. You use Java. Java is getting harder *for you*.

Alan has done some hacking on the JDK1.5 early-access, and says:

Enhanced for loop is useful.

The generics enhancements are way cool.

[With "variance"] more things ‘just work’.

Frankly, programmers will need to be discouraged from writing generic classes. … The newly genericised java.util collections classes should be all the generics most of us need, most of the time.

‘The Masses’ will find their day-to-day coding simplified

Actually, I like the message that I think Alan is getting across. Okay, so maybe there is a little more rope with which to hang yourself, but maybe this allows more room for simplicity when each feature is used appropriately by the right people. Maybe I should change my statement to:

JDK1.5 is giving tools to the API/library writers1 to make it easier for them to make life easier for us2.

On coding standards

No matter how you feel about the new Java features, you should think about updating your coding standards. Sure, we are going to need rules about where whitespace goes around generic’s < and > etc., but that’s not going to be the important bit.

We need to think about some rules that ensure Java stays simple to use (for people on both sides of the Smart/Masses boundry). For example, some C++ project have a rule something like “Only overload operators for ‘numeric’ types”. This is an attempt by those projects to make life easier for the Masses; curtailing the wanton excesses of any Joe Hackers.

So what coding guidelines might be called for with the new JDK 1.5 stuff? Some quick ideas3:

  • static import with .*. Only use it for java.lang.Math. Otherwise, import each method as needed. Never import enum constants (*shrug*).
  • Always use the ‘for each’ loop when simply iterating over a collection or array, never use the old ‘for’ syntax.
  • Avoid creating new generic classes. New generic classes are acceptable if they are some sort of ‘container’ class (like a Pair or a Triplet, etc.).
  • Some guidelines about ‘variance’ here, something like: Always ensure Pair<Integer> can be assigned to a Pair<Number>.
  • Avoid adding methods to enum classes. Except for “formatter” methods (just made that exception up, don’t know enough about enums yet).

1 Who should be the proverbial ‘Smart People’.

2 Us unwashed ‘Masses’.

3 These are just to get you brain ticking, we’ll all need some real-world experience with JDK1.5 before good coding standards are formed.

3 Responses to this entry:

  1. Neal Gafter Says:

    Coding standards for variance (now wildcards). One big one: don’t use them in return types just to express readonlyness of the result. That will force your client to store the result using a wildcard, which will force him to learn about them. Instead, reserve wildcards for the places where they simplify the signatures in an API.

    This principle was applied liberally between the 2.0 and 2.2 prototypes, with the result that the 2.2 prototype APIs are much easier to read and use than any previous version. I think if you look at them and try them (and just glance at the new syntax for wildcards - it’s intuitive!) you’ll be very pleased.

  2. Matt Quail Says:

    Yeah, I certainly like the "?" wildcards. <? extends Number> makes a whole lot of sense.

  3. Aditya Says:

    jdk1.5 shifts the responsibility of writing the boilerplate code from the programmer to the compiler. So it
    far sure that chances of mistake would be less.
    New features like generics will take some time for adjustment of developers especially when mix of legacy
    and new code will happen.
    Enhanced for loop and Autoboxing/unboxing looks cool.
    Typesafe enums, Static import and up to some extent generics looks like as if java is moving towords
    its parent C++ :)

Leave a Reply

Click here to leave a reply