-2
\$\begingroup\$

Well I have 3 methods that are overloads, but in it's scope has the same scope except one method call.

    public static Texture2D DrawAnnulus(this Texture2D texture, Func<int, int, Color?> predicate, AnnulusConfig annulusConfig)
    {
        InitTexture(ref texture, annulusConfig.offset.x + annulusConfig.radius * 2, annulusConfig.offset.y + annulusConfig.radius * 2);

        Polar(texture, annulusConfig.offset.x, annulusConfig.offset.y, annulusConfig.radius, predicate);

        if (annulusConfig.apply)
            texture.Apply();

        return texture;
    }

    public static Texture2D DrawAnnulus(this Texture2D texture, SectorList list, AnnulusConfig annulusConfig)
    {
        InitTexture(ref texture, annulusConfig.offset.x + annulusConfig.radius * 2, annulusConfig.offset.y + annulusConfig.radius * 2);

        Polar(texture, annulusConfig.offset.x, annulusConfig.offset.y, annulusConfig.radius, (xx, yy) => Annulus(annulusConfig.offset.x, annulusConfig.offset.y, xx, yy, annulusConfig.radius2, list));

        if (annulusConfig.apply)
            texture.Apply();

        return texture;
    }

    public static Texture2D DrawAnnulus(this Texture2D texture, Color? color, AnnulusConfig annulusConfig)
    {
        InitTexture(ref texture, annulusConfig.offset.x + annulusConfig.radius * 2, annulusConfig.offset.y + annulusConfig.radius * 2);

        Polar(texture, annulusConfig.offset.x, annulusConfig.offset.y, annulusConfig.radius, (xx, yy) => Annulus(annulusConfig.offset.x, annulusConfig.offset.y, xx, yy, annulusConfig.radius2, color));

        if (annulusConfig.apply)
            texture.Apply();

        return texture;
    }

As you can see the only changing part is the Polar method call. The other part is only checks.

How could I simplify this?

\$\endgroup\$

closed as unclear what you're asking by t3chb0t, alecxe, Zeta, janos, Mast Dec 29 '18 at 9:37

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ You need to tell us the purpose of the code and explain what it does. It' currently just code without any explanation which makes it off-topic. \$\endgroup\$ – t3chb0t Dec 28 '18 at 18:40
2
\$\begingroup\$

You could create a single private method and pass the Polar call as an action from each of the overloads

public static Texture2D DrawAnnulus(this Texture2D texture, Func<int, int, Color?> predicate, AnnulusConfig annulusConfig)
    => DrawAnnulus(
        texture,
        annulusConfig,
        () => Polar(
            texture,
            annulusConfig.offset.x,
            annulusConfig.offset.y,
            annulusConfig.radius,
            predicate));

public static Texture2D DrawAnnulus(this Texture2D texture, SectorList list, AnnulusConfig annulusConfig)
    => DrawAnnulus(
        texture,
        annulusConfig,
        () => Polar(
            texture,
            annulusConfig.offset.x,
            annulusConfig.offset.y,
            annulusConfig.radius,
            (xx, yy) => Annulus(
                annulusConfig.offset.x,
                annulusConfig.offset.y,
                xx,
                yy,
                annulusConfig.radius2, list)));

public static Texture2D DrawAnnulus(this Texture2D texture, Color? color, AnnulusConfig annulusConfig)
    => DrawAnnulus(
        texture,
        annulusConfig,
        () => Polar(
            texture,
            annulusConfig.offset.x,
            annulusConfig.offset.y,
            annulusConfig.radius,
            (xx, yy) => Annulus(
                annulusConfig.offset.x,
                annulusConfig.offset.y,
                xx,
                yy,
                annulusConfig.radius2,
                color)));

private static Texture2D DrawAnnulus(Texture2D texture, AnnulusConfig annulusConfig, Action polarAction)
{
    InitTexture(ref texture, annulusConfig.offset.x + annulusConfig.radius * 2, annulusConfig.offset.y + annulusConfig.radius * 2);

    polarAction();

    if (annulusConfig.apply)
        texture.Apply();

    return texture;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ I have to say that I find your calls pretty scarry. \$\endgroup\$ – t3chb0t Dec 28 '18 at 18:37
  • \$\begingroup\$ I totally agree. To be perfectly honest, I don't think I'd go this route myself. Of all the approaches I've seen so far, I believe I like OP's the best - although possibly some refactoring could be done upstream so that these extension methods aren't required to do such gymnastics. \$\endgroup\$ – benj2240 Dec 28 '18 at 18:42
1
\$\begingroup\$

You could create a single private method and decide there what overload to call based on the presence of the arguments

public static Texture2D DrawAnnulus(this Texture2D texture, 
            Func<int, int, Color?> predicate, 
            AnnulusConfig annulusConfig)
    => texture.DoDrawAnnulus(predicate, anulusConfig, null, null);

    public static Texture2D DrawAnnulus(this Texture2D texture
            ,SectorList list
            ,AnnulusConfig annulusConfig)
    => texture.DoDrawAnnulus(null, anulusConfig, list, null);    

    public static Texture2D DrawAnnulus(this Texture2D texture, 
            Color? color, 
            AnnulusConfig annulusConfig)
    => texture.DoDrawAnnulus(null, anulusConfig, null, color);

private static Texture2D DoDrawAnnulus(this Texture2D texture, 
                Func<int, int, Color?> predicate, 
                AnnulusConfig annulusConfig, 
                SectorList list, 
                Color? color)
    {
        InitTexture(ref texture, annulusConfig.offset.x + annulusConfig.radius * 2, annulusConfig.offset.y + annulusConfig.radius * 2);

        if (list != null)
        {
          Polar(texture, annulusConfig.offset.x, annulusConfig.offset.y, annulusConfig.radius, (xx, yy) => Annulus(annulusConfig.offset.x, annulusConfig.offset.y, xx, yy, annulusConfig.radius2, list));
        }
        else if (predicate != null)
        {
           Polar(texture, annulusConfig.offset.x, annulusConfig.offset.y, annulusConfig.radius, predicate);
        }
        else
        {
            Polar(texture, annulusConfig.offset.x, annulusConfig.offset.y, annulusConfig.radius, (xx, yy) => Annulus(annulusConfig.offset.x, annulusConfig.offset.y, xx, yy, annulusConfig.radius2, color));
        }

        if (annulusConfig.apply)
            texture.Apply();

        return texture;
    }
\$\endgroup\$
  • \$\begingroup\$ Reflection? Dynamics? Ahhhhhh! \$\endgroup\$ – t3chb0t Dec 28 '18 at 18:38
  • \$\begingroup\$ Yep, I might delete that. \$\endgroup\$ – Adrian Iftode Dec 28 '18 at 18:39
  • \$\begingroup\$ Do you think a polymorphic approach would solve the problem, or is it too much this too? \$\endgroup\$ – Adrian Iftode Dec 28 '18 at 18:42
  • \$\begingroup\$ Not sure, I'm not going to take any deeper look at OP's code until they fix the question and make it on topic. \$\endgroup\$ – t3chb0t Dec 28 '18 at 18:43
0
\$\begingroup\$

You could create helper methods for the repeated parts of the three overloads.

private static void InitTexture(ref Texture2D texture, AnnulusConfig annulusConfig)
    => InitTexture(ref texture, annulusConfig.offset.x + annulusConfig.radius * 2, annulusConfig.offset.y + annulusConfig.radius * 2);

private static void ApplyIfNecessary(Texture2D texture, AnnulusConfig annulusConfig)
{
    if (annulusConfig.apply)
        texture.Apply();
}
\$\endgroup\$
  • \$\begingroup\$ This should be part of the previous answer and not another one. \$\endgroup\$ – t3chb0t Dec 28 '18 at 18:37
  • \$\begingroup\$ I don't see why. It's a very different approach. \$\endgroup\$ – benj2240 Dec 28 '18 at 18:39
  • \$\begingroup\$ This isn't a different approach because InitTexture isn't even defined in your other answer. It's a method that is missing there. \$\endgroup\$ – t3chb0t Dec 28 '18 at 18:41
  • \$\begingroup\$ InitTexture (with all the arguments) isn't defined in any answer; I assume it exists because it's called by OP. \$\endgroup\$ – benj2240 Dec 28 '18 at 18:44

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