Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are enum good to indicate object state? [closed]

Tags:

c#

.net

enums

Lead developer in my current project forbid me to use any enums. He claims enums are just problems. I can use it only if I'm forced - when communicate with outside applications/libraries that require enum. He tells that I should have object for every state. For example we have game with object Soldier. So I have to have objects: - RunningSoldier - SittingSoldier - StandingSoldier

Is he right? I don't feel it. I have only arguments like I don't want to write so many classes.

Thanks!

like image 608
apukineimakiapudjuh Avatar asked Mar 18 '23 07:03

apukineimakiapudjuh


1 Answers

For your case enums are bad. Because I think RunningSoldier will have different properties/methods than other type of soldiers. If you will have an enum for the state, then in your every method you will have a if/else/switch condition. So your methods will be large and harder to maintain. So it is better to have different classes with a base Soldier class (OOP concepts).

Update:

If you need to add another type for soldier, then adding a class will be easier than enum. As for enum you have to go through all the methods (move, jump, fire, etc). But for class case it is easy to add a new class and implement those methods. Nice and easy.

It might be possible that in some other case enums will be better than separate classes. Like for example enum for CurrencyType or Units. I have a method in my code which takes two enums and converts value from one

public static double ConvertUnits(this double original, SimplifiedUnits sourceUnits, SimplifiedUnits targetUnits)
{
    if (sourceUnits == targetUnits || sourceUnits == SimplifiedUnits.Unknown)
    return original;
    if (sourceUnits == SimplifiedUnits.Feet && targetUnits == SimplifiedUnits.Meters)
    return original * GeometryConstants.MetersPerFoot;
    if (sourceUnits == SimplifiedUnits.Meters && targetUnits == SimplifiedUnits.Feet)
    return original / GeometryConstants.MetersPerFoot;
        throw new NotSupportedException();
}
like image 52
fhnaseer Avatar answered Mar 20 '23 07:03

fhnaseer