File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "number of parameters" Watch "number of parameters" New topic

number of parameters

anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
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 ??
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.

SCJP Tipline, etc.
Selvan tiru

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.
Ken Manohar

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.
Michael Matola
Ranch Hand

Joined: Mar 25, 2001
Posts: 1793
I once came across the quip (probably in someone's signature) that if your method has 10 parameters, you've probably forgotten a few.
I agree. Here's the link:
subject: number of parameters
It's not a secret anymore!