In our ASP.NET MVC project, we have an HtmlHelper
extension method to generate a static google map.
public static MvcHtmlString StaticMap(this HtmlHelper helper, string address, string alt, int width, int height, int zoom)
{
var src = new Uri("http://maps.google.com/maps/api/staticma开发者_开发百科p?markers=size:mid|color:red|{0}&zoom={1}&size={2}x{3}&maptype=roadmap&sensor=false".FormatInvariant(Uri.EscapeUriString(address), zoom, width, height));
var href = new Uri("http://maps.google.com/maps?f=q&source=s_q&hl=en&q={0}".FormatInvariant(Uri.EscapeUriString(address)));
var img = new TagBuilder("img");
img.MergeAttribute("src", src.ToString());
img.MergeAttribute("alt", alt);
var link = new TagBuilder("a") { InnerHtml = img.ToString() };
link.MergeAttribute("href", href.ToString());
return MvcHtmlString.Create(link.ToString());
}
For this new project, we are also trying to keep all code analysis rule on. Now, obviously, Visual Studio Code analysis states that we should remove the helper
parameter because it is not being used.
That made me wondering if an extension method should always make use of the extended object and if it does not, then maybe it shouldn't be an extension method.
Does someone has a link to a guideline or an explication that would help me decide?
If you're not using the helper
object, you'r not extending anything and there's really no reason not to make this a regular static method in your own namespace.
The distinction to the using code between an extension method and a static method is purely conceptual; one relates to an object of a particular type in the same manner as an instance method, and the other does not.
This being so, I would ask the question, "does considering this operation to be upon an HtmlHelper object make sense?". This in turn becomes "does considering HtmlHelper objects to provide this operation, make sense?". If I answered "yes" to that question, I would consider an extension method to be a reasonable approach, whether I used an HtmlHelper object or not. Conversely, if I answered "no" to that question, I would consider an extension method to be an ill-advised approach, even if the HtmlHelper object did get used.
The code-analysis does a better job at analysing code-use than concepts, so it gives a warning here. It's imperfect, and indeed there are other cases where ignoring a parameter is reasonable (maintaining compatibility either with a previous version or an interface being the best case, "reserved for future use" being a more arguable case).
It's notable that in some languages (well, C++ for one) you can leave a parameter name out precisely to mean "this parameter is in the signature, but won't be used". I quite like this, as it is true that generally not using a parameter is a bad sign, so it's nice to have a means to indicate you were deliberate in this.
Edit:
Another justification. Imagine you had an extension method on a class that did use the relevant object. Now imagine you realise you can make it more reliable, efficient, or otherwise "better" for some value of "better" by re-writing in such a way that you are no longer using the object. Should you not be allowed to keep the extension method now that you've improved it? Should you be forced to stay with the inferior version?
Why did you make this an extension method to begin with? I'd say that this is not something you should do. I can't think of a good reason for it.
精彩评论