aspose file tools*
The moose likes Java in General and the fly likes What is the maximum number of parameters to be passed ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the maximum number of parameters to be passed ?" Watch "What is the maximum number of parameters to be passed ?" New topic
Author

What is the maximum number of parameters to be passed ?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 845
I have a theoretical question, what is the maximum number of parameters to be passed is considered a good practice.

In my method i am passing 5 parameters. I am moving forward to add more, so total will be 7 parameters.

Well i have a solution to this problem. To make a Map and put 3 parameters inside it. and pass it as a 1 parameter.

can you guys share your expertise ?


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Actually I use the Map solution all the time. It has the great advantage of not breaking when you add more functionality.

Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

I will also sometimes define a bean to abstract a complex set of arguments -- has a bit more structure than a Map. But I like the Map solution as well (a habit from JavaScript). I'll use whichever seems to make the most sense within a scenario.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I tend to make a bean, often with "fluent"-style methods. I use maps sometimes, but find the lack of type safety (depending on what you're doing, of course) a little bothersome.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

David Newton wrote:I use maps sometimes, but find the lack of type safety (depending on what you're doing, of course) a little bothersome.

That. And the fact that both sides need to agree on the keys. But sometimes, a map just feels right.
David Sharpe
Ranch Hand

Joined: Jun 15, 2009
Posts: 32
If I write a method that has more than three parameters, I usually get suspicious that I'm either not decomposing the problem, or not encapsulating my solution.

For example:
void recordSale(Sale sale, int day, int month, int year) {...}

Should probably be:
void recordSale(Sale sale, Date date) {...}

Or potentially even just:
void recordSale(Sale sale) {...}

Like most best practices, any answer to your question will really only be a heuristic for good design. Sometimes, one parameter is too many.

I worry that the Map solution is easily misused:
void recordSale(Sale sale, Map date) {...}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the maximum number of parameters to be passed ?