Forums Register Login

Following the Javaranch programming style guide....

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Moving to the Cattle Drive forum
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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*
+Pie Number of slices to send: Send
Each time you have an opening curly brace '{', you need to indent the code on the line that follows (4 spaces).
+Pie Number of slices to send: Send
*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 ]
    +Pie Number of slices to send: Send
     

    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 ]
  • +Pie Number of slices to send: Send
    Welcome to the Coddle Drive, a generous place for those that may come after you.
    +Pie Number of slices to send: Send
    +Pie Number of slices to send: Send
    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)
    If you look closely at this tiny ad, you will see five bicycles and a naked woman:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1189 times.
    Similar Threads
    New to java
    primality formula
    Teach Yourself : Gain Performance in 1 minutes
    Displaying rows
    Tables in Java
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 03:35:30.