• 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

Code Formatting Question

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello fellow ranchers,
I am in the final stages of reviewing my source to reveal any incorrect coding conventions, and have a question to ask. I am running Jtest to
help find/fix all my code format errors. It has a rule that all variables
be declared at the beginning of a method. Is this a standard? I have never
heard of this before. Can anyone elaborate on if spaced out variable declarations are acceptable, or do they all need to be declared first within a particular block of code?
Thanks all for your help.
Zak Nixon
SCJP 1.4
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really do not believe that it is a rule to declare your variables at the beginning of a method. However, here is Sun's take on it:
http://java.sun.com/docs/codeconv/
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are too (or more) points of view regarding this.
On one hand, I think that Sun's coding conventions (section 6.3) say that all variables should be declared first-thing in methods .... that way it is less confusing and so forth for people coming along later for maintaining and so forth.
However, if you read Refactoring by Fowler and probably others, they indicate that variables should be defined closest to where they are actually used. (Again to be clearer, etc ...)
So beats me. I'd have to look in my code, but I'm sure I'm using a mix of the two. When there is one logical flow to a method I would most certainly group all the variable declarations at the beginning. But if there are several logical blocks of code in the method, I would probably put variables near where they are needed (if I didn't do extract-method on those blocks in the first place).
Hope that helps. I'm not sure how strict the grader will get to be with coding conventions. It's not like he has that much time to grade our exams. (That's me hoping, at least).
[ December 15, 2003: Message edited by: Nathaniel Stoddard ]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nathaniel,
I have heard that the original plan was for it to be explicitly stated that candidates must follow the Sun Coding Standard, and that it would be rigidly adhered to. However this no longer seems to be the case (thank goodness).
It is probable that there are still marks assigned to coding standards (see the SCJD FAQ on marking for a breakdown of marks in the old assignment).
Where you can loose points is if you do not apply a consistent standard. For example if sometimes use one naming convention and at other times use another. Or if your indentation changes. Or you use multiple formats for any one of the other items mentioned in the Sun Coding Standard. It is important to keep your code consistent.
Regards, Andrew
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nathaniel,

When there is one logical flow to a method I would most certainly group all the variable declarations at the beginning. But if there are several logical blocks of code in the method, I would probably put variables near where they are needed


It's not so far from what is suggested at section 6.3 of Sun's coding convention, as they say beginning of blocks, not methods.

Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "}".) Don't wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope.
void myMethod() {
int int1 = 0; // beginning of method block
if (condition) {
int int2 = 0; // beginning of "if" block
...
}
}


Best,
Phil.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page: "Code Conventions for the JavaTM Programming Language",
it says: "Revised April 20, 1999 "
Is it Sun's most recent revision?
 
Seid Myadiyev
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to my question above, could you please give a simple example, I am not sure I exactly understand what it states. Thank you in advance.
Seid
8.1 Blank Lines
Blank lines improve readability by setting off sections of code that are logically related.
-- Two blank lines should always be used in the following circumstances:
- Between sections of a source file
- Between class and interface definitions
-- One blank line should always be used in the following circumstances:
- Between methods
- Between the local variables in a method and its first statement
- Before a block (see section 5.1.1) or single-line (see section 5.1.2) comment
- Between logical sections inside a method to improve readability
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page: "Code Conventions for the JavaTM Programming Language",
it says: "Revised April 20, 1999 "
Is it Sun's most recent revision?

The version posted at
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
is the latest, yes. Ignore the date; it's not correct. I had a version I downloaded a year ago, and found some minor revisions. (I don't remember what they were, but it was different.) Sun may revise things without changing the date.
 
Seid Myadiyev
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Jim, for your reply. Could you also help me with the second question? Thank you very much!
Seid
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic