views:

72

answers:

3

I'm facing a particular line that is 153 characters long. Now, I tend to break things after 120 characters (of course, this is heavily dependent on where I am and the local conventions.) But to be honest, everywhere I break the line just makes it look bad. So I'm looking for some ideas on what I should do for it.

Here's the line:

private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper = new HashMap<Class<? extends Persistent>, PersistentHelper>();

I'm open to both ideas about how/where to break the line (and why), as well as ways to shorten the line itself.

We're not a Java shop, and there aren't local conventions for this sort of thing, or obviously I would simply follow them.

Thanks!

+1  A: 

IMHO this is the best way to write your line :

private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper =
        new HashMap<Class<? extends Persistent>, PersistentHelper>();

This way the increased indentation without any braces can help you to see that the code was just splited because the line was too long. And instead of 4 spaces, 8 will make it clearer.

Colin Hebert
+1  A: 

Uses Guava's static factory methods for Maps and is only 105 characters long.

private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper = Maps.newHashMap();
Willi
+1 Very good idea. If you don't want the additional dependency, it is very easy to write such a static factory on your own.
Helper Method
I don't think using a library to get shorter lines is a good idea.
Carlos
I would not recommend including it for that purpose, but many projects already use it. And as Helper Method pointed out, it's a one liner which saves some key strokes.
Willi
An interesting idea, obviously that method was introduced for exactly this problem.
glowcoder
+1  A: 

In general, I break lines before operators, and indent the subsequent lines:

Map<long parameterization>
    = new HashMap<ditto>();

String longString = "some long text"
                  + " some more long text";

To me, the leading operator clearly conveys that "this line was continued from something else, it doesn't stand on its own." Other people, of course, have different preferences.

Anon
And as a related comment: wouldn't it be nice if Java had typedef?
Anon
glowcoder
I agree with the break before operators except with the assignment operator, I find it harder to read when your `=` is on the second line. And soon Java will have the Diamond operator :)
Colin Hebert
I guess when I consider the two, I prefer it before even for `=`. I suppose that's just a matter of taste, but when I look at it, it makes the most sense.
glowcoder