• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variable declaration  RSS feed

 
Vlada Den
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it a better style to declare variables in the end of a class?

Eg:


What are your opinions?

Thanks,

Vlada
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why on earth would u like to do that ?...
Obviously the compiler is smart enough to read your variables while finding them in the methods. But a person reading the code will have to search for the variable declaration.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen this layout several times in code and in a couple of unofficial 'style guides'. I agree - its a pain in the rear end to read and you have to go down to the bottom just to find the type of a variable.
 
Vlada Den
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering, because I saw that in some sources...

Vlada
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vlada Den:
Is it a better style to declare variables in the end of a class?


Another data point:
Seen it, don't understand it, hate it.

One project manager that worked at the same company with me wanted his developers to order class members as follows:
A) Public fields
B) Public methods
C) Private fields
D) Private methods
(I assume there were more categories for package and protected.)

That way the public members were at the top, making them easy to find for users of the class.

If all your fields are private and all your methods are public, then you end up with the "inverted" class.

Ryan
[ April 29, 2005: Message edited by: Ryan McGuire ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We were taught to do that 20 years ago in assembler because it put all the variables together in a core dump. Don't do it any more. I sometimes see variables declared between methods, too:

Also yucky, even if not named booger.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't see much point in separating public from private parts of a class, myself. if you're reading a class' source code, as opposed to just the JavaDoc for it, you'll probably want to read the private bits too. in fact, in some of my code i've been known to put static fields at the very top, followed by private fields, then public fields, then methods in no particular order.

but then, i'm a firm believer in the goodness of JavaDoc. it might not be quite a beginner's tool — its command-line usage admittedly seems confusing to me — but once you're past the immediate beginner stage, you'll be using some IDE pretty soon anyway, and any good IDE can build the JavaDoc for you with one click of a mouse. write good JavaDoc comments everywhere, and they'll serve as all the info that mere users of your class will need, defeating the purpose of separating private fields from public ones in the source.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In early versions of the original C++ compiler Cfront, there could be problems unless all the member variables were defined first in a class. The problems were eventually, after quite a long time, resolved, so that members could be anywhere. In a very reactionary way, legions of C++ programmers started declaring their members at the end of a class. Oh, they had some random justification as to why this was better, but it's clear that they were doing it just because they finally could, not because it made any kind of sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!