• Post Reply Bookmark Topic Watch Topic
  • New Topic

Limitations for number of parameters in method/constructor  RSS feed

 
Nivasini Srinivasan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In our application, we have lot of bean classes with more number of variables in each of the bean class. We have used parameterized constructors to initialize the objects. Sometimes overloaded constructors are also used. But Checkstyle points out that the maximum number of arguments given to a method or constructor must be 7. Can anyone tell the reason about why it must be limited to 7?

Also what can be the best way to initialize such kinds of objects then?

Thanks in advance.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that perhaps 7 is some sort of "rule of thumb", that methods etc with more then 7 parameters become difficult to use,
in that the readability of the code becomes degraded and that the code itself could become difficult to maintain.

I would suggest, that a method with more then 2 or 3 parameters has more then one responsiabilty and thus could be refactored,
of course this is just a guide and not a rule.


As to how to construct objects that have long paramter lists, you could take a look at the builder pattern, a Google search should
return several references.

Gavin
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, too
 
Nivasini Srinivasan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gavin

I had a look at the builder design pattern. It looks like we have to split the complex object into some small objects. Builder is the one that maintains the intermediate states, accumulates them and returns the result back to the client. It seems like there must be more number of objects in this case.

Now which solution will be the best in terms of memory consumption, loading, readability and scalability?

Creating a single object with all variables set at one shot or creating numerous tiny objects and accumulating them?
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just checked Effective Java, and its example of using the builder pattern (Item 2), makes use of inner classes, and looks very neat.

I wouldnt have thought the memory impact was noticable, the example makes use of two inner class one of which is static.

What you have to decide, and this is a classic software development problem, is the improvement in readability of clients of your bean and the maintainability of the bean itself worth the performance impact.

Only yourself as the designer can answer that.

you should also ask yourself, do you need a bean with so many parameters, does it have more then one responsability.

Gavin

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No longer a beginner's question. Moving.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!