I'm mainly thinking of x,y coordinates but I guess 3D coordinates and higher also apply.

Let's say I'm writing a function to return the center of a rectangle given its width and height. Which is the more logically correct out of

center(width, height) {
    return {
        x: width / 2,
        y: height / 2


center(dimensions) {
    return {
        x: dimensions.width / 2,
        y: dimensions.height / 2


I know in reality both ways would be fine... but logically, should coordinates or dimensions be thought of as one item or many?

  • I'd prefer dimensions object given that it is more flexible, but only if you use a dimensions object throughout your program as well. But it's ultimately subjective which is better.
    – Neil
    Aug 1 '17 at 10:25
  • 2
    Curious question, overall when you are so sure about returning a typed result rather than an array. The question boilds down to what implementation express better the domain of the function?.
    – Laiv
    Aug 1 '17 at 14:28

It's better to bundle them into objects. That way, you only need to care about the internal structure when you want to access one of the fields. Compare this:

dimensions = someArea.calculateDimensions();
theCenter = center(dimensions.width, dimensions.height);
someObject.moveTo(theCenter.x, theCenter.y);

To this:


The second version:

  • Works in 2D and 3D.
  • Doesn't care if the field names are x, y or top, left(notice the different order), width, height or w, h or whatever.
  • Much simpler.

Another benefit of using objects is that you can't accidentally treat, for example, coordinates as dimensions. You have to do so explicitly. This can prevent all sort of weird logical bugs...


If you use vector arithmetic or vector algebra, your data type will make the conversion from mathematics to code much easier if you bundle the coordinates. Vector arithmetic or algebra uses the vector or point as a single unit.


If you have existing code, follow it's convention. Which may well be a mishmash, i.e. support both options.

BTW, there is often a reasonable 3rd option, if the ordering is "obvious", such as Cartesian coordinates, to pass an array of coordinates. So you could do (pseudocode)

center(dimensions) {
  float c = new float(dimensions.length);
  for (int i = 0; i < dimensions.length; i++)
    c[i] = dimensions[i] / 2;
  return c;
  • Strongly disagree about using an array. I've dealt with too much code that does this and it's horrible. It's very hard to read and understand and very easy to make mistakes. Making a struct (or equivalent) is so simple that there's no reason to ever use an array. Aug 4 '17 at 3:18
  • @user1118321 I agree that the array is slightly more error prone, but how would you handle something like the dot product of 2 vectors? A "Vector2" struct, a "Vector3", "Vector4", struct, ad nauseum? This is a case where the array is a huge win.
    – user949300
    Aug 5 '17 at 15:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.