aspose file tools*
The moose likes Beginning Java and the fly likes how to reduce the number of input parameters to a java function? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to reduce the number of input parameters to a java function?" Watch "how to reduce the number of input parameters to a java function?" New topic
Author

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

Michael L. Zhang
Ranch Hand

Joined: Jul 06, 2003
Posts: 33
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

Joined: Aug 31, 2004
Posts: 11343

Define a class.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You could use a factory pattern where you configure a factory then create the product:


There is no emoticon for what I am feeling!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Michael L. Zhang
Ranch Hand

Joined: Jul 06, 2003
Posts: 33
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

Joined: Dec 15, 2005
Posts: 108
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 ]

Mike Noel
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
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.


ASCII silly question, Get a silly ANSI.
Michael L. Zhang
Ranch Hand

Joined: Jul 06, 2003
Posts: 33
Thanks, Mike and Stuart. Good ideas!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4742
    
    7

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to reduce the number of input parameters to a java function?