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
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.
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
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
posted
0
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