Using Extensions: Weighing The Pros vs Cons

前端 未结 5 1137
时光取名叫无心
时光取名叫无心 2021-01-06 06:54

Recently I asked a question about how to clean up what I considered ugly code. One recommendation was to create an Extension Method that would perform the desired function

5条回答
  •  长发绾君心
    2021-01-06 07:43

    Personally I think the "problems" of extension method readability are vastly overstated. If you concentrate on making your code easy to read in terms of what it's doing, that's more important most of the time than how it's doing it. If the developer wants to trace through and find out what's actually happening behind the scenes, they can always click through to the implementation.

    My main problem with extension methods is their discovery method - i.e. via a specified namespace instead of a specified class. That's a different matter though :)

    I'm not suggesting that you put in extension methods arbitrarily, but I would seriously consider how often you need to know how every expression in a method works vs skimming through it to see what it does in broader terms.

    EDIT: Your use of terminology may be misleading you slightly. There's no such thing as an "extension object" - there are only "extension methods" and they have to exist in static types. So you may need to introduce a new type but you're not creating any more objects.

提交回复
热议问题