正在扩展String类与IsNullOrEmpty混淆? [英] Is extending String class with IsNullOrEmpty confusing?

查看:280
本文介绍了正在扩展String类与IsNullOrEmpty混淆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都知道,爱String.IsNullOrEmpty(yourString)方法。

我在想,如果它要迷惑开发商或使code更好,如果我们扩展String类有这样的方法:

  yourString.IsNullOrEmpty();

临:


  1. 更可读。

  2. 少打字。

缺点:


  1. 可能会造成混淆,因为 yourString
    变量可以是和它看起来
    就像你在执行方法
    变量。

你怎么想的?

同样的问题,我们可以询问 myObject.IsNull()方法。

这我怎么会写:

 公共静态类StringExt
{
  公共静态布尔IsNullOrEmpty(此字符串文本)
  {
    返回string.IsNullOrEmpty(文本);
  }  公共静态布尔ISNULL(此对象OBJ)
  {
    返回OBJ == NULL;
  }
}


解决方案

我个人不这样做的粉丝。与扩展方法最大的问题,现在是发现性。 Unleses你平掉了解所有的这些存在于某一特定类型的方法,这是不可能看一个方法调用,并知道这是一个扩展方法。因此我发现问题做了不可能用通常的方法调用的扩展方法任何东西。否则,你最终会混淆开发者。

一个推论这个问题的存在C ++。在一些C ++实现它的可能的,只要你不触及任何类型的字段或虚拟的方式来调用空指针实例方法。我和几件code那做到这一点还故意,给方法differentt行为时,此== NULL 。这是相当令人抓狂的工作。

这是不是说我不喜欢的扩展方法。恰恰相反,我喜欢他们,并经常使用它们。但是我觉得有写他们的时候,你应该遵循两个重要的规则。


  • 治疗的实际方法的实现,如果它只是一个静态的方法,因为它实际上是。例如抛出ArgumentException的,而不是NullReference例外空这个

  • 请不要让扩展方法执行技巧,正常的实例方法不能做

修改回应迈赫达德的评论

与利用它的问题是,我没有看到str.IsNullOrEmpty具有超过String.IsNullOrEmpty(STR)一显著功能优势。我看到的唯一的好处是一个需要比其他类型较少。同样可以对一般的扩展方法可说的。但在这种情况下,你还改变了人们对程序流程的方式。

如果短打字是什么人真正想要不会IsNullOrEmpty(STR)是一个更好的选择吗?它既是明确无误的,是最短的全部。真正的C#有这样的功能今天的支持。但想象一下,如果在C#中,我可以说

 使用SomeNamespace.SomeStaticClass;

这样做的结果是,在SomeStaticClass所有方法在全局命名空间,现在是并且可用于绑定。这似乎是人们想要什么,但他们将其连接到一个扩展方法,我不是一个巨大的风扇。

Everyone knows and love String.IsNullOrEmpty(yourString) method.

I was wondering if it's going to confuse developers or make code better if we extend String class to have method like this:

yourString.IsNullOrEmpty();

Pro:

  1. More readable.
  2. Less typing.

Cons:

  1. Can be confusing because yourString variable can be null and it looks like you're executing method on a null variable.

What do you think?

The same question we can ask about myObject.IsNull() method.

That how I would write it:

public static class StringExt
{
  public static bool IsNullOrEmpty(this string text)
  {
    return string.IsNullOrEmpty(text);
  }

  public static bool IsNull(this object obj)
  {
    return obj == null;
  }
}

解决方案

I'm personally not a fan of doing this. The biggest problem with extension methods right now is discoverability. Unleses you flat out know all of the methods which exist on a particular type, it's not possible to look at a method call and know that it's an extension method. As such I find it problematic to do anything with an extension method that would not be possible with a normal method call. Otherwise you will end up confusing developers.

A corollary to this problem exist in C++. In several C++ implementations it's possible to call instance methods on NULL pointers as long as you don't touch any fields or virtual methods on the type. I've worked with several pieces of code that do this intentionally and give methods differentt behavior when "this==NULL". It's quite maddening to work with.

This is not to say that I don't like extension methods. Quite the contrary, I enjoy them and use them frequently. But I think there are 2 important rules you should follow when writing them.

  • Treat the actual method implementation as if it's just another static method because it in fact is. For example throw ArgumentException instead of NullReference exception for a null this
  • Don't let an extension method perform tricks that a normal instance method couldn't do

EDIT Responding to Mehrdad's comment

The problem with taking advantage of it is that I don't see str.IsNullOrEmpty as having a significant functional advantage over String.IsNullOrEmpty(str). The only advantage I see is that one requires less typing than the other. The same could be said about extension methods in general. But in this case you're additionally altering the way people think about program flow.

If shorter typing is what people really want wouldn't IsNullOrEmpty(str) be a much better option? It's both unambiguous and is the shortest of all. True C# has no support for such a feature today. But imagine if in C# I could say

using SomeNamespace.SomeStaticClass;

The result of doing this is that all methods on SomeStaticClass were now in the global namespace and available for binding. This seems to be what people want, but they're attaching it to an extension method which I'm not a huge fan of.

这篇关于正在扩展String类与IsNullOrEmpty混淆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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