.NET Tips and Tricks

Blog archive

The Special Case Pattern

You have a GetCustomerById method in a factory object inside a repository that, when passed a CustomerId, returns a Customer object. Except, of course, sometimes there isn't a matching Customer object to return. What's the best way to handle this?

You have three options:

  1. Raise an exception.
  2. Return Nothing/null
  3. Return a "special object"

Raising an exception has performance implications because everything stops for tea when .NET processes an exception. Really, you should only raise an exception if you intend to stop processing … and that's not a decision that should be made by some method in some middle-tier object. Stopping the application is a decision that should be made by the application itself.

The second option isn't a bad one … except that quite a lot of code doesn't deal well with null references. By returning null/Nothing you're forcing the developer calling your method to wrap it in a Try…Catch block (there's those exceptions again) or test for null. And a null reference doesn't tell the developer much about what went wrong.

Your third option is really your best choice and it even has a name: The Special Case pattern. The only wrinkle is that your special object has to be compatible with the datatype that your method is returning (Customer, in this case). Your special object will, therefore, need to be some class that shares an interface with the objects that your method normally returns.

If, for example, your method returns a Customer object then your special object will need to be something that inherits from Customer -- a CustomerNotFound class, for example. The CustomerNotFound object should have all of its properties set to their default values.

Now code that calls your method can use the datatype of the special object:

Dim cust As Customer
cust = CustomerFactory.GetCustomerById("A123")
If TypeOf cust Is CustomerNotFound Then

to see what kind of error occurred.

Posted by Peter Vogel on 03/04/2016


comments powered by Disqus

Featured

  • Random Neighborhoods Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the random neighborhoods regression technique, where the goal is to predict a single numeric value. Compared to other ML regression techniques, advantages are that it can handle both large and small datasets, and the results are highly interpretable.

  • As Some Orgs Restrict DeepSeek AI Usage, Microsoft Offers Models and Dev Guidance

    While some organizations are restricting employee usage of the new open source DeepSeek AI from a Chinese company due to data collection concerns, Microsoft has taken a different approach.

  • Useful New-ish Features in .NET/C#

    We often hear about the big new features in .NET or C#, but what about all of those lesser known, but useful new features? How exactly do you use constructs like collection indices and ranges, date features, and pattern matching?

  • TypeScript 5.8 Beta Speeds Program Loads, Updates

    "TypeScript 5.8 introduces a number of optimizations that can both improve the time to build up a program, and also to update a program based on a file change in either --watch mode or editor scenarios."

  • AI Toolkit for VS Code Now Lets You 'Bring Your Own Model'

    "AI Toolkit extension for VS code now supports external local models via Ollama. It has also added support remote hosted models using API keys for OpenAI, Google and Anthropic."

Subscribe on YouTube