During a recent project I\'ve been working on, I\'ve had to use a lot of functio
ID: 654334 • Letter: D
Question
During a recent project I've been working on, I've had to use a lot of functions that kind of look like this:
static bool getGPS(double plane_latitude, double plane_longitude, double plane_altitude,
double plane_roll, double plane_pitch, double plane_heading,
double gimbal_roll, double gimbal_pitch, double gimbal_yaw,
int target_x, int target_y, double zoom,
int image_width_pixels, int image_height_pixels,
double & Target_Latitude, double & Target_Longitude, double & Target_Height);
So I want to refactor it to look something like this:
static GPSCoordinate getGPS(GPSCoordinate plane, Angle3D planeAngle, Angle3D gimbalAngle,
PixelCoordinate target, ZoomLevel zoom, PixelSize imageSize)
This appears to me to be significantly more readable and safe than the first method. But does it make sense to create PixelCoordinate and PixelSize classes? Or would I be better off just using std::pair<int,int> for each. And does it make sense to have a ZoomLevel class, or should I just use a double?
My intuition behind using classes for everything is based on these assumptions:
If there are classes for everything, it would be impossible to pass a ZoomLevel in where a Weight object was expected, so it would be more difficult to provide the wrong arguments to a function
Likewise, some illegal operations would cause compile errors, such as adding a GPSCoordinate to a ZoomLevel or another GPSCoordinate
Legal operations will be easy to represent and typesafe. i.e subtracting two GPSCoordinates would yield a GPSDisplacement
However, most C++ code I've seen uses a lot of primitive types, and I imagine there must be a good reason for that. Is it a good idea to use objects for anything, or does it have downsides that I am not aware of?
Explanation / Answer
Yes, definitely. Functions/methods that take too many arguments is a code smell, and indicates at least one of the following:
The function/method is doing too many things at once
The function/method requires access to that many things because it's asking, not telling or violating some OO design law
The arguments are actually closely related
If the last one is the case (and your example certainly suggests so), it's high time to do some of that fancy-pants "abstraction" that the cool kids were talking about oh, just some decades ago. In fact, I'd go further than your example and do something like:
static GPSCoordinate getGPS(Plane plane, Angle3D gimbalAngle, GPSView view)
(I am not familiar with GPS so this is probably not a correct abstraction; for instance, I don't understand what zoom has to do with a function called getGPS.)
Please prefer classes like PixelCoordinate over std::pair<T, T>, even if the two have the same data. It adds semantic value, plus a bit of extra type safety (compiler will stop you from passing a ScreenCoordinate to a PixelCoordinate even though both are pairs.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.