One common code style

As you may support, it is important to have/use one single common code style. This style should offer the best possible readability for all. So far some different code styles have been used in the xith codebase. I’ve been talking with Amos about that and so far he accepted all of my justified suggestions. I’ll sum them up here and we can discuss about them. When the majority of everybody willing to contribute to Xith3D or Xith-TK agreed to them, we can introcuce them as the common coding guidelines.

  • Braces (putting the opening-brace at the beginning of the next line):
    The advantages of putting the opening-brace at the beginning of the next line is simple to descibe but very important:
    [list]
    [li]It is much more structured, since you can easily see from position of the braces, where a block begins and where it ends (espeacially where it begins).
  • It forces you to leave a blank line between the signature and method body. Well, this is done anyway by most people. But why then not simply put the opening brace at the begining of the next line???
  • And it provides a better, nicer look of the coding, which is of course subjective.

Putting the opening-brace at the end of the signature sometimes seems to me like the search for an answer for the question “Where to put this lazy opening-brace?”. But putting it at the beginning of the next line is “using it as a tool for improving the code structure” as it is meant to be.

[/li]

  • indentation:
    Indetation must start with zero, then all lines are to be indeted (hierarchically) by 4 spaces. This also means, that no tabs are to be used, since tabs are differently displayed in different environments. And of course the empty lines are to be indeted, too. This is nothing one will directly see, but it helps a lot when adding new lines next to these empty lines.

  • line wrapping:
    By standard Eclipse wraps lines at column 80, which is somewhat old, since we nowadays have larger resolutions and are not forced to work on a 80 columned DOS screen. The automatic line wrapping whould be switched off (set to a large value like 1024). If I ever want a line to be wrapped, I’ll do it manually when I type the line. No need to wrap a line, if it has 81 columns.
    However JavaDoc comments should be wrapped by column 80, since it is readable block text, which provides a better readability if wrapped at column 80.

  • white spaces:
    The parameters of method calls should be spaced by one space after the opening bracket and one before the closing bracket like this:
    [/list]


myMethod( arg0, arg1, arg2 );

I myself needed some time to acclimate with this, but at the end it again provides an even better readability. Well, if Eclipse is configured like this, it will produce code like this:


myMethod( a( b( c( myFunction( p0, p2, p3 ) ) ) ), arg0, arg1, arg2 );

Which is a little bloated. So decide on your own, where to let eclipse format the code, and where you want to have it a little more campact. But at least when you have one braced arguments list, space it.

Return statements are to be spaced and braced, too, since it just fits into the whole system, as well as throw statements, even if Eclipse code formatter doesn’t fully support it. Do it like this:


public void myMethod1()
{
    throw( new IllegalArgumentException("bla") );
}

public String myMethod2()
{
    return( "bla" );
}

  • JavaDoc:
    Of course it is a good idea to always create JavaDoc tags, when you create a new class. Never commit an uncommented class. Try to use abstract texts, which say more than “Does XXX” for a doXXX() method, if it does more.

  • visibility:
    Try to give a class or method as less visibility as possible. This means, make a method private, if it is logically private (never used from outside). Keep in mind, that no-modifyer is not private! It is the same as protected, but is not visible in subclasses.

  • Make use of class hierarchy:
    If you want to use a Vector, HashMap, Point3f, etc., consider, if you need the additional methods the concrete class offers compared to an implemented interface like List, Map, Tuple3f. If you don’t need them, just define the variable or (more importantly) the parameter as the interface type. This gives you the advantage, that you’re free to choose the conrete class or in case of parameters gives the users the freedom to choose, which is a real big benefit.
    This is mostly already done and has less to do with code formatting, but I thought it would be good to list it here, too.

If I forgot anything, I’ll edit this posting and add it.

I created an Eclipse code formatter export file. So you don’t need to configure your Eclipse on your own. It would be very nice, if one of you NetBeans users could send me a file for NetBeans. I’ll then attach the file to this posting, too. Don’t know, if NetBeans even supports code formatting.

You can download the formatter export file from Xith3D Code conventions page.

Marvin

After nobody objected, please consider these code conventions as the official ones.

Marvin

Marvin, are you aware that at least two rules are against the sun java style coding convention ? (braces and white spaces in method signatures)

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

I can’t see why you want to use a style different from what everybody else does in thousands of project. For me, this would clearly be a show stopper for Xith, marked as “not professional” at first source code inspection.

sorry for beeing late, I forgot about this topic.

Lilian :slight_smile:

At least the opening brace code convention from sun is braindead. In fact all projects our company works on use the bracket-on-newline style. On the other hand we also prefer tabs over spaces for some reason.

I consider your post as positive provokation, since judging a project by code-style would be “upper-management-style” and regarding your posts you seem to really work for your living :wink:

hehe, I hope other users will reply now…

One thing for sure : I don’t like opening braces on new lines : too much space taken on screen for something that doesn’t carry any kind of information.
I also #Dont.like(_ these, white, spaces_) especially when using nested calls, but I could cope with it.

Lilian :slight_smile:

Modern programming is not about reducing the size of the source, nor about conserving space on 20" ultra high resolution monitors (yours for just a couple of hundred pounds right now!) - it is generally accepted that code clarity and ease of maintenance are far more important, and we can currently display around 10 times as much text as we used to back when opening brace line compression was first mooted.

I’m not saying you’re wrong, just pointing out that the compromise has to be made somewhere.

Also, my memory was that the official java code style used braces on new lines?

IIRC this one comes from old word processors and source editors where ctrl-arrow combinations worked “better” with the whitespacing (or … vice versa: they worked better with the other spacing, which is how the other spacing came to be used at all.

Can’t remember the reasoning (makes little difference to me, I reformat the code “properly” before editing ;)) but IIRC the “correct” english-language spacing is generally believed to be easier to work with - probably because it is correct, english-language, spacing, and english is currently the dominant world language. Not a great reason, but anything that makes coding that little bit less stressful when I’m working at 4am for a 6am deadline is fine by me :slight_smile:

Actually, ignore the english language spacing thing. I can’t remember if it really is or not. Will edit post later with corrections when not rushing out of the house. Sorry.

FYI official java code style uses opening braces on same line than their corresponding block declaration.

I think braces on new lines are inelegant, certainly because i’m used to 9 years of not putting them there, but also as I said, they really don’t carry information, so they don’t deserve that special treatment.

a ‘;’ after a code sentence (at the end of a line) doesn’t convey much more information, and you wouldn’t put it alone in the next line on your 20" monitor, wouldn’t you ?

Lilian :slight_smile:

it really is amazing how many time this argument comes up! :slight_smile:

why can’t people, especially on os projects, just look up the sun java recommendations and use those? then everyone knows where they are? They are even a easily selectable option in most ide’s.

Dan (<-bemused)

Yeah sure, that was Marvin idea.

And so is mine, check out the ‘official’ link I provided.

Lilian :slight_smile:

p.s. don’t take that topic too seriously :slight_smile: you and marvin are doing a great job these days on Xith. I hope I’ll be able to contribute someday as much as you two.

I also think it’s easiest just to use the Sun standard: Anyone quickly browsing through the source isn’t going to be surprised by what they see, and anyone actually working on the code can do a quick esc, ctrl+f and have it how they want.

Formatting is not a big issue, it’s more important to set rigid standards for method naming, javadoc and so on.

Incidentally, I thought the whole opening-brace-on-the-same-line thing just a wheeze to save on printing costs for K&R. It then got entrenched and java followed suit so as to ease transition. Or at least that’s what I remember reading somewhere.

I agree that formatting is less critical than API integrity…if most everyone is using Eclipse then lets use the default formatting …

Always a fun heated discussion: http://en.wikipedia.org/wiki/Indent_style

Kev

I started with Whitesmiths style, years later I switched to BSD/Allman style (because it was way easier to use in some specific editor) and finally I switched to the K&R style.

Personally I think there isnt much of a difference, but I switched to K&R, because its the standard in java. So, its benefitical for all parties if I use it myself, too.

english language spacing thing

Its not part of the language the spacing is standarized through ISO norms and its a printing thing. I pretty much only use spaces in method heads such as foo(int x, int y) and thats about it.

I certainly am ;). But at teast the one about braces is crappy. As I mentioned above, I can’t see how this one made its way into any modern standard. Nowadays we aren’t forced anymore to work on a 80x24 terminal. We have some space to spand on readability. Being objective nobody can deny, that putting BSD/Allman style is the best readable one (ignoring whet he is used to see). And putting some whitespaces after open bracket and before closing brackets in method calls just further supports/improves readability. Nevermind what Sun pronounced years ago. A modern standard (for modern displays) should look the way I described, shouldn’t it?

Everyone else ??? No. I believe, there are as many people putting the braces at the “right” position as ones putting it at the “wrong” place ;).
There’re valid aguments for all styles. But really think the one described above is the best for modern times. Not too many people will want to put code on a printer when we have enough memory to load an entire souce file (and more). As I sayed, nowadays we can spend more on readability. And readbility is absolutely improved by braces-at-the-next-line and widely degenerated by braces-at-the-same-line.

Well, every time I see code written in Sun’s standard, I have a vision of a script kiddie writing the code without knowing what he is doing ;D. Don’t feel offended. It’s just a funny idea, I have to get rid of the anger I have with code less readable because of wrong brace placement ;). – Ah! forget that ;). It sounds like I don’t respect others skilledness. Which truely is not the case, be asured.

At the end the true quality of a piece of code cannot be taken from the formatting but from the content. But a good and most imprtantly a common formatting is very important for readability.

Well, I wouldn’t say it doesn’t carry any information. It truely supports finding the beginning of a block. Certainly IDEs like Eclipse help you to even find a brace not at the same indention level. But even with such help it is way easier to find it, read and directly understand code (not only foreign code), when the opening brace is at the same intentation level as the closing one. And as I wrote above, a “blank” line after the method signature further improves readability. Many programmers do this anyway. The the brace is best fitted into this (free) place.

And please reread this about the white spaces:

Well, it’s just a question of how you define “inelegant”. Viewing it from readability-point-of-view, then putting the brace at the same line is inelegant. Viewing it from ancient-80x24-terminal-times, then braces at the next line is inelegant. Other definitions are imaginable. So as a conclusion, nowadays the letter definition is the one fitting better for “inelegant”.

Oh dear, this is something completely different :). The braces structure the code and semicola just mark the end of a statement. Usually this is the end of the line, so there’s no discussion about that. And I’ve never seen a discussion nor somebody using it like this.

Simple reason: The braces part is ugly, crappy and antiquated.

Luckily Eclipse supports to modify and export it. Then there can be a place where a “better one” can be found by everyone ;). Like it is now for Xith.

Well, when I press ctrl+f on some piece of code with an unmodified Eclipse formatter, I don’t get what I want. It is even far away from waht I want. How can that be? ;).

This certainly is as important as formatting. That’s why I put some notes about that under the formatting things ;).

I hope, I was able to clerify my intentions.

Marvin

Seems like you’ve just said:

“Yeah, there is a common standard, but I don’t like, so Xith isn’t using it.”

Or not?

Kev

PS. Antiquated is putting braces on a seperate line, one justification I’ve heard for it at Lucent Technologies (where I first started working after uni) was to minise the number of changed lines in source control when updating a conditional statement.

PPS. A common coding standard really doesn’t effect productivitiy very much. Any software engineer with an ounce of ability is going to be able to either, a) cope, adapt and just read the code, b) simply format it locally to the style they like. There are some rare cases where a really bad coding convention may hinder development (see my previous comments on military standards) but rarely does enforcing that competent engineers comply to some arbitary set of conventions invented by one of their peers actually increase productivity.

PPPS. What does the PPP stand for on these things?

PPPPS. And how many cany you have? :slight_smile:

Why exactly do you need a coding standard other than the standard Sun one? Other than the braces issue (which is a religious issue I’m not touching with a ten foot clown pole), I don’t think I’ve seen anyone do things substantially different in Java code.

Compare and contrast to C/C++ where every man and dog has their own radically different style, and even the loosest Java code looks nice and consistant. :-X

No. I think I sufficiently justified it with readability reasons, which is an important argument, isn’t it?

Readability :). And it is not religious, but important ;).

Marvin

Not really, you’ve stated your opinion - whether or not any of your conventions or the Sun one’s aid readability is pretty subjective. It would seem at least 50% of views expressed so far favor just using the Sun standards. If it were me I’d focus on making the code understandable and correct rather than worrying about inventing new standards to be questioned and criticised.

However, since it appears you (and Amos) are the only ones doing any development on Xith these days (and with things like this I can’t see it changing a great deal) I don’t suppose it really matters too much. It’s your guy’s project so why even post it, just enforce.

Kev