• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Formatting Java Code

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use tabs set to 4 spaces but also have a line length of 120 characters.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cross posted
http://www.java-forums.org/advanced-java/32607-formatting-java-code.html
 
Debbie Dawson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!".
 
Master Rancher
Posts: 5153
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Yeah, but that one's about PHP code. Totally different question.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:

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


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.
 
Marshal
Posts: 80520
456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80520
456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Debbie Dawson wrote: . . .
1 position?
2 positions?
4 positions? . . .

One of our Readers insists on 3
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80520
456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and FORTH is a whitespace-sensitive language.
 
Debbie Dawson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:

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


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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:



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


Debbie

 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:

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


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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Debbie Dawson wrote:

Mike Simmons wrote:

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


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
Master Rancher
Posts: 5153
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:

Mike Simmons wrote:

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


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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80520
456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80520
456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think
ALT+SHIFT+F

will do the basic formatting....



This will help for the further settings with Netbeans IDE
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80520
456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
reply
    Bookmark Topic Watch Topic
  • New Topic