2
\$\begingroup\$

I'm writing a simple 2D rendering class.

Below is the method that creates a polyline. A polyline can be closed or not according to a parameter closed.

void Polyline(IList<Point> points, bool closed)
{
    var pointsCount = points.Count;
    if (pointsCount < 2)
        return;

    int count = pointsCount;
    if (!closed)
        count = pointsCount - 1;

    for (int i1 = 0; i1 < count; i1++)
    {
        int i2 = (i1 + 1) == pointsCount ? 0 : i1 + 1;
        Point p1 = points[i1];
        Point p2 = points[i2];
        AddLineSegment(p1, p2);
    }
}

But I think the parameter name closed is a little confusing. It may have two different meanings:

  1. points have already closed the polyline: points[0] == point[points.Count-1]
  2. points doesn't close the polyline and we will add the first point to rendered polyline in the method

I want to make it clear to users that the second is the right meaning. So what kind of name should I use?

I think closesIt or something similar should be better but it still sounds ambiguous. What's your opinion?

\$\endgroup\$
3
\$\begingroup\$

A closed polyline is really a polygon. For clarity, why not just call it a polygon? There is no point in trying to do too much in the same function. Not only would the calling code be clearer, the implementations would be simpler too, without the special cases.

void Polyline(IList<Point> points)
{
    var n = points.Count - 1;
    for (int i = 0; i < n; i++)
    {
        AddLineSegment(points[i], points[i + 1]);
    }
}

void Polygon(IList<Point> points)
{
    Polyline(points);
    if (points.Count >= 3)
    {
        AddLineSegment(points[points.Count - 1], points[0]);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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