I’ve got a bunch of duplicate code that looks like this:
If mValue is Nothing Return ""
Return mValue.ToUpper
I defined the following extension method to reduce duplicate code:
<System.Runtime.CompilerServices.Extension()>
Public Function EmptyIfNull(this As String) As String
If String.IsNullOrEmpty(this) Then Return ""
Return this
End Function
The duplicate code can be re-written as:
Return mValue.EmptyIfNull.ToUpper
Is there a downside to this?
The only downside is that you’re essentially recreating what’s already in the language (the null coalescing operator, or If function, as it’s implemented in VB.NET)
Should do what you’re looking for.
As for your extension method, there’s no need to call
String.IsNullOrEmpty, as you only need to handle the case where it’sNothing.