views:

146

answers:

6

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;
   }
+1  A: 

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

David Hedlund
Sorry, either case, the indentation and brace usage are not following [the conventions](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html). The first look much like "banner style" the second look much like C# convention.
BalusC
BalusC: While I personally agree with same-line braces, I don't think that Oracle's style-guide is the be-all-and-end-all. Coding style is very much a personal thing.
Stephen
@BalusC: Yes, it is slightly off, I made the assumption that the point of the two examples were to differentiate formatting that does and that doesn't place `{` on a line of its own, which was the biggest difference I found between the two examples.
David Hedlund
@Stephen: The question was about what the convention is, though, which is not very subjective at all, regardless of whether or not you agree that it *ought* to be the convention. I don't know whether Oracle is the perfect reference point for java convention, but it may well be, and regardless, the fact that coding style is personal shouldn't prevent us from discussing what the convention is.
David Hedlund
@Stephen: it's however the guideline for Java developers so that all Java code is as quick and good understandable and maintainable by every other Java developer.
BalusC
@Stephen coding style is only a personal thing if you're the only programmer on a project. After that, follow Oracle's style unless you all unanimously agree that a rule is both silly and that a specific other rule is better. Sun's style doesn't generally make it harder to read/write java so no point in deviating, unless you never have to share your code.
wds
@wds Oracle is not the be-all-and-end-all even in multi-person projects. In multi-person projects, one person (aka the boss or the client) typically has more money and resources than the others. The boss is the be-all-and-end-all.
emory
@emory that's not mutually exclusive with what I said, is it? If your boss or client decides on code style, chances are he's still gonna pick Sun's style (or something close to it), since that is what most developers will be able to read.
wds
+2  A: 

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

Rob Hruska
+2  A: 

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

Bivas
+2  A: 

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.

Stephen
That's the question, if programmers prefer the first example over the second, or not. I personally like the second example, cause the block where the code goes is clearly seen.
Demonick
Well, then it's entirely subjective, as I said. In my (small) experience of working, I've yet to see a strong like for one over the other really.
Stephen
+6  A: 

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.

BalusC
+1 This is both, cleaner, and more Java than the original samples
OscarRyz
And guess what, the Eclipse default formatter agrees.
BalusC
Good read, thanks.
Demonick
I added an answer to show the if/try idiom I saw in James Gosling code long time ago in Huckster https://huckster.dev.java.net/ which btw, solves the lack of "try" in the code.
OscarRyz
I already guessed something like that. I use this idiom all the time in `finally` blocks :)
BalusC
+1  A: 

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.

OscarRyz