c# - Is extending String class with IsNullOrEmpty confusing? -


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

I was wondering if this would make the string class a method in such a way as to mislead the developers or improve the code:

  yourString.IsNullOrEmpty ();  

PRO:

  1. More readable.
  2. Less typing.
  3. Cons:

    1. Can be misleading because yourstring variable null and it looks like you are executing the method on the null variable.

    What do you think?

    The same question as we MyObject.IsNull () method.

    How will I write it:

      public static class StringExt {public static bool isNullOrEmpty (this string text) {return string} INLLIRAlight (text); } Public Static Bull Ianal (This Object OBJE) {Obz == Blank; }}  

I am not a fan to personally do this. The biggest problem with the expansion methods is the search capability. You know all the methods without any specific types of methods, it is not possible to see a method call and know that this is an extension method. As if I find problematic with doing anything with any extension method which would not be possible with normal method call. Otherwise you will end up confusing developers.

A result of this problem exists in C ++. In many C ++ implementations, it is possible to call the example methods on the null pointer unless you touch on any field or type of virtual methods. I have worked with several pieces of code that deliberately do it and give methods different ways when " this == tap / code>". It is quite upset to work with it.

It does not say that I do not like extension methods. On the contrary, I enjoy them and often use them. But I think that you should follow two important rules while writing.

  • Dealing with the actual method implementation as if it is just another static method because it really is. For example, the NullReference throws instead of the exception to ArgumentException
  • An extension method can not be a common example method which

    edit < / Strong> Answer to Mehrday's comment

    The problem with taking advantage of this is that I do not see it as a significant functional benefit on the Strong.ISNLLacti (ARR). I see that the only advantage is that it is necessary to type less than the other, in general it can be said about the extension methods in general. But in this case, you are also turning people who think about the flow of the program in return.

    If less is to be typed then people really want IsNullOrEmpty (str) will not be a better option? This is both obvious and the smallest of all. There is no support for such a feature today but if I can say something in C #

    The result of doing so is that all methods on someStaticClass were now in the global namespace and are available for binding. It seems like what people want, but they are enclosing it with an extension method, which I am not a big fan.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -