Guest User

Guest User

  • Tech Writer
  • 48
  • 12k

How can I best divide up this constructor?

Oct 2 2019 3:50 PM
I am running into the problem of my constructors becoming too large. Please see the example for a Vehicle class below:
  1. public Vehicle(double width, double length, double turningRadius, double angle, double middleX,
  2.                double middleY, double velocity, double steeringAngle, double velocityMax,
  3.                double velocityChange)
With 10 parts, this is already quite large. I plan to add other aspects to this, such as color, gear shifting, friction, number of tires, etc. My question is, how do I restructure this so that it is more redable and manageable?
 
I have considered things like combining middleX and middleY into a point, or making a struct for the 3 velocity-related items, or even setting nothing in the constructor and setting each item or group of items with its own function, but that gets messy.
 
How can I address this issue, when do I know how much is too much, and how can I fix it? 

Answers (3)