Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code indentation - anonymous class

 
Mike Ngo
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
Sam Codean
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic