aspose file tools*
The moose likes Java in General and the fly likes Formatting Java Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Formatting Java Code" Watch "Formatting Java Code" New topic
Author

Formatting Java Code

Debbie Dawson
Ranch Hand

Joined: Aug 31, 2010
Posts: 30
How do you format your Java code...

Do you use Tabs or Spaces to indent?

And by how many positions do you indents?

1 position?
2 positions?
4 positions?
Other?

I think a standard line is 80 characters, so I'm starting to wonder if using a 4-position indent is too much?!

(It is easier to read than a 2-position indent, but also seems to waste space?!)

I am using NetBeans as an IDE, and it allows you to change whether us use Tabs or Spaces to indent, and by how much you indent.

What do you do and why?

Thanks,



Debbie

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I use tabs. I am aware that if someone has another tab-width then I have that the code can get messy.
I prefer tabs because then I only have to press backspace once instead of 4 times and I find it easier
to format with it e.g. oeps I used 3 spaces instead of four. I'm also aware that ctrl+backspace solves
my first issue with spaces but that doesn't always work like I want it to work.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I use tabs set to 4 spaces but also have a line length of 120 characters.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Tabs are evil. IDE/editor handles all that anyway, and the headache of various alignment issues go away when you just stick with spaces.

Four spaces per tab. I'm also at 120 chars per line, but I get twitchy when I hit 90... but wrapping at 80 in Java is a recipe for unreadability.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

Cross posted
http://www.java-forums.org/advanced-java/32607-formatting-java-code.html


luck, db
There are no new questions, but there may be new answers.
Debbie Dawson
Ranch Hand

Joined: Aug 31, 2010
Posts: 30
David Newton wrote:Tabs are evil. IDE/editor handles all that anyway


Handles what anyways?!


, and the headache of various alignment issues go away when you just stick with spaces.


What alignment issues?


Four spaces per tab. I'm also at 120 chars per line, but I get twitchy when I hit 90... but wrapping at 80 in Java is a recipe for unreadability.


Why 120 characters?

Writing verbose code?

I thought 80 was the standard for nearly forever?



Debbie

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Tabs have no place in code. The only real "rules" I have for my staff regarding formatting are "use real words for identifiers" and "no tabs!".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9

Yeah, but that one's about PHP code. Totally different question.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What alignment issues?

Try making multi-line statements align consistently with tabs or a mix of spaces and tabs, across environments/apps.
Why 120 characters?

Because it's Java: trivial lines can still go over 80 chars. Java is *inherently* verbose.
I thought 80 was the standard for nearly forever?

And when we had 80x24 terminals, it made sense. Now we don't. An 80-char limit is like cutting off the ends of your roast because your great-grandmother's pan was too small.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Mike Simmons wrote:

Yeah, but that one's about PHP code. Totally different question.

Indeed--there's *no* way to make PHP code look good regardless of indentation.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Debbie Dawson wrote:
David Newton wrote:Tabs are evil. IDE/editor handles all that anyway


Handles what anyways?!
Converting tabs to spaces.

, and the headache of various alignment issues go away when you just stick with spaces.

What alignment issues?
If you use a different editor, the tabs may appear different sizes.
Four spaces per tab. I'm also at 120 chars per line, but I get twitchy when I hit 90... but wrapping at 80 in Java is a recipe for unreadability.


Why 120 characters?

Writing verbose code?
I thought 80 was the standard for nearly forever?
Debbie
You can usually get a lot more than 80 columns onto a screen, but it is a good thing to have a limit as a convention, and stick to it.
120 is all right when you use an IDE or editor with no real limit to width. If, however, I am writing FORTH and have to copy-and-paste
onto a terminal with 80 columns, I occasionally get wrapping problems which the compiler misinterprets as errors if my comments are more than
80 columns wide.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Debbie Dawson wrote: . . .
1 position?
2 positions?
4 positions? . . .
One of our Readers insists on 3
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Campbell Ritchie wrote:If, however, I am writing FORTH and have to copy-and-paste[...]

Good point; the environment *does* matter, and white-space-sensitive languages make a difference.

(Although I almost never had wide lines in Forth--perhaps a consequence of one system having a 40-char terminal, or my general short attention-span.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
. . . and FORTH is a whitespace-sensitive language.
Debbie Dawson
Ranch Hand

Joined: Aug 31, 2010
Posts: 30
Mike Simmons wrote:

Yeah, but that one's about PHP code. Totally different question.


It's not a sin to get different people's opinions on things. (Thank you very much!)


Debbie
Debbie Dawson
Ranch Hand

Joined: Aug 31, 2010
Posts: 30
David Newton wrote:
Mike Simmons wrote:Indeed--there's *no* way to make PHP code look good regardless of indentation.


Tell me how you *really* feel, David...


Debbie
Debbie Dawson
Ranch Hand

Joined: Aug 31, 2010
Posts: 30
Campbell Ritchie wrote:


Don't lines automatically wrap in IDE's (even with Tabs)??


Debbie

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

Mike Simmons wrote:

Yeah, but that one's about PHP code. Totally different question.


I take it you didn't read the thread title on the other forum.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

Debbie Dawson wrote:
Mike Simmons wrote:

Yeah, but that one's about PHP code. Totally different question.


It's not a sin to get different people's opinions on things. (Thank you very much!)


Debbie

No, but you should BeForthrightWhenCrossPostingToOtherSites
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Darryl Burke wrote:
Mike Simmons wrote:

Yeah, but that one's about PHP code. Totally different question.


I take it you didn't read the thread title on the other forum.

:sigh:

Yes, I did. I was making a joke. Lighten up.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Debbie Dawson wrote:
Campbell Ritchie wrote:


Don't lines automatically wrap in IDE's (even with Tabs)??


No. And if it did, you'd still have to deal with alignment issues.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Debbie Dawson wrote: . . . Don't lines automatically wrap in IDE's (even with Tabs)??
No, as David has said. You may get them displayed wrapped, but that would differ from IDE to IDE, and you would still get that problem when displaying on a text editor, with code tags, on paper, etc.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

There's one big advantage to tabs. I used to work very closely with another developer. I have a strong preference for 4-space indentation. He had a strong preference for 2 spaces. So we used tabs, set up our editors accordingly, and always got the view we wanted.

I usually use spaces nowadays, but you simply can't solve the above problem with spaces - tabs make it trivial.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Matthew Brown wrote:. . . I have a strong preference for 4-space indentation. He had a strong preference for 2 spaces. . . .
I am surprised your company didn't have a policy about tabs and spaces.
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
I think
ALT+SHIFT+F

will do the basic formatting....



This will help for the further settings with Netbeans IDE


SCJP 1.5
"A candle looses nothing by lighting another candle"
itechmentors.com
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Campbell Ritchie wrote:
Matthew Brown wrote:. . . I have a strong preference for 4-space indentation. He had a strong preference for 2 spaces. . . .
I am surprised your company didn't have a policy about tabs and spaces.

You've never worked in a University then?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Matthew Brown wrote:I usually use spaces nowadays, but you simply can't solve the above problem with spaces - tabs make it trivial.

Yeah, actually you can.

But that still doesn't solve the issue of mixed tab/space alignment.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

David Newton wrote:
Matthew Brown wrote:I usually use spaces nowadays, but you simply can't solve the above problem with spaces - tabs make it trivial.

Yeah, actually you can.

Really? How?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Re-format it: convert to required format on SCCS checkin/checkout, or on editor load/save, or whatever. Most dev tools can be set up to do this automatically.

It still won't handle the real issue (mixed-mode alignment), but it allows crazy 8-spaces-per-tab people to not screw up everybody else's day.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

David Newton wrote:Re-format it: convert to required format on SCCS checkin/checkout, or on editor load/save, or whatever. Most dev tools can be set up to do this automatically.

It still won't handle the real issue (mixed-mode alignment), but it allows crazy 8-spaces-per-tab people to not screw up everybody else's day.

OK, I suppose I meant "how, without continually reformatting". Automating it would be OK, though.

I can see that mixing spaces and tabs is a nightmare. But the solution seems to be to use one consistently. If my colleague had been a crazy 8-spaces-per-tab person I wouldn't have even noticed, because I'd see them displayed as I want them.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That the point, though--with mixed-mode alignment you *can't* make it work correctly. Consider the various ways one can align multi-line statements. Aligning solely on tab-boundaries doesn't always provide readability; there are lots of situations where you want n-tabs + n-spaces in order to have things line up attractively (or semantically). If tab settings change that flies out the window and is impossible across environments. Reformatting can solve that problem, but it's irritating, hence spaces.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Matthew Brown wrote: . . . You've never worked in a University then?
We have a very strict indentation policy here at Teesside University. Everybody must have a different indentation policy from everybody else. If two people use the same indentation policy, there is a £20000000 budget cut and half the department are offered early retirement!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting Java Code
 
Similar Threads
Join or Not Join?
NX: Coding Standards
overloading main()
Coding conventions
Files from a directory is not being recognized