• 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

code indentation - anonymous class

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the proper identation for this code?

menuItem = new JMenuItem(new AbstractAction("Exit") {
public void actionPerformed(ActionEvent e) {
shutDown();
}});
[ November 04, 2006: Message edited by: Mike Ng ]
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeroen,

Can you "support"/"prove" your indenting suggestion with a Sun standard?

I tried applying Sun's coding standards... http://java.sun.com/docs/codeconv/html/CodeConventions.doc3.html#248)
...to Mike's example and it 'aint so pretty. :-)

Yours does look nice, but you're not indenting 8 spaces...so I was wondering how you got to what you got.

Cheers,

Simon
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Sun's standard is less than clear on this point, but my best guess is that either of these is valid:


This indents everything after the first line by 8 spaces, and then adds an additional 4 and 8 spaces of indentation to the method signature and method body, respectively.

My own personal preference, when not forced to follow Sun's standard, would be to do what Jeroen did in his first example. However I don't believe that's supported in the standard. Whether or not it's something they'd penalize you for in the SCJD, I don't know.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel that Sun should not be that strict in evaluating the code conventions.
i do understand that they might need the indentation to be Java like and not like VB
what i mean is
function()
{
}
is not allowed the preferred way is
function() {
}

Apart from this the basic indentation is necessary other than that it overhead. Like for example it is stated that the public and private methods areto be grouped at top etc. i do not follow that religiously. Sometimes it makes more sense to add public and its supporting private methods contiguously
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The standard doesn't say you MUST use 8 spaces.
In fact it only says you SHOULD use 4 spaces (not tabs) with tabs set to 8 spaces (though you should not use them?) unless that makes for code that's hard to read in which case 8 spaces should be used instead.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic