aspose file tools*
The moose likes Cattle Drive and the fly likes Following the Javaranch programming style guide.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Following the Javaranch programming style guide...." Watch "Following the Javaranch programming style guide...." New topic
Author

Following the Javaranch programming style guide....

Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
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 ]

John Bonham was stronger, but Keith Moon was faster.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

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.


Please ignore post, I have no idea what I am talking about.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Moving to the Cattle Drive forum


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
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

Joined: Jul 22, 2000
Posts: 9053
    
  12
Each time you have an opening curly brace '{', you need to indent the code on the line that follows (4 spaces).
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
*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 ()
    Ranch Hand

    Joined: May 08, 2001
    Posts: 1932
    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 ]
  • Pauline McNamara
    Sheriff

    Joined: Jan 19, 2001
    Posts: 4012
        
        6
    Welcome to the Coddle Drive, a generous place for those that may come after you.
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Thomas Paul
    mister krabs
    Ranch Hand

    Joined: May 05, 2000
    Posts: 13974
    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)


    Associate Instructor - Hofstra University
    Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Following the Javaranch programming style guide....