GPGVM
GPGVM

Reputation: 5619

Error handling in extension methods

So my situation is a little more complex but this will get the point across. We'll use the typical person object.

public class Person()
{
    public string FirstName {get; set; }
    public string LastName {get; set; }
}

Now I have a List of the person object but the typical extension methods aren't enough. I would like to add my own so I create an extension class and the compiler does it's magic.

public static class ListExtension()
{
    public static string FullName(this Person source)
    {
         return source.FirstName + " " + source.LastName;
    }
}

All is well. Now I am concerned with error handling. Do I implement the error handling in the extension or by adding another extension method (I'm thinking like TryParse() ). For example:

    public static string FullName(this Person source)
    {
         if(source.FirstName == null || source.FirstName == "")
         { throw new ArgumentException("The first name is null or empty!"); }

         return source.FirstName + " " + source.LastName;
    }

OR

public static bool TryNameConcat(this Person source)
{
    bool _isSafe = false;

    if(source.FirstName != null && source.LastName != null)
    { _isSafe = true; }

    return _isSafe;
}

The implementation is very different between the two in my mind. It might make no difference in the amount of electrons / cycles etc. but one approach seems proactive and the other reactive?

Edit:

Thank you for the great pointers and comments. As I said in the beginning my situation is more complex. I just cooked up this simple one to get the point across. Do I throw exception in my extension methods or do I proactively do a sanity check before proceeding with an operation that could error.

Upvotes: 0

Views: 1728

Answers (2)

Habib
Habib

Reputation: 223362

First you should question the need of extension method, why not make it an instance method to begin with.

For error checking in extension method, For your particular example, you don't have to check for null, since string.Concat would replace null with empty string. But for delimiter placement between First and Last name you will need the check.

I guess you are dealing with a different situation than simple string concatenation and if you really have to write extension methods (and not instance method) then extracting out functionality in extension methods only makes sense if you are going to use it independently. Like in your case if your method TryNameConcat can be used independently without your method FullName then have the logic in extension method, otherwise don't.

Upvotes: 1

Thomas Weller
Thomas Weller

Reputation: 11717

An extension method is not different from any other (static) method. It's only 'syntactic sugar' that makes it look different, but the compiler resolves it to a normal method. Therefore, you can treat it just like any other method. In other words: throw an exception from your extension method (i.e. go with your first solution), and wrap the method call in a try/catch-block.

HTH Thomas

Upvotes: 1

Related Questions