Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "null this" an acceptable use of extension methods? [closed]

So, I really enjoy using extension methods.. maybe a bit too much. So, I'm going to ask about my latest enjoyment to ensure that I'm not going too far.

Scenario is that we have a Guid? variable that gets passed in. If the variable is null or Guid.Empty, then we want to use a different Guid. So, I wrote an extension method to make it read like English:

    internal static Guid OrIfEmpty(this Guid? guid, Guid other)
    {
        if (!guid.HasValue || guid.Value == Guid.Empty)
        {
            return other;
        }
        return guid.Value;
    }

This automatically implies that a "null this" will not throw an exception. For instance, this will work:

((Guid?)null).OrIfEmpty(other);

This is not possible without using extension methods, and in my opinion can be quite misleading. However, it's just so concise and clean! So, what do you think? Is this an acceptable thing to do or could it be too confusing to other programmers?

Also, I'm sure there will be other scenarios where I do things like this and checking this for null, but this is the best example I have right now.

Note: I'm not really asking about this Guid? business in particular. I'm asking more about the overall pattern implemented (having an extension method where the this can be null)

like image 567
Earlz Avatar asked Jan 28 '13 15:01

Earlz


People also ask

Can you call extension method on null?

Through the use of extension methods a lot of cases can be handled. As extension methods are in reality static methods of another class, they work even if the reference is null . This can be utilized to write utility methods for various cases.

When would you not use an extension method?

When you're not sure which Type is the one to extend, don't use extension methods. For example, to build a house from brick and mortar I can extend brick with brick. BuildHouse(mortar) , or I can extend mortar with mortar.

What is correct extension method?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What is extension method with example?

An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example.


1 Answers

This is not possible without using extension methods

Sure it is, just make is a regular static method call (which is all extension methods are):

in my opinion can be quite misleading

I agree, since it looks like you're calling an instance method on a null instance. It could be worse:

string s = null;
string n = s.OrIfEmpty("empty");

At first glance this looks like an obvious NullReferenceException waiting to happen, but it compiles and works as designed.

Since your question is really just soliciting opinions, there's not one right answer, but I certainly would be cautious and document the extension method to indicate that the this parameter could be null. Or (as @quezalcoatl implies) rename it to be more explicit that it supports null values:

internal static Guid OrIfNullOrEmpty(this Guid? guid, Guid other)
like image 193
D Stanley Avatar answered Oct 18 '22 04:10

D Stanley