Style Guide for Java 5 Generics and Annotations

S

Stanimir Stamenkov

The "Code Conventions for the Java Programming Language"
<http://java.sun.com/docs/codeconv/> doesn't state rules for
formatting newer Java 5 syntax: generics and annotations. In this
regard I've thought some may prefer a "compressed" style with these:


Map<String,String> stuff;

instead of:

Map<String, String> stuff;


@MyAnn(name="foo",data={43,234,65,76,125})
String stuff;

instead of:

@MyAnn(name = "foo", data = { 43, 234, 65, 76, 125 })
String stuff;


What's your preferred style (which may differ for the two cases)?
 
J

Jason Cavett

Clearly this is a matter of programmer preference.

I personally put spaces inside the parentheses and prior to the opening angle
bracket.  I also put spaces inside the curly braces.  I also put a space after
each comma.  I also put opening curly braces for control structures on their
own line because putting them at the end of the control statement line is just
plain goofy.

What do you think, isn't the "compressed" style harder to read?  With
everything jumbled together it makes it more difficult for the eye to pick out
syntactic breaks - white space makes it neurological instead of cognitive,
therefore much easier and faster.

If I were a code reviewer I'd ding code for lack of white space as in the
"compressed" example.

Your exact use of white space may vary, but use the principle to empower the
non-cognitive automatic neurological attributes of the eye-brain circuit, not
force people to reason through every character.

Yeah, I have to agree with Lew.

Lack of whitespace makes things harder to read for the developer.
(Typically - some whitespace is just pointless.)
 
R

Roedy Green

The "Code Conventions for the Java Programming Language"
<http://java.sun.com/docs/codeconv/> doesn't state rules for
formatting newer Java 5 syntax: generics and annotations. In this
regard I've thought some may prefer a "compressed" style with these:

I had to look to see how "I" do it. I just accepted the IntelliJ Idea
default, which is to use the space.

The appeal of taking out the space is it makes the generic type into a
single lump. You don't have to mentally parse it to conclude --
generic type runs from here to here. You can do it with your
whitespace detectors which don't tie up the computational parts of
your brain.

This is one of the beauties of Forth. All the syntactic elements are
delimited by space. You don't need any intellectual effort to
tokenise. It is one of the reasons I tend to use more space in my code
than most people do.
 
R

RedGrittyBrick

Stanimir said:
The "Code Conventions for the Java Programming Language"
<http://java.sun.com/docs/codeconv/> doesn't state rules for formatting
newer Java 5 syntax: generics and annotations. In this regard I've
thought some may prefer a "compressed" style with these:


Map<String,String> stuff;

instead of:

Map<String, String> stuff;


@MyAnn(name="foo",data={43,234,65,76,125})
String stuff;

instead of:

@MyAnn(name = "foo", data = { 43, 234, 65, 76, 125 })
String stuff;


What's your preferred style (which may differ for the two cases)?

To paraphrase/mangle the message of an esteemed contributor ...

I personally -don't- put spaces inside the parentheses and prior to the
opening angle bracket. I -rarely- put spaces inside the curly braces.
I -always- put a space after each comma.

I -never- put opening curly braces for control structures on their own
line because -I prefer- putting them at the end of the control statement
line -as I am (apparently)- just plain goofy.

-- * --

Actually I just hit Ctrl+Shift+F in Eclipse very often and accept
whatever it does. Sometimes I add // empty comments to the ends of
lines in order to force it to do line breaks where I want them in
wrapped statements. I did configure Eclipse to format source according
to "Java Conventions" rather than it's own.

I prefer my source code relaxed - not uptight!
 
S

Stanimir Stamenkov

Tue, 12 Aug 2008 17:29:42 GMT, /Roedy Green/:
I had to look to see how "I" do it. I just accepted the IntelliJ Idea
default, which is to use the space.

The appeal of taking out the space is it makes the generic type into a
single lump. You don't have to mentally parse it to conclude --
generic type runs from here to here. You can do it with your
whitespace detectors which don't tie up the computational parts of
your brain.

My point, at least for the annotations (I've included the generics
for completeness) is when I use the "compressed" style I don't have
to mentally parse them to reach "the important" part.

public void doStuff(@WebParam(name = "foo") String foo,
@WebParam(name = "bar") String bar) {
...
}

As annotations are modifiers like "public", "static", etc. I tend to
read them as "atomic" tokens and the "extra" white space causes my
brain to parse the whole of it in order to determine where the next
token starts.

public void doStuff(@WebParam(name="foo") String foo,
@WebParam(name="bar") String bar) {
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top