Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Style: order of fields, params and methods

Thread view: 
Karsten Wutzke - 15 Aug 2007 14:50 GMT
Hello!

When programming I keep switching the order of declared fields, method
params (especially for constructors) and sometimes methods themselves
in the class.

-> Is there a specific order recommended? <-

For constructors:

Mostly I keep ordering them in "most mandatory order first". I put
those in the param list first, probably coming off some PHP coding
where you can omit passing optional method params at the end:

function do_something($param1, $param2, $param3, $param4 = null,
$param5 = "black")
{
...
}

=> Calling do_something(1, "dfgdf", 0); is perfectly valid.

Is it better to keep "mandatory order first", use "order by topic",
e.g. group somewhat related params whether mandatory or optional. Or
another?

Karsten
GArlington - 15 Aug 2007 15:13 GMT
> Hello!
>
[quoted text clipped - 24 lines]
>
> Karsten

Declared fields and methods:
group by access rights from public to private (alphabetical order
maybe for ease of finding them later).

In java you have to declare each method for any number and types of
params you may want to use, i.e.
myMethod(String param1, Object param2, int param3){
... common processing here ...
}
myMethod(String param1, Object param2){
myMethod(param1, param2, defaultValueForParam3)
}
myMethod(String param1){
myMethod(param1, defaultValueForParam2)
}
Karsten Wutzke - 15 Aug 2007 16:42 GMT
> > Hello!
>
[quoted text clipped - 31 lines]
> In java you have to declare each method for any number and types of
> params you may want to use,

I know. The PHP thing was just the general motivation of my question -
to maybe emphasize/promote "mandatory first order".

i.e.
> myMethod(String param1, Object param2, int param3){
snipped

Karsten
Patricia Shanahan - 15 Aug 2007 15:54 GMT
> Hello!
>
[quoted text clipped - 23 lines]
>
> Karsten

For parameters, I prefer to put the most optional ones at the end,
because I may want to do multiple constructors or methods, turning them
into optional parameters.

void doSomething(int param1, int param2, int param3, Object param4,
String param5){
...
}

void doSomething(int param1, int param2, int param3, Object param4){
  doSomething(param1, param2, param3, parm4, "black");
}
void doSomething(int param1, int param2, int param3){
  doSomething(param1, param2, param3, null);
}

Even if, initially, I'm only doing the first version, putting the
potentially optional parameters last leaves the door open to making them
optional later.

I tend to group data logically, because I use Eclipse a lot and its
package explorer lists alphabetically.

Patricia
Daniel Pitts - 15 Aug 2007 16:41 GMT
> Hello!
>
[quoted text clipped - 24 lines]
>
> Karsten

I find that if I think I might want to re-order, add, or remove
parameters from a constructor or function that has a large number of
parameters already, I choose to use the Parameter Object, and have it
usually fulfill the Builder pattern as well.

class SomethingParams {
 private String param;
 private String blah;

 public SomethingParams setParam(String value) {
   param = value;
   return this;
 }

 public SomethingParams setBlah(String blah) {
   this.blah = blah;
   return this;
 }
}

doSomething(new SomethingParams().setBlah("My blah is better than your
blah").setParam(null));

You should also add a validation check (either as a method on
SomethingParams, or in doSomething) that throws an
IllegalArgumentException or some such, if the manditory arguments
aren't correctly set.

Also, at this point, you might consider whether or not doSomething
actually belongs in SomethingParams, and if SomethingParams can be
made to be more self contained.

Or, if you would pass this Param object into a constructor, use the
Factory pattern instead, and have a SomethingBuilder.newSomething(),
which calls the appropriate constructor for the client, based on the
other configuration they've done.
Adam Maass - 15 Aug 2007 16:55 GMT
> Hello!
>
[quoted text clipped - 3 lines]
>
> -> Is there a specific order recommended? <-

Eh. This is entirely stylistic, and up to each to determine.

Generally, I order the members of a class like this:

Constants (IE, static final fields)

Static variables (static, non-final fields)

Instance variables

Constructors (Generally fewer parameters to the complete list of parameters.
Generally, all constructors end up resolving to one in this class.)

Instance methods. (If I worry about order at all within this grouping, it
generally is by "topic." A grouping by topic tends to suggest an opportunity
for another class if you're into aggressive refactoring.)

Static methods, except "main."

Instance member classes

Static member classes

main method.
Oliver Wong - 15 Aug 2007 20:11 GMT
> Hello!
>
[quoted text clipped - 3 lines]
>
> -> Is there a specific order recommended? <-

   As far as I know, there's no standard (defacto or otherwise) for the
order of class members. If your programming group has a specific standard,
follow that one. Otherwise, do whatever you want?

   Personally, I don't really care how my coworkers order the members,
because my IDE can present a sorted view of them without actually
manipulating them on the disk, so I see them in the order that I want to
see them.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.