Code line wrapping - how to handle long lines

corsiKa picture corsiKa · Sep 16, 2010 · Viewed 142.1k times · Source

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!

Answer

Anon picture Anon · Sep 16, 2010

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

Map<long parameterization> longMap
    = 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.