Demonick
Demonick

Reputation: 2126

Java formatting style

Is the formatting below some kind of convention in Java? I personally found it more difficult to read, than the second example. Am i alone here? And is there a way to make such a custom formatting in NetBeans when choosing Source -> Format?

public boolean handleEvent(Event e) {
    if (e.target == quit) System.exit(0);
    else if (e.target == parent) {
        // Code here
        // Bug workaround
        }
        catch (IOException ex) { infoarea.setText("I/O Error"); }
        return true;
    }

(second example)

public boolean handleEvent(Event e) 

   {
      if (e.target == quit) System.exit(0);
      else if (e.target == parent) 

          {
            // Code here
            // Bug workaround
          }
      catch (IOException ex) { infoarea.setText("I/O Error"); }
      return true;
   }

Upvotes: 1

Views: 3266

Answers (6)

BalusC
BalusC

Reputation: 1108762

Wikipedia has a whole article about indentation style. Oracle has setup the Java coding conventions and a style guide PDF. It's true that it's a matter of taste, but Java-specific guidelines are there so that all the Java code is as quick and good understandable and maintainable by every other Java developer. I hate it to see C# style in Java code, but the other way round, I also hate it to see Java style in C# code. Use Java style for Java code and C# style for C# code. It keeps everything clear for everyone.

Your example doesn't compile, but assuming that there's really no try, here's how one would do it the clean Java way:

public boolean handleEvent(Event e) {
    if (e.target == quit) {
        System.exit(0);
    } else if (e.target == parent) {
        // Code here
        // Bug workaround
    } catch (IOException ex) { 
        infoarea.setText("I/O Error");
    }
    return true;
}

The first style you presented look much like banner style and the second much like GNU style (which is a bit similar to Allman / C# style.

Upvotes: 8

OscarRyz
OscarRyz

Reputation: 199234

The Java coding convention started by Sun and now continued by Oracle say:

If's should always use braces and the open brace should be in the same line as the declaration

Again, these are only conventions, not compilation rules. If your team chooses different, the most important thing is everyone follows it ( even if it's a style you don't like ). But it always will be easier for someone new to pick into the coding style, if the preferred by Java is used.

One exception I use, with if's is the if/try idiom.

Modifying your code would be like this:

public boolean handleEvent(Event e) {
    if (e.target == quit) { 
        System.exit(0); 
    } else if (e.target == parent) try { // if/try idiom
        // Code here
        // Bug workaround
    } catch (IOException ex) { 
        infoarea.setText("I/O Error"); 
    }
    return true;
}

About netbeans configuration, I think the option is in :

"Tools / Options, click Editor icon and select Formatting tab, change braces placement."

But, I don't have a netbeans at hand right now.

Upvotes: 1

Stephen
Stephen

Reputation: 6087

Although the given code doesn't compile, I assume you are talking about using opening braces on the same line as the class/method/command, as opposed to taking a new line before using them?

If so, It's really a totally subjective thing - I personally hate having my braces on a new line; it looks as wrong to me as same line ones do to you. The only important thing is to make sure that if you're working in a team, you're all sticking to the same conventions - there's no real right/wrong for this matter.

Upvotes: 3

Bivas
Bivas

Reputation: 1494

Use the Java code convention to configure your IDE style http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

Upvotes: 2

Rob Hruska
Rob Hruska

Reputation: 120296

I think the formatting in both examples is hard to read, and against most conventional styles.

Upvotes: 2

David Hedlund
David Hedlund

Reputation: 129792

I'll assume you're talking about whether or not to place { at a new line or not, and disregard the fact that there's a catch without a try in there.

Yes, the former is definitely convention in Java, while the latter is convention in C# (although your indentation looks a bit off in both examples).

I use both (in their respective languages) and once you get used to it, I don't really think there's much difference in readability. That's subjective, though, the answer to your question is simply "Yes."

Upvotes: 1

Related Questions