Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to reduce the number of input parameters to a java function?

 
Michael L. Zhang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just want to know what is the best practice to reduce the number of input parameters to a java function?

I took over a piece of code, one java constructor function has 15-20 input parameters, including boolean, String data type, etc. Since we keep adding new parameters, we 'd better use other ways to reduce the number of input parameters?

What is the best way to do that?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define a class.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a factory pattern where you configure a factory then create the product:
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albrechtsen:
You could use a factory pattern where you configure a factory then create the product


That actually sounds more like the Builder pattern to me.

An alternative is to simply introduce parameter objects: group parameters into their own classes. For example, instead of passing street, house number, zip code and city as separate objects, pass an Address object.

There are also some more advanced alternatives, but without knowing more about your code, it's hard to advice on which one to use.

If you want to know more about the general techniques of improving your code, I'd highly recommend reading "Refactoring" by Martin Fowler.
 
Michael L. Zhang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks every one, the below is the piece of code, you can see, the last constructor takes a lot of input parameters. The java code to call the last constructor has to use a lot of input parameters. Any advice on this piece of code? The java file name is: Resource.java, which is a common object to hold status data and it is used every where in the application.

 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going along with the earlier suggestion it looks like you need a class to send into the ctor. Maybe a class called ResourceConfiguration. You could get rid of all the existing ctors and just use one:



You could set up the ResourceConfiguration class to initialize all fields to null or some other meaningful default so the user only has to set the fields that deviate from the default value. The ResourceConfiguration class might be a good place to define standard constants if you have any (using static final fields).

Long arg lists start to get hard to manage cause you start to forget the order of the args. Since all of your args are either String or boolean it's pretty easy to get the order of the actuals wrong without the compiler noticing.

_M_
[ December 20, 2005: Message edited by: Mike Noel ]
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Design Patterns terminology, think beans / value objects.

And I like to quote good old Bruce here:

"For every problem in OO programming, the solution is to create a new class"

Non-verbatim.
 
Michael L. Zhang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Mike and Stuart. Good ideas!
 
Junilu Lacar
Bartender
Pie
Posts: 7268
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also the option of IoC/DI (Inversion of Control/Dependency Injection) using containers such as Spring and others like it. Spring helps you cut down on a lot of the setup code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic