com.stansassets.foundation
com.stansassets.foundation copied to clipboard
EnumUtility (kinda code-review)
Just stumbled upon this project.
was looking through the code a bit and found the Enum Utility:
https://github.com/StansAssets/com.stansassets.foundation/blob/b3332055c35bb65162e22570b14c06d59ff4155e/Runtime/Utilities/EnumUtility.cs#L9
A bit of code-review:
- all methods should have generic constraints:
where T : struct, Enum
- there already is a
Enum.TryParse<T>
method in .net standard 2.1 (https://docs.microsoft.com/en-us/dotnet/api/system.enum.tryparse?view=netcore-2.1), it should be faster than catching an exception. - CanBeParsedToEnum is the same as
Enum.TryParse("my string", out _)
(the_
omitting the result if not needed) -
ParseEnum
should be namedParseOrDefault
the name does not need to contain the word "Enum" as the static class already has the word "Enum" in it. also an optional parameter of type T to be able to define the default would be good. Also using the Enum.TryParse method instead of handling exceptions should result in a cleaner code.