Results 1 to 9 of 9
  1. #1
    syncppl is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default methods with a lot of parameters

    Hi,

    I want your opinion on the following problem:

    I have few methods that take 10 to 15 arguments. I dont like this happening. Whats the best way to get rid of this? Can I use a MAP as argument? Whats the best way here?

    Thanks.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Difficult to give a solution without knowing the problem that's trying to be solved.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Quote Originally Posted by syncppl View Post
    Hi,

    I want your opinion on the following problem:

    I have few methods that take 10 to 15 arguments. I dont like this happening. Whats the best way to get rid of this? Can I use a MAP as argument? Whats the best way here?

    Thanks.
    As r...x says, it all depends. For me that's usually (note "usually", not "always") implies you're missing something from your model...or your model is wrong.

  4. #4
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    Just use an array of objects,
    Java Code:
    Object[] arr = new Object[length];
    But by using this method you cannot use primitives, so for ints you just have to make Integers, which might not be the way you like.
    Java Code:
    Integer i = 5;
    The one who laughs last is teh stupid.
    :3

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Quote Originally Posted by Twitchy5 View Post
    Just use an array of objects,
    Java Code:
    Object[] arr = new Object[length];
    But by using this method you cannot use primitives, so for ints you just have to make Integers, which might not be the way you like.
    Java Code:
    Integer i = 5;
    That's not a solution.
    That's an appalling hack...

  6. #6
    JavaVideos is offline Member
    Join Date
    Sep 2009
    Posts
    13
    Rep Power
    0

    Default

    I would think about whether these arguments could be wrapped into there own class.

    Then you can just pass one object into the method that is an instance of this new class.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tolls View Post
    That's not a solution.
    That's an appalling hack...
    I have to agree, as you'll have an array that contains all different types of objects, a virtual Object stew. An alternative is to create a class just to hold the parameters. This is what Swing does in many instances, including with it's GridBagLayout. It has a class called GridBagConstraints that holds many different objects in a logical way. But note that if you do this, you may not want to fold all of the parameters into the large parameter object. For instance when you use a GridBagConstraint object to add a component to a Container that uses GridBagLayout, you don't fold the most important parameter, the component into the GridBagConstraint object. Since it is so very important, it get's passed to the Container on its own:

    Container#add(Component c, GridBagConstraint gbc)
    Last edited by Fubarable; 09-15-2009 at 06:46 PM.

  8. #8
    syncppl is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks everyone for your suggestions.

    What I understood is that I should have a bean class and should wrap all the prams in its object as pass it as a parameter.

    public void doSomething(ABean bean){
    ...
    }

    The problem with this is that ABean class should be visible in the package where this method is declared and also should be visible in the package from where doSomething(.. is being called.

    Having Map<String, Object> as method's argument solves the problem. Is this the right approach? Am I thinking in right direction?

    Cheers
    Last edited by syncppl; 09-16-2009 at 10:45 AM. Reason: typo

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    As I said earlier, it looks to me like your whole model is wrong if you're passing around 10-15 parameters. That doesn't mean simply creating a Class to wrap these 10-15 things up in, nor does it mean the usually poor choice of sending in a HashMap. The only time that makes some sense is for properties, and even then I'd think twice about using it.

    As said in post #2, without actually knowing your project we can't really say.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. How to add two other parameters?
    By albi_87m in forum Advanced Java
    Replies: 3
    Last Post: 05-18-2009, 01:25 PM
  3. Hiding parameters from URL
    By Saurabh321 in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 12:43 PM
  4. get parameters in jsp
    By cecily in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2007, 04:29 AM
  5. parameters=null
    By mary in forum Java Servlet
    Replies: 1
    Last Post: 07-13-2007, 04:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •