File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Program Structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program Structure" Watch "Program Structure" New topic
Author

Program Structure

Don Mega
Greenhorn

Joined: Aug 30, 2010
Posts: 7
What is the suggested way a program should be structured?
I read that attributes first then methods.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

According to Oracle's conventions for Java...
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...

Ref: http://www.oracle.com/technetwork/java/codeconventions-141270.html#16817


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

And welcome to the Ranch!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oracle is, of course, wrong on this one. Declaring variables as close as possible to their use makes it easier to spot refactoring opportunities. Maintaining vertical locality is very useful for readability and comprehension.
Don Mega
Greenhorn

Joined: Aug 30, 2010
Posts: 7
What is "refactoring"?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Try google.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

David Newton wrote:Oracle is, of course, wrong on this one. Declaring variables as close as possible to their use makes it easier to spot refactoring opportunities. Maintaining vertical locality is very useful for readability and comprehension.

I agree. The whole "declare your variables at the start of the block" is a remainder from the "old" C days, where you couldn't declare variables anywhere you wanted.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Wait... Are you suggesting there might be differences of opinion regarding "standards"?

Okay, so I don't declare them all up front either (and sometimes feel "wrong" doing that). But at least Oracle's document provides a starting point for questions like this.
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
I agree with Marc here. Even I declare the variables at the beginning of a code block, it allows me, and most of the times others, to "read" my code properly without disrupting from the flow of the program.
It might be an ancient method, however, works perfectly fine for me.


-Ninad


Don't walk as if you rule the world, walk as if you don't care who rules it...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

But it *doesn't* allow you to read the "flow" properly--that's the point. Unless your method is very, very small, you have more information to hold in your head as you read, otherwise you'll flip back up to the declarations to see the type, or to check if it's initialized, or... It also obfuscates the meaning of code chunks: if all the variables are declared at the top you have one less way of reasoning about the dependencies of the code you're reading. This reduces the ease of refactoring, in at least two ways: (1) it reduces the chances of spotting groups of functionality that could be extracted, and (2) If using an IDE to extract the method it'll pass in variables declared at the top of the method rather than having them be a local in the extracted method, creating more manual work.

And how does declaring it where it's used, which adds either (a) nothing, or (b) a single word, to the token stream break the flow?

If the method is, say, ~10 lines long, I could almost argue it's fine declaring them at the top. Almost. Mostly all it does is add unnecessary noise to method, reduces the ability to reason, and creates additional manual labor during refactoring.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

Just make sure whatever you do:

1) You do it consistently.
2) You follow whatever conventions your teacher/boss/company tells you to follow.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
fred rosenberger wrote:Just make sure whatever you do:

1) You do it consistently.
2) You follow whatever conventions your teacher/boss/company tells you to follow.


Now that I can do...and I guess as long it does not hamper the efficiency and quality you can continue as you are doing.
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Well, if you are using Eclipse, then you have an option of sorting the members. It will be found under the Source Menu. Eclipse comes with a predefined sorting order - Of Course you can modify it to match whichever standards you are following
This sorting will sort the members of a class and not the lines inside a method though. From the default configuration from sorting of Eclipse, you'll find the static members are placed before instance variables.
Roughly the structure is
static fields
static initialization blocks
static methods
instance fields
initialization blocks
Constructors
instance methods

By instance xxx I refer to the non static members of a class.


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
 
wood burning stoves
 
subject: Program Structure