Search code examples
c#methodsoverloadingdry

Is there a way to reduce the repetitive code present in overloaded methods that share common functionality? If so, how?


Here, I have two overloaded methods with the sole purpose of returning a list of Vertex structs responsible for rendering a quad.

public static List<Vertex> Quad(Vector3 position, float size)
{
    return new List<Vertex>()
    {
        new Vertex() { Position = new Vector3(position.X - (size / 2), position.Y - (size / 2), position.Z) },
        new Vertex() { Position = new Vector3(position.X + (size / 2), position.Y - (size / 2), position.Z) },
        new Vertex() { Position = new Vector3(position.X + (size / 2), position.Y + (size / 2), position.Z) },
        new Vertex() { Position = new Vector3(position.X - (size / 2), position.Y + (size / 2), position.Z) },
    };
}

public static List<Vertex> Quad(Vector3 position, Color4 color, float size)
{
    return new List<Vertex>()
    {
        new Vertex()
        {   // struct containing 3 floating-point values: x, y, z
            Position = new Vector3(position.X - (size / 2), position.Y - (size / 2), position.Z),
            // struct containing 4 floating-point values: Red, Green, Blue, Alpha
            Color = new Color4(color.R, color.G, color.B, color.A),
        },
        new Vertex()
        {
            Position = new Vector3(position.X + (size / 2), position.Y - (size / 2), position.Z),
            Color = new Color4(color.R, color.G, color.B, color.A),
        },
        new Vertex()
        {
            Position = new Vector3(position.X + (size / 2), position.Y + (size / 2), position.Z),
            Color = new Color4(color.R, color.G, color.B, color.A),
        },
        new Vertex()
        { 
            Position = new Vector3(position.X - (size / 2), position.Y + (size / 2), position.Z),
            Color = new Color4(color.R, color.G, color.B, color.A),
        },
    };
}

As you can see, it is going to get quite long after more overloaded methods are added for the remaining vertex properties and valid combinations of each.

internal struct Vertex
{
    public Vector3 Position { get; set; }

    public Color4 Color { get; set; }

    public Vector2 TexCoord { get; set; }

    public int TexUnit { get; set; }
}

I know that something like this could be reduced using other methods to carry the common functionality, but I really don't think that it would be very applicable in this type of situation as I believe it would provide little improvement and to me, it seems a little bit messy.

At the current rate, this file is going to become very large and tedious to navigate and I feel like it violates the D.R.Y (Do not repeat yourself) principle.

Is there a better, cleaner, and less repetitive way of achieving method overloading? If so, how?


Solution

  • You could have methods with less parameters call the method with all the parameters with some defaults, e.g.

    public static List<Vertex> Quad(Vector3 position, float size)
    {
        Color4 defaultColor = new Color4(...);
        return Quad(position, defaultColor, size);
    }
    

    If the parameter with a default could be a constant, then you could also specify the default in the method signature, making it an optional parameter:

    public static List<Vertex> Quad(Vector3 position, Color4 color, float size = 1f) {
    }