| Author |
Pass Lots of Parameters to Methods
|
Natalie Kopple
Ranch Hand
Joined: May 06, 2003
Posts: 320
|
|
I have close to 30 parameters. I have to pass all those parameters to lots of the methods that are to be invoked. What is the proper way to do it? I am thinking to do the following and please comment.
1. create a Java Bean class called DefaultParameters. Declare all those 30 parameters with their respective type, and code getters and setters of those parameters in the Java Bean.
2. pass the DefaultParameters as one of the parameters in methods to be invoked:
3. retrieve each individual default parameter inside each method; for example:
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
There isn't a proper way to do it. It depends on the parameters.
If I had that problem I would start by asking things like whether the parameters come in groups, and various other analysis-type questions.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
|
. . . or consider whether it is a good idea to have a method requiring that many parameters in the first place. I read somewhere that somebody (probably Joshua Bloch) said you should never have more than three parameters for a method, because of the risk of confusion with more parameters.
|
 |
Greg Brannon
Bartender
Joined: Oct 24, 2010
Posts: 530
|
|
Making Paul's point differently, a goal in Object Oriented programming should be to encapsulate a program's data as much as possible into objects and then create methods to operate on or with those objects to provide needed results. Can some, all, or a majority of those 30 parameters be included as parts of an object or split between a small number of objects?
Think about your design and how those parameters can be logically grouped into objects in a way that makes sense rather than considering each parameter as a separate entity that must have its own, top-level identity.
Good luck, and let us know what you work out.
|
Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
|
 |
Natalie Kopple
Ranch Hand
Joined: May 06, 2003
Posts: 320
|
|
Thanks to all of you for your help.
What I am assigned to do is the foremost front line of an application. After an application is launched, I am to get all of operational parameters; such as the application instance ID, application instance start time, measurements reset period, execution tracing logging level, application start date, etc. All of them will be made availabel for view via the Application admin GUI.
In additon, I have to instatiate all the major components of the application in sequence, and pass parameters; such as user request ID, request start time, parsed request URI, all aforementioned operational parameters, etc. to the methods of each of those major components of the application.
Question 1. I just found out that the operational parameters are in a XML file. Can we pass XML as a parameter to a method?
Question 2. What is the way to code this kind of very top level of an application? I have never done this kind of work before.
Thank you.
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12269
|
|
If everything is in an XML file, sure you can pass the DOM Document reference.
Personally, I do that sort of thing with a Map. That way you never get tangled up in the number of parameters because there is only the Map reference being passed.
Sure the store and retrieve using a key takes a tiny bit of extra time but if you choose the key words right it becomes nearly self documenting, well worth it in the long run.
Bill
|
Java Resources at www.wbrogden.com
|
 |
Natalie Kopple
Ranch Hand
Joined: May 06, 2003
Posts: 320
|
|
The major components in the application are; for example, Edit Logger Component, De-Duplicate Component, etc.
I have to invoke methods of every single major component in a sequential order.
Should I use thread? I mean if I should extends Thread and put method implementaion code in run()?
|
 |
Natalie Kopple
Ranch Hand
Joined: May 06, 2003
Posts: 320
|
|
|
Sorry, I want to take back my question in regard to "thread".
|
 |
 |
|
|
subject: Pass Lots of Parameters to Methods
|
|
|