Question: I have 2 classes, a Point, which represents a point in 2D space, and a Triangle, which contains 3 Points in a list (vector) of vertices. 

For an assignment, I had to write a perimeter function that gives me the perimeter of a Triangle. To do that, I need to find the 3 side lengths from the vertices, then just add them. I ended up with:

```
double Triangle::getPerimeter() {
    double s1Length = vertices[0].distanceTo(vertices[1]);
    double s2Length = vertices[1].distanceTo(vertices[2]);
    double s3Length = vertices[2].distanceTo(vertices[0]);
    return s1length + s2length + s3length;
}

```

To me though, that looked ugly, so I decided to try storing the sides in an array:

```
double Triangle::getPerimeter() {
    double lengths[3] = {
        vertices[0].distanceTo(vertices[1]),
        vertices[1].distanceTo(vertices[2]),
        vertices[2].distanceTo(vertices[0])
    }
    return lengths[0] + lengths[1] + lengths[2];
}

```

Which I subjectively see as neater.

Obviously, if this algorithm were reusable, storing the sides in a traversable has the benefit of being scalable, but this method only works for triangles; beyond that, the order of the vertices matter, and it gets more complicated.

Is it ever general practice to store variables like this in an array, for the purpose of aesthetics? 

And is there any benefit beyond aesthetics? Does storing similarly used variables together in memory net any performance gains?

Answer: 
I do not see why you need to store them in either many variables or array:

```
double Triangle::getPerimeter() {
    double perm = 0.0;
    for( int i =0; i < 3; ++i ) {
       int next = (i + 1) % 3;
       perm += vertices[i].distanceTo( vertices[next] );
    }
    return perm;
}

```

Anyway your code should be readable and then optimize if necessary
In C++, arrays (if a element is getting accessed directly without iteration) and variables have the same speed.