MillerMark / MrAnnouncerBot

A Twitch bot written in C#
32 stars 13 forks source link

IncludeIf #20

Closed wilbennett closed 3 years ago

wilbennett commented 5 years ago

Mark,

Here is an example of what I was suggesting a few streams ago. To be clear, I'm not saying it's "better". Just the way I usually do it because "I" think it's easier to read and maintain. Let me know what you think.

Wil

Original:

static WeaponProperties GetWeaponProperties(WeaponDto weaponDto)
{
    WeaponProperties result = WeaponProperties.None;
    if (MathUtils.IsChecked(weaponDto.Ammo))
        result |= WeaponProperties.Ammunition;
    if (MathUtils.IsChecked(weaponDto.Finesse))
        result |= WeaponProperties.Finesse;
    if (MathUtils.IsChecked(weaponDto.Heavy))
        result |= WeaponProperties.Heavy;
    if (MathUtils.IsChecked(weaponDto.Light))
        result |= WeaponProperties.Light;
    if (MathUtils.IsChecked(weaponDto.Martial))
        result |= WeaponProperties.Martial;
    if (MathUtils.IsChecked(weaponDto.Melee))
        result |= WeaponProperties.Melee;
    if (MathUtils.IsChecked(weaponDto.Ranged))
        result |= WeaponProperties.Ranged;
    if (MathUtils.IsChecked(weaponDto.Reach))
        result |= WeaponProperties.Reach;
    if (MathUtils.IsChecked(weaponDto.Special))
        result |= WeaponProperties.Special;
    if (MathUtils.IsChecked(weaponDto.Thrown))
        result |= WeaponProperties.Thrown;
    if (MathUtils.IsChecked(weaponDto.TwoHanded))
        result |= WeaponProperties.TwoHanded;
    if (MathUtils.IsChecked(weaponDto.Versatile))
        result |= WeaponProperties.Versatile;

    return result;
}

Alternate:

static WeaponProperties GetWeaponProperties3(WeaponDto weaponDto)
{
    WeaponProperties result = WeaponProperties.None;
    IncludeIf(weaponDto.Ammo, WeaponProperties.Ammunition);
    IncludeIf(weaponDto.Finesse, WeaponProperties.Finesse);
    IncludeIf(weaponDto.Heavy, WeaponProperties.Heavy);
    IncludeIf(weaponDto.Light, WeaponProperties.Light);
    IncludeIf(weaponDto.Martial, WeaponProperties.Martial);
    IncludeIf(weaponDto.Melee, WeaponProperties.Melee);
    IncludeIf(weaponDto.Ranged, WeaponProperties.Ranged);
    IncludeIf(weaponDto.Reach, WeaponProperties.Reach);
    IncludeIf(weaponDto.Special, WeaponProperties.Special);
    IncludeIf(weaponDto.Thrown, WeaponProperties.Thrown);
    IncludeIf(weaponDto.TwoHanded, WeaponProperties.TwoHanded);
    IncludeIf(weaponDto.Versatile, WeaponProperties.Versatile);

    return result;

    void IncludeIf(string propertyValue, WeaponProperties value)
    {
        if (MathUtils.IsChecked(propertyValue)) result |= value;
    }
}

More Generic:

static WeaponProperties GetWeaponProperties(WeaponDto weaponDto)
{
    WeaponProperties result = WeaponProperties.None;
    IncludeIf(MathUtils.IsChecked(weaponDto.Ammo), WeaponProperties.Ammunition);
    IncludeIf(MathUtils.IsChecked(weaponDto.Finesse), WeaponProperties.Finesse);
    IncludeIf(MathUtils.IsChecked(weaponDto.Heavy), WeaponProperties.Heavy);
    IncludeIf(MathUtils.IsChecked(weaponDto.Light), WeaponProperties.Light);
    IncludeIf(MathUtils.IsChecked(weaponDto.Martial), WeaponProperties.Martial);
    IncludeIf(MathUtils.IsChecked(weaponDto.Melee), WeaponProperties.Melee);
    IncludeIf(MathUtils.IsChecked(weaponDto.Ranged), WeaponProperties.Ranged);
    IncludeIf(MathUtils.IsChecked(weaponDto.Reach), WeaponProperties.Reach);
    IncludeIf(MathUtils.IsChecked(weaponDto.Special), WeaponProperties.Special);
    IncludeIf(MathUtils.IsChecked(weaponDto.Thrown), WeaponProperties.Thrown);
    IncludeIf(MathUtils.IsChecked(weaponDto.TwoHanded), WeaponProperties.TwoHanded);
    IncludeIf(MathUtils.IsChecked(weaponDto.Versatile), WeaponProperties.Versatile);

    return result;

    void IncludeIf(bool cond, WeaponProperties value)
    {
        if (cond) result |= value;
    }
}

You could make the IncludeIf method global and generic as well. There would need to be a modification since you have to "change" the enum to a number to "or" it in a generic method. I don't think the global approach is worth the expense though (you have to box/unbox or something similar - like use IConvertible).

krptodr commented 4 years ago

Just came here and thought I'd recommend a possible use case for Value-Objects:

https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/implement-value-objects#value-object-implementation-in-c

MillerMark commented 3 years ago

Both are great suggestions. Thanks friends!