Extension Methods and Application Code
Posted
by Mystagogue
on Stack Overflow
See other posts from Stack Overflow
or by Mystagogue
Published on 2010-04-21T21:50:26Z
Indexed on
2010/04/21
21:53 UTC
Read the original article
Hit count: 360
extension-methods
|guidelines
I have seen plenty of online guidelines for authoring extension methods, usually along these lines:
1) Avoid authoring extension methods when practical - prefer other approaches first (e.g. regular static methods).
2) Don't author extension methods to extend code you own or currently develop. Instead, author them to extend 3rd party or BCL code.
But I have the impression that a couple more guidelines are either implied or advisable. What does the community think of these two additional guidelines:
A) Prefer to author extension methods to contain generic functionality rather than application-specific logic. (This seems to follow from guideline #2 above)
B) An extension method should be sizeable enough to justify itself (preferably at least 5 lines of code in length).
Item (B) is intended to discourage a develoer from writing dozens of extension methods (totalling X lines of code) to refactor or replace what originally was already about X lines of inline code. Perhaps item (B) is badly qualified, or even misinformed about how a one line extension method is actually powerful and justified. I'm curious to know. But if item (B) is somehow dismissed by the community, I must admist I'm still particularly interested in feedback on guideline (A).
© Stack Overflow or respective owner