aspose file tools*
The moose likes Cattle Drive and the fly likes Spacing Clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Spacing Clarification" Watch "Spacing Clarification" New topic
Author

Spacing Clarification

Brad Ford
Ranch Hand

Joined: Mar 22, 2001
Posts: 40
Hi all!
Just got nitpicked on try #2 for ass. 1b. Lots of 'Spacing!' comments. I checked the style manual pretty thoroughly and tried to follow it i.e. a space around all identifiers, space between brackets and contents, etc. The rule of thumb in the style guide is that all names should be double-clickable (is that a word???).
Anyhow, here's is a sample cut and pasted from my email:
> for ( int i = 0; i < lastLine; i++ )
spacing!
Here's the same code cut and pasted from the style guide:

* for ( int i = 0 ; i < v.length() ; i++ )
Am I missing something? Oh. It appears I am. Sporting a sheepish and embarassed look, I'm going to post this anyway. I didn't realize from the style guide that there should be a space preceding semicolons but, looking at it again, the examples clearly show, but do not explicitly state, that a semicolon should be preceded by a space for anything except a closing brace. Is that right? (side note - even when a semicolon is not preceded by a space like this;, the identifier is still double-clickable)
Also, two more clarification questions:
1. arrays are shown in the style guide as x[0], x[ i ], and x[ 0 ] but not x[i]. Does this mean that for any numeric index, spaces are optional or is the x[ 0 ] format preferred?
2. String initializations - are spaces required or not between the braces and the quotes? eg. String x = ("some string"); or String x = ( "some string ");
Thanks for the clarification.
David Junta
Ranch Hand

Joined: Dec 10, 2000
Posts: 86
Brad,
You are correct that your "mistake" was to say int i = 0; instead of int i = 0 ; However, as you'll see in the Style Guide, not ALL semi-colons are preceded by a space.
Methods, e.g., do NOT have a space before the semi-colon:
System.out.print( "Hi!" );
For arrays, the spaces within the [] brackets are optional, but you can NOT use a space before the left bracket like args [0]...it must be args[0] or args[ 0 ].
If you look at the "2.1 - Indentation" section you'll see that you don't need a space between the parens and the ".
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070

side note - even when a semicolon is not preceded by a space like this;, the identifier is still double-clickable

That may be true in the editor you are working in, but it may not be true in the editor I may be working in. We are trying to get people in the habit of making code that anyone can read, anyone can easily edit, and it will look and act the same in any text editor.
Bill
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10

the examples clearly show, but do not explicitly state, that a semicolon should be preceded by a space for anything except a closing brace. Is that right?

Not exactly. The rule is "spacing around all identifiers". That includes a space between the identifier and the punctuation (which includes semicolons and commas). There is no rule regarding semicolons and spacing.

1. arrays are shown in the style guide as x[0], x[ i ], and x[ 0 ] but not x[i]. Does this mean that for any numeric index, spaces are optional or is the x[ 0 ] format preferred?

optional unless an identifier is inside the brackets. A space is required between an identifier and a bracket.

2. String initializations - are spaces required or not between the braces and the quotes? eg. String x = ("some string"); or String x = ( "some string " );

personal preference.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
Great questions Brad. It took me multiple attempts at Java-1a and 1b before I eventually spaced everything correctly. After reading the excellent replies I have a clearer idea of what�s going on.
I had my own style before finding Java Ranch, like most people. I just submitted Java-3 and have started working on Java-4. I can't believe how much more readable my code has become. It doesn't look like shorthand anymore. It is making it easier for me to learn Java.

Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spacing Clarification