File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes init method with lots of parameters vs lots of setters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "init method with lots of parameters vs lots of setters" Watch "init method with lots of parameters vs lots of setters" New topic
Author

init method with lots of parameters vs lots of setters

lil Yaure
Greenhorn

Joined: Feb 25, 2010
Posts: 23
Hi all,

Comparing an init() method with lots of parameters vs lots of seperate setters, or maybe a constructor.
I'm wondering whether there are any noticable performance or convention when we need to pass in more than 10 different parameters to an instance of a class?
by the way lots of these instance are created at the same time and I'm getting data from the database

In general I don't like initialising stuff in the constructor, I have been using seperate setters but I'm wondering whether the many method(setters) call will impact performance.
init() method looks cleaner with less code to type so I'm starting to change to this style, it also helps get rid of half the evil setters

Any opinions?



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Have you actually *seen* a performance issue? I can't imagine it'd make that much of a difference. Java's pretty darn fast. A huge wad of parameters would be faster than a huge wad of method calls (I'm assuming here!) but in terms of real-world performance... I'd have to benchmark it.

A method with ten parameters, however, wouldn't fly in most of my code reviews. Blindly avoiding setters seems just as bad, unless you're deliberately aiming for immutability (not a bad thing). I've sometimes taken to putting the properties/getters/setters in a base class to avoid the noise in a class that does actual work, but people make fun of me for that. Lombok might cure me of this; have to wait and see.

Depending on how much you need to ensure valid object state a more fluent, parallel group of setters might be appropriate, and can help alleviate the confusion caused by looking at a method with so many parameters. See http://c2.com/cgi/wiki?TooManyParameters.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

My understanding tells me that each individual function call would bring a Stack and storing variables and Exception handling and all. So there ought to be a performance hit somewhere down the line? Its always 'Ok' if the requirements demand so and thats better left aside.

Ideally when creating objects I'd choose to go via the constructor approach if the number of parameters that decide on the same are considerable. Else I'd choose a setter method for this.

Though, like David, so rightly, pointed out the performance is hardly gonna be affected in each scenario, it again varies considering each scenario is different (at least slightly) from the other?

Cheers,
Raj.
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
10 or 20 setters code dosen't make much of performance difference.

I will prefer the setters approach with default values for most of the properties.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


And here,

I talk about ideal programming practices? :-)

Raj.
lil Yaure
Greenhorn

Joined: Feb 25, 2010
Posts: 23
thanks for all your opinions

I haven't notice any performance issues, though it keep stuck in my head as I'm repeatingly doing it lots of times
think I'll stick with huge wad of parameters!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init method with lots of parameters vs lots of setters