Reputation: 307
I am assigned to a job of optimizing performance of an Application.
Often it is needed to pass 16-25 parameters to the constructor, and setting them there. I want to create a class & object for this and set these values to the object and then pass. It will be good to read. But will it do any good to my task (i.e performance optimization)?
Upvotes: 15
Views: 1631
Reputation: 167
As oops suggest to collaborate multiple properties into a single unit called class , which is represented by an object so it will improve performance and readiness of code
Upvotes: 0
Reputation: 107237
It would be fairly unusual in modern software to have 25 loose variables on a calling function, and then pass these explicitly to a method or ctor.
More often than not, in an OO design, these variables would instead already be packaged into a class (or a few classes), grouped on their logical responsibilities.
And since Java
passes objects by reference, passing a single object reference on the stack could have some performance benefit (fewer variables to push to the stack). However the real benefit would be code readability and maintenance.
It must however be noted that doing so would require that the class of the object being passed be shared between consumer and service - this may be an issue, depending on what the transfer class is modelling (e.g. is it a Data Transfer Object, a Business Entity, a View Model, an XML / JSON serialization object, etc?). If sharing type between caller and callee would violate your architecture, then you would typically map the 25 variables into another suitable canonical class (or classes, again observing SRP refactoring concerns) and pass this(these), instead. At this point, there will be no performance benefit, but the readability / maintainability benefit will be retained.
Upvotes: 5
Reputation: 18408
Performance differences deriving from this, if any, are very unlikely to be noticeable.
If your task is to solve performance problems, then your first job is to locate where the problems are. You do that by profiling the application. Have you done that and has it demonstrated that the problem is in the parameter passing ?
Upvotes: 14
Reputation: 393781
It might improve the performance slightly, but the main benefit would be increasing the readability of your code. A constructor with 16-25 parameters is not readable, and very hard to use.
Of course, you should only introduce new classes that make sense (i.e. the parameters are related to each other). There's no point is shoving 15-26 unrelated parameters to one class just for the sake of passing them to a constructor.
Upvotes: 15