Java syntax style and tool question

P

Phil Latio

First question. There seems to be two styles of writing Java code (see
below), one with open curly brackets on the same line and the other with the
open curly brackets on the line below. Does anyone know what the technical
names are for these two styles?

// Style No.1 which I hate
public MultiListener() {

// Style No.2 which I like
public MultiListener()
{

Secondly, does anyone know of a tool that converts the Style No.1 syntax to
Style No.2? I absolutely detest the Style No.1 but if ever download any
example code, it's nearly always in that style.

Perhaps it's just me but it so much easier to read and understand other
people's code if it is laid out using the Style No.2. I change it manually
at the moment but I am sure someone has written a little tool which would
save me a lot of time.

Cheers

Phil
 
M

Michael Borgwardt

Phil said:
First question. There seems to be two styles of writing Java code (see
below), one with open curly brackets on the same line and the other with the
open curly brackets on the line below. Does anyone know what the technical
names are for these two styles?

Not exactly technical, but
http://info.astrian.net/jargon/terms/i.html#indent_style

Secondly, does anyone know of a tool that converts the Style No.1 syntax to
Style No.2?

There's probably dozens, e.g.

http://www.jindent.com/java-source-code-formatter
http://jalopy.sourceforge.net/

IDEs like Netbeans or eclipse usually also have an integrated code formatter.
 
S

Sudsy

Phil said:
First question. There seems to be two styles of writing Java code (see
below), one with open curly brackets on the same line and the other with the
open curly brackets on the line below. Does anyone know what the technical
names are for these two styles?

// Style No.1 which I hate
public MultiListener() {

K&R, named after Kernighan and Ritchie, authors of "The C Programming
Language".
// Style No.2 which I like
public MultiListener()
{

Don't know. "Contemporary" perhaps?
 
J

John

Phil said:
First question. There seems to be two styles of writing Java code (see
below), one with open curly brackets on the same line and the other with the
open curly brackets on the line below. Does anyone know what the technical
names are for these two styles?

// Style No.1 which I hate
public MultiListener() {

// Style No.2 which I like
public MultiListener()
{

Secondly, does anyone know of a tool that converts the Style No.1 syntax to
Style No.2? I absolutely detest the Style No.1 but if ever download any
example code, it's nearly always in that style.

Perhaps it's just me but it so much easier to read and understand other
people's code if it is laid out using the Style No.2. I change it manually
at the moment but I am sure someone has written a little tool which would
save me a lot of time.

Cheers

Phil

I reckon (without any evidence at all) that KnR is used most widely.
This is *completely* unjustified, but I normally find that newbies use
No.2 (I don't know why). Most Sun code uses KnR except for around
classes where they use No.2.

Bizarre really.

John
 
P

Phil Latio

Not exactly technical, but

Many thanks.
http://www.jindent.com/java-source-code-formatter
http://jalopy.sourceforge.net/

IDEs like Netbeans or eclipse usually also have an integrated code
formatter.

Many thanks for those links.
- Jiindent looks very nice, a bit pricey for my budget being a student but
$49/$69 is still going to worth it as it will save me quite a bit of time. I
have downloaded and tried out the demo version, changed a couple of settings
in the Customiser part and it then does exactly what I want. Only thing
missing is an icon file for the shortcut on my desktop. Perhaps I should
make one.

- Jalopy seems to only come as plug-ins or as a command-line program.

Anyhow stuck "java source code formatter" into Google and got some others
too.
http://www.semdesigns.com/Products/Formatters/JavaFormatter.html
 
M

MERIGON Olivier

- Jiindent looks very nice, a bit pricey for my budget being a student but
$49/$69 is still going to worth it as it will save me quite a bit of time.
I

Eclipse does the job and is free...
 
P

Phil Latio

John said:
I reckon (without any evidence at all) that KnR is used most widely.
This is *completely* unjustified, but I normally find that newbies use
No.2 (I don't know why). Most Sun code uses KnR except for around
classes where they use No.2.

Bizarre really.

Two points.

I don't have the best eyesight in the world so I print/display code perhaps
larger than most. This quite often brings more wordwraps which to a novice
like me can make the syntax difficult to digest.

Second point is that when I am looking at other people's code, I feel I am
looking more at how they have implemented something or overcome a particular
problem rather than the exact syntax. I am quite often breaking Java classes
down to particular methods and they just jump out at me with style 2.

Cheers

Phil
 
P

P.Hill

Phil said:
First question. There seems to be two styles of writing Java code (see
below), one with open curly brackets on the same line and the other with the
open curly brackets on the line below. Does anyone know what the technical
names are for these two styles?

// Style No.1 which I hate
public MultiListener() {

// Style No.2 which I like
public MultiListener()
{

Phil,

As others have pointed out, there are many tools, but I think it is worth
pointing out that I also used to greatly prefer style 2 (Allman/BSD style) and
even was envolved in various internal groups which picked that style over K&R
style back when I was a C/C++ programmer. But regardless of that background I
now use the K&R form.

Why? Because you are fighting up hill. K&R used it, thus it became the
defacto C standard, thus the defacto C++ standard and when the old C/C++
programmers put together Java they kept with their old habits. I know
use K&R style, because the JDK and most other Java code is in this style.
When in Rome I'd recommend speaking Italian. When formatting Java, C or C++
you might as well get used to K&R style. Much of the world speaks English and
it's darn ugly and irregular of a language, but it's just the pragmatic way to go.

have fun,
-Paul
 
A

Alan Moore

First question. There seems to be two styles of writing Java code (see
below), one with open curly brackets on the same line and the other with the
open curly brackets on the line below. Does anyone know what the technical
names are for these two styles?

// Style No.1 which I hate
public MultiListener() {

// Style No.2 which I like
public MultiListener()
{

Secondly, does anyone know of a tool that converts the Style No.1 syntax to
Style No.2? I absolutely detest the Style No.1 but if ever download any
example code, it's nearly always in that style.

Perhaps it's just me but it so much easier to read and understand other
people's code if it is laid out using the Style No.2. I change it manually
at the moment but I am sure someone has written a little tool which would
save me a lot of time.

You might check out jEdit: http://www.jedit.org/
It has at least one plugin for reformatting code, based on the
PrettyPrint module of JRefactory: http://jrefactory.sourceforge.net/

I just want to say that I, too, prefer the second style (which is also
known as Pascal style), and use it whenever I can--which basically
means I only use on my own personal projects. It's not only much more
readable, it can also be more horizontally compact. With Pascal
style, I can use a two-column indent and have no trouble at all
distinguish scope levels; with K&R, I find a minimum four-column
indent is needed. A smaller indent, of course, means less line
wrapping, which adds even more to readability.
 
B

Brian Palmer

P.Hill said:
As others have pointed out, there are many tools, but I think it is worth
pointing out that I also used to greatly prefer style 2 (Allman/BSD
style) and even was envolved in various internal groups which picked
that style over K&R style back when I was a C/C++ programmer. But
regardless of that background I now use the K&R form.

I also once prefered the
if (B)
{
// ...
}

form, but I found that reducing the number of 'almost empty' lines
helped me in maximizing how many useful lines would fit on screen. The
main benefit I had found in the more expansive version, easier
matching of braces, was satisfied by switching to more capable editors
that would highlight or otherwise indicate corresponding delimiters.
 
A

Andy Fish

Phil Latio said:
Secondly, does anyone know of a tool that converts the Style No.1 syntax to
Style No.2? I absolutely detest the Style No.1 but if ever download any
example code, it's nearly always in that style.

I would say, get used to it. You should concentrate on what the code is
doing and regard the layout as superficial and irrelevant.

If you spend time reformatting code when you're being paid to implement a
change, you will be less efficient and also cause grief with source code
control systems (when someone diffs the files it will look like you have
made more changes than you actually have)

FWIW I have moved from style 2 to 1 over the years. I don't really know why
but I think it's because the code takes up less vertical space on the
page/screen. I would have thought you would come to appreciate this if you
use large type sizes.

Andy
 
T

Thai DANG

The name is "stroustrup indentation". I'm using it with Emacs for years now. I prefer it because the code is easier to read.
 
D

Darrell Grainger

I reckon (without any evidence at all) that KnR is used most widely.
This is *completely* unjustified, but I normally find that newbies use
No.2 (I don't know why). Most Sun code uses KnR except for around
classes where they use No.2.

The K&R style is good in reducing line count. I can fit more information
on the screen using it than using style #2. If I am doing a code review or
just trying to debug a section of code, it is nice to have it all on one
screen/sheet. A line for just a brace can quickly eat up the screen space.

In C, functions where originally formatted as:

int main(argc, argv)
int argc;
char *argv[];
{
return 0;
}

Moving the opening brace up didn't look very appealing. Additionally,
functions were the important element of C and this helped to make
functions stand out. It seems (and this is just my opinion) that Sun has
adopted the convention to make classes stand out. Additionally, in Java,
the classes tend to be something like:

class myClass
extends some.really.long.package.and.class
implements one.interface, and.possible.another.interface
{
// body
}

Style #2 is preferred by new programmers because it lets them easily match
up braces. This is especially important in C. Many C programming
environments still do not have automatic brace matching. When you compile
the code with a missing brace the error is usually something cryptic like
"unexpected end of file" associated with the last line of code.
 
D

Darrell Grainger

Many thanks.

formatter.

Many thanks for those links.
- Jiindent looks very nice, a bit pricey for my budget being a student but
$49/$69 is still going to worth it as it will save me quite a bit of time. I
have downloaded and tried out the demo version, changed a couple of settings
in the Customiser part and it then does exactly what I want. Only thing
missing is an icon file for the shortcut on my desktop. Perhaps I should
make one.

- Jalopy seems to only come as plug-ins or as a command-line program.

Anyhow stuck "java source code formatter" into Google and got some others
too.
http://www.semdesigns.com/Products/Formatters/JavaFormatter.html

Two things to concern before you get into the habit of reformatting code.
You might want to just get used to using whatever format is given you. If
you pysch yourself into needing style #2 you might have problems later.

If you are on a team and they use the k&r style, you might have to use it.
A number of developers get a little bent when you start reformatting
their code. If you have to share you will most likely have to work with
k&r style code.

Plus, managers don't like you changing the code without a good reason.
Personal preference is not a good reason. Changing the code adds risk. If
there is no benefit to the team or customers they will not allow the added
risk.

Additionally, when you start working with loops that have if/else and
switch statements you might find that the entire loop does not fit on your
screen. Trying to step through and debug a loop that causes the screen to
scroll up and down is a major pain. Many teams have rules like, "a loop
must not be greater than XX lines." Even if you are okay with the code
scrolling up and down, others on your team will not be.

Finally, if you do code inspections on large projects, you will 1) not
want to wade though too many pages and 2) will want to have as much on one
page as you can because flipping back and forth between pages is a major
pain (gets worse if the code is printed double sided).
 
P

Phil Latio

MERIGON Olivier said:
time.
I

Eclipse does the job and is free...

Do you mean Eclipse with a particular plug-in (apart from obviously JDT)?

I am currently in my first few hours on Eclipse and I've seen plug-ins that
format the souce code but nothing within what I have installed already.
Doing Source>Correct Indentation in the Java editor seems to give me Style 1
and although it's early days, I don't see a simple way of changing this.

Cheers

Phil
 
P

P.Hill

Phil said:
I am currently in my first few hours on Eclipse and I've seen plug-ins that
format the souce code but nothing within what I have installed already.
Doing Source>Correct Indentation in the Java editor seems to give me Style 1
and although it's early days, I don't see a simple way of changing this.

Looking around a bit you'll eventually find:

window | preferences
+java
+-- Code Formatter
and
+editor
+-- Templates
(note the checkbox for "use code formatter")

I believe there might be another setting to tweek.

HTH,
-Paul
 
P

P.Hill

vivienne said:
When I studied Pascal this was known as Bilberts notation

Which one?!?!

Geeze, people can we read and answer the questions!
When someone says a or b, you can't say: yes, that's me favorite.

-paul
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top