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
)
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 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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With