Is extending String class with IsNullOrEmpty confusing?

后端 未结 12 1963
有刺的猬
有刺的猬 2020-12-06 01:40

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 t

相关标签:
12条回答
  • 2020-12-06 02:28

    I think the root of this problem is what Jon Skeet has mentioned in the list of things he hates in his favorite language (C#): C# should not have imported all extension methods in a whole namespace automatically. This process should have been done more explicitly.

    My personal opinion about this specific question is (since we can't do anything about the above fact) to use the extension method if you want. I don't say it won't be confusing, but this fact about extension methods (that can be called on null references) is a global thing and doesn't affect only String.IsNullOrEmpty, so C# devs should get familiar with it.

    By the way, it's fortunate that Visual Studio clearly identifies extension methods by (extension) in the IntelliSense tooltip.

    0 讨论(0)
  • 2020-12-06 02:33

    It doesn't just look like you're calling a method on a null variable. You /are/ calling a method on a null variable, albeit one implemented through a static extension method. I had no idea extension methods (which I was already leery of) supported that. This even allows you to do crazy things like:

    public static int PowerLength(this string obj)
    {
    return obj == null ? 0 : obj.Length;
    }
    

    From where I'm standing now, I would classify any use of an extension method on a null reference under considered harmful.

    0 讨论(0)
  • 2020-12-06 02:33

    Let's look at the pro:s and con:s...

    1. More readable.

    Yes, slightly, but the improvement in readability is outweighed by the fact that it looks like you are calling an instance method on something that doesn't have to be an instance. In effect it's easier to read, but it's harder to understand, so the improved readability is really just an illusion.

    1. Less typing.

    Yes, but that is really not a strong argument. If typing is the main part of your programming, you are just not doing something that is remotely challenging enough for you to evolve as a developer.

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

    True, (as mentioned above).

    0 讨论(0)
  • 2020-12-06 02:35

    Yes, it will confuse.

    I think that everyone who knows about IsNullOrEmpty() perceives the use of it as quite natural. So your suggested extension method will not add more readability.

    Maybe for someone new to .NET this extension method might be easier to deal with, but there is the danger possibility that she/he doesn't understand all facets of extension methods (like that you need to import the namespace and that it can be invoked on null). She/he will might wonder why this extension method does not exist in another project. Anyway: Even if someone is new to .NET the syntax of the IsNullOrEmpty() method might become natural quite fast. Also here the benefits of the extension methods will not outweight the confusion caused by it.

    Edit: Tried to rephrase what I wanted to say.

    0 讨论(0)
  • 2020-12-06 02:35

    When comparing class instance to null using ".IsNull()" is not even shorter than using " == null". Things change in generic classes when the generic type argument without constraint can be a value type. In such case comparison with default type is lengthy and I use the extension below:

        public static bool IsDefault<T>(this T x)
        {
            return EqualityComparer<T>.Default.Equals(x, default(T));
        }
    
    0 讨论(0)
  • 2020-12-06 02:36

    In general, I'm only ok with extension methods being safe to call on null if they have the word 'null' or something like that in their name. That way, I'm clued in to the fact that they may be safe to call with null. Also, they better document that fact in their XML comment header so I get that info when I mouse-over the call.

    0 讨论(0)
提交回复
热议问题