• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Define a class.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a factory pattern where you configure a factory then create the product:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Mike and Stuart. Good ideas!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What? What, what, what? What what tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic