Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes number of parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "number of parameters" Watch "number of parameters" New topic
Author

number of parameters

anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
hi,
Is there any guideline on the number of arguments of a method.
Actually i need a method where i need to pass 10 parameters is it advisable ??
regards
Anil
Bill Siggelkow
Ranch Hand

Joined: Jun 27, 2001
Posts: 57
I would say 10 parameters is far too many ... I try to keep the number of method or constructor arguments to 4 or less.
A lot of arguments makes a method difficult and cumbersome to use.
If you need this many arguments, it indicates that you may need to create a higher level of abstraction with your objects. You may end up creating several classes that contain the actual 10 arguments that you were passing. Chances are that they are probem-domain relationships between the arguments ... look for these relationships to identify your higher-level abstractions.


Bill Siggelkow
Jade Cove Solutions
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Often, if I find that I need to pass that much data to a method, that data is usually closely related enough that it can constitute an object in itself. Create an object that stores that data and, instead of passing 10 parameters, pass 1 object that contains that data. You can probably even find some useful behaviors for that object in managing that data.
Corey


SCJP Tipline, etc.
Selvan tiru
Greenhorn

Joined: Jul 12, 2001
Posts: 18
It is more of a programming guide lines
than real limitation on the number of parameters
a method can receive.
--Selvan
Ken Manohar
Greenhorn

Joined: Mar 16, 2002
Posts: 4
I read in Java Programming from the Beginning by K.N. King that the maximum number of parameters a method (or constructor for that matter) should be 7 as this is supposed to be the most number of things a person can remember at a time. However, while nothing is wrong, I would think 10 parameters are a lot and some changes in design may be necessary. :roll:
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I think this is really a matter or preference, but I feel that anything over 4 is getting a bit extreme. Granted, there are cases when this is required but, if you really need to pass that much data, I'd say that data can probably be grouped into an object. Then, you can just pass that object.
Corey
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1744
    
    2
I once came across the quip (probably in someone's signature) that if your method has 10 parameters, you've probably forgotten a few.
 
wood burning stoves
 
subject: number of parameters
 
Similar Threads
programming to the interface
Declarative Exception Handling
maximum number of parameters...
entrySet method Signature
call a html/jsp page from an applet and pass parameters in post method