• 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

Program Structure

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the suggested way a program should be structured?
I read that attributes first then methods.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is "refactoring"?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try google.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just make sure whatever you do:

1) You do it consistently.
2) You follow whatever conventions your teacher/boss/company tells you to follow.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Always! Wait. Never. Shut up. Look at this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic