File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

number of parameters

 
anil bisht
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Selvan tiru
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is more of a programming guide lines
than real limitation on the number of parameters
a method can receive.
--Selvan
 
Ken Manohar
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1796
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I once came across the quip (probably in someone's signature) that if your method has 10 parameters, you've probably forgotten a few.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic