使用扩展:称重的优点缺点VS [英] Using Extensions: Weighing The Pros vs Cons

查看:120
本文介绍了使用扩展:称重的优点缺点VS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我如何收拾什么,我认为是丑陋的代码问题。一项建议是创建能够执行所需的功能,并返回我想要的扩展方法。我首先想到的是好极了!是如何的酷扩展......但经过多一点思考,我开始对使用扩展...

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 and return back what I wanted. My first thought was 'Great! How cool are Extensions...' but after a little more thinking I am starting to have second thoughts about using Extensions...

我主要担心的是,它看起来像第二个想法扩展是一个定制的快捷键,可以使它很难为其他开发商效仿。我明白使用扩展可以帮助使代码的语法更容易阅读,但对下列幕后的声音是什么?

My main concern is that it seems like Extensions are a custom 'shortcut' that can make it hard for other developers to follow. I understand using an Extension can help make the code syntax easier to read, but what about following the voice behind the curtain?

就拿我以前的问题的代码片段:

Take for example my previous questions code snippet:

if (entry.Properties["something"].Value != null)
  attribs.something = entry.Properties["something"].Value.ToString();

现在的扩展名改为:

public static class ObjectExtensions
{
    public static string NullSafeToString(this object obj)
    {
        return obj != null ? obj.ToString() : String.Empty;
    }
}

和调用:

attribs.something = entry.Properties["something"].Value.NullSafeToString();



Definetely一个方便的路要走,但是否真的值得另一个类对象的开销?会发生什么,如果有人想重用我的代码片段,但不明白扩展?我可以很容易地用相同的结果中使用的语法:

Definetely a handy way to go, but is it really worth the overhead of another class object? And what happens if someone wants to reuse my code snippet but doesn't understand Extension? I could have just as easily used the syntax with the same result:

attribs.something = (entry.Properties["something"].Value ?? string.Empty).ToString()

所以,我做了一个小挖,发现一对夫妇的文章,谈到了使用扩展的优点/缺点。对于那些倾向于看看下面的链接:

So I did a little digging and found a couple of articles that talked about the pros/cons of using Extensions. For those inclined have a look at the following links:

MSDN:扩展方法

扩展方法最佳实践

扩展方法

我真的不能确定哪个是更好的路要走。那我希望他们做以上的自定义什么显示的扩展代码来完成相同的任务?我会很兴趣了解什么是真正的开发者认为这个话题......

I can't really decide which is the better way to go. Custom Extensions that do what I want them to do or more displayed code to accomplish the same task? I would be really interested in learning what 'real' developers think about this topic...

推荐答案

我个人觉得问题扩展方法可读性都大大高估了。如果你专注于使您的代码容易来讲阅读的什么的它在做什么,这是更重要的大部分时间比的如何的它这样做。如果开发者想要追查通过并找出实际发生在幕后,他们总是可以通过点击执行。

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.

我和推广方法的主要问题是他们的发现方法 - 通过指定的命名空间,而不是指定类IE浏览器。这是一个不同的问题,但:)

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 :)

我不建议你把在扩展方法随意,但我会认真考虑你需要多长时间知道如何在每一个表情方法的工作原理VS通过它飞掠,看看它在更广泛的方面

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.

编辑:您使用的术语可能会有些误导你。有作为扩展对象没有这样的事 - 只有扩展方法,他们在静态类型的存在。所以,你可能需要引入一个新的键入的,但你没有创建任何更多的对象

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.

这篇关于使用扩展:称重的优点缺点VS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆