2

I am trying to understand generics and while i was watching 1 pluralsight video of SCOTT ALLEN on Generics,he showed example of ugly code vs good code but i didnt understood something which i would like to mention below :

public enum Steps
{
  Step1,
  Step2,
  Step3
}

Example of Ugly code :

Steps value = (Steps)Enum.Parse(typeof(Steps),input);

Good code and strongly typed :

public static class StringExtensions
{
   public static TEnum ParseEnum<TEnum>(this string value)
   {
      return (TEnum)Enum.Parse(typeof(TEnum),value);
   }
}

var input = "Step1";
var value = input.ParseEnum<Steps>();
console.writeline(value);

But here i dont understand why second code is good as it is also doing a casting and seems like code 1 and 2 is same or may be I have not properly understood why second code is better as it is doing same type casting.

Can someone please explain how second code is strongly type and better as per Author although it is doing same casting as first code snippet?

I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • 1
    For me the extension seems even uglier as it implies you could parse **every** string to an enum which you certainly can´t. Having said this your question is completely opinion-based. You should ask the author what he ment by better code. – MakePeaceGreatAgain Feb 20 '19 at 11:45
  • Another problem with the extension is that you can pass any type as the generic type and the code will compile, but will throw at runtime. (e. g. `input.ParseEnum()` will throw) – Chris Dunaway Feb 20 '19 at 17:19

3 Answers3

3

I think this is a bad example. The only reason I think the second code snippet is 'good code' is that it conforms to the DRY principle (Do Not Repeat Yourself). This is generic code that code be written a 100 times, but now done once and done good.

For the generics part, there is quite some wrong with this code. You can call it on types not being an enum for example. And it doesn't really show the benefits of generics. A much simpler example would have done.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Should we really answer this question that assumes something to be a good or a bad practice? Doesn´t make this the question opinion-based? – MakePeaceGreatAgain Feb 20 '19 at 11:48
  • I was in doubt whether this question could leverage an objective answer. I think it does. – Patrick Hofman Feb 20 '19 at 11:49
  • 1
    @HimBromBeere I'm not entirely sure of that. The OP says it's "strongly typed" when it isn't, it's exactly the same as the first version. That can be objectively answered. Patrick, good answer by the way – Camilo Terevinto Feb 20 '19 at 11:50
  • @CamiloTerevinto I agree the "strongly typed"-part is simply answered, as it is simply whrong. However that´s not the actual question, which is "why is it better", which implies it **is** better, which can´t objectivly be claimed. – MakePeaceGreatAgain Feb 20 '19 at 12:04
  • @HimBromBeere I just trying to understand from generics perspective that how second code snippet is strongly type and better as compared to first code snippet – I Love Stackoverflow Feb 20 '19 at 12:11
  • Upvoted for your kind efforts towards helping me and for your valuable time.Appreciated :) – I Love Stackoverflow Feb 20 '19 at 12:23
  • @CamiloTerevinto Yes the author mentioned in the video that second example is strongly typed and the code is doing the same type casting hence i got confused so as to how it is strongly typed and asked here which is the better place to ask such question :) – I Love Stackoverflow Feb 20 '19 at 12:28
3

Good code and strongly typed

That's not good code, and there's nothing more strongly typed than in the first example. Even more, with that, IntelliSense would suggest me that

var name = "@CamiloTerevinto@".ParseEnum<Fruit>();

is valid, when it clearly isn't. In fact, that code doesn't even have a try-catch for a scenario like that.

Overall, the second snippet is somewhat better only to comply with DRY, but I wouldn't make it an extension method nor call it strongly typed.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
1

I would say the extension method provides better readability when used and I guess that is what Scott Allen was trying to show an example of. Did he really say it is more strongly typed?

The DRY principle is another good point for putting it inside a method.

Also I would probably extend it with a check for valid string values, such as:

public static TEnum ParseEnum<TEnum>(this string enumValue, bool ignoreCase = true) where TEnum : struct
{
    if (!Enum.TryParse(enumValue, ignoreCase, out TEnum result))
    {
        throw new ArgumentException(
            $"{enumValue} is not a valid {typeof(TEnum).Name}",
            typeof(TEnum).Name);
    }

    return result;
}

...but that is beside the point.

DaggeJ
  • 2,094
  • 1
  • 10
  • 22