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

Spacing Clarification

 
Brad Ford
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Michael Pearson
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic