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

Following the Javaranch programming style guide....

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a indentation query....
I have a nested if statement, and am wondering just how this is covered, in terms of spacing and indentation.
Below is a code snippet:-

Is this the correct way to indent it, according to the style guide?
[ edited to help preserve the indenting using the [code] and [/code] UBB Tags -ds ]
[ January 23, 2003: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use the *code* tags to show indentation. It's that button below the window in which you enter you post, and it says "code". Place your code inside of the tags.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve means the CattleDrive style guide.
Each statement indent is 4 spaces. You may need to
tweak you editor to generate tabs of 4 spaces. That is it must not put tab characters in, but 4 real hard space characters.

Also, if you have a long line which goes too far over to the right, then split it at a convenient place and indent the next line by eight spaces.

And, Steve, notice the other spaces I have sprinkled around in the code, they are also required by the Style Guide.
[ January 23, 2003: Message edited by: Barry Gaunt ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Cattle Drive forum
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, Steve, notice the other spaces I have sprinkled around in the code, they are also required by the Style Guide.
Some are required, but not all. The Style Guide requires that identifiers be surrounded with spaces (subject to a few exceptions) - but it says nothing about putting space elsewhere. A line like

is perfectly legal according to the guide. Only "a" and "b" are identifiers. The nitpicker may well have something to say about this code, but that's a different issue.
My example is a bit extreme, but there are other examples in the Style Guide where numeric and String literals are not surrounded by whitespace. (Not keywords though.) Usually people put the additional spaces in so it looks more balanced - but it's not actually required.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
[b]
is perfectly legal according to the guide. Only "a" and "b" are identifiers. The nitpicker may well have something to say about this code, but that's a different issue.


*cough* parenthesis to help clarify precedence *cough*
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each time you have an opening curly brace '{', you need to indent the code on the line that follows (4 spaces).
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*cough* parenthesis to help clarify precedence *cough*
Well, OK. I was thinking more along the lines of
  • minimal variable names a and b are poor examples of self-documenting code
  • [a == "foo"] should quite probably be [a.equals("foo")] (though maybe not)
  • why not use the more common short-circuiting || rather than |?
  • But if we want to coddle those who don't remember precedence, we can add some parentheses as well.
    [ January 26, 2003: Message edited by: Jim Yingst ]
     
    jason adam
    Chicken Farmer ()
    Posts: 1932
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Jim Yingst:
    *cough* parenthesis to help clarify precedence *cough*
    Well, OK. I was thinking more along the lines of

  • minimal variable names a and b are poor examples of self-documenting code
  • [a == "foo"] should quite probably be [a.equals("foo")] (though maybe not)
  • why not use the more common short-circuiting || rather than |?
  • But if we want to coddle those who don't remember precedence, we can add some parentheses as well.
    [ January 26, 2003: Message edited by: Jim Yingst ]

  • Agreed, but so is using int i = 0 in for loops for that matter, but I digress
  • In regards to Strings and the way the JVM handles these objects, using == and .equals will on many occasions yield the same result, so I didn't feel like bringing that up
  • Because if I wanted to address that, then I would have to put forth more effort than I felt at the time, like to state "| is more often used for binary operations, whereas the logical || is used for better optimization of conditional testing" or something like that (see, look what you made me go and do)
    When it comes down to reading code, I would much rather see parenthesis used in relatively complex conditional tests than have to figure out exactly what the previous coder was trying to do. You could write any assignment in a single line of code and I would eventually be able to figure out what is going on, but I wouldn't like you much afterwards It's not a factor of coddling, it's more about being generous to those that may come after you.
    Of course, this was my meager attempt at being humorous, and look where it got me...
    [ January 26, 2003: Message edited by: jason adam ]
  •  
    Sheriff
    Posts: 4012
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Coddle Drive, a generous place for those that may come after you.
     
    Jim Yingst
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    mister krabs
    Posts: 13974
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have to admit that I am a coddler when it comes to parenthesis. I always assume that the person looking at my code will not recall the order of precedence. So if it isn't in left to right precedence I will always use parens:
    5 + (3 * 10) // parens completely unnecessary but I'll put them in anyway
    3 * 10 + 5 // parens not needed and I'll probably leave them out (although I might put them in anyway)
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic