• Post Reply Bookmark Topic Watch Topic
  • New Topic

Number of parameters in a method - how could I do this better?  RSS feed

 
K DeLucia
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty new to java and have just discovered CheckStyle. One of the out-of-the-box checks is that a method doesn't have more than 7 parameters. Below is my method. Basically, I want to clear some session attributes. Is there a better way I could do this rather than passing in each parameter? Should I put them into a list/array? What would make the most sense?



Any suggestions are appreciated. Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, in a case like this, it makes more sense to just duplicate the body of the routine everywhere it appears, and delete the method altogether. It will be extraordinarily difficult to ensure that every time you call this method you get all the parameters in the right order; writing it out explicitly makes this harder to do. Of course, then it's easier to forget one, but you can't have everything!
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason why we couldn't just have these grouped as instance variables of some object, and pass the object to this method?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!