-1

Do Special Case or Null Object design patterns still provide value when application behavior, not just object behavior has to change?

I was tasked with revisiting an old application and refactoring its code to better log/handle errors.

This application is a .NET MVC API written in C# that handles readonly data. Since it is readonly I originally thought it was a good candidate for using Null or Special Case Objects, when returning data from the service layer. The problem is when an error happens creating these special case objects application behavior should be affected. It is an API it should return relevant HttpStatus Codes. This means behavior needs to change not only at the source of the exception but also during presentation of the response.

I have seen code that uses explicit type checking or does object inspection to determine whether application behavior should change. Making type checking regular practice seems especially bad in C#. I'm also hesitant about using domain objects to determine state it seems like blurring responsibilities, even if they are special case.

I believe I am going to go with a different solution, probably a generic response wrapping the actual data payload. However I now am wondering are there any good cases anyone can think of where one would still want to use a Null or Special Case Object to handle exceptions, when exceptions require a change in application behavior past where the exception occurred.

After exploring the possibility of using Null or Special Case Objects it seems like its uses are limited to when application behavior is completely independent of the response you might get from a service/method call. If it is required to act differently I can't see myself using Null or Special Case Object. If anyone has had different experience please let me know, because it seems this is a common pattern and I may be missing/misunderstanding something.

  • 1
    You need to be more specific about your requirements. If this is a JSON web service, do you need it to return a JSON response indicating special-case errors? That can certainly be done. – Robert Harvey Nov 8 '17 at 20:06
  • It returns JSON or XML. Yes Special case could return JSON with information about what went wrong. My question is since application behavior has to change, not just that objects behavior why would I want to use it? Imagine instead you have a User Object and a special case InvalidUser but business rules require you to redirect to a login page whenever there is an invalid user what do I gain from the special case object? – PieMaker Nov 8 '17 at 20:15
  • Redirection wouldn't occur in the JSON web service. You would have to do that somewhere else. – Robert Harvey Nov 8 '17 at 20:17
  • I'm not sure what type checking or "special case objects" has to do with this. Just write code that detects the invalid user from the JSON result and performs a redirect. Are you wondering how to do this? – Robert Harvey Nov 8 '17 at 20:18
  • 1
    I'm not clear on what you mean by "special case object." – Robert Harvey Nov 8 '17 at 20:25
1

If you are using a RESTful approach, your messaging should conform to the Uniform Interface.

This means that if a client attempts to GET a resource that is not present, the proper response would be HTTP 404 NOT FOUND.

If a client makes a request that makes the server fall over, the proper response would probably be HTTP 500 Internal Server Error.

If a client makes a request that is not authorized, some flavor of 403.x might be appropriate.

Null objects and special case objects are more likely to be useful in internal API's, e.g. simple method calls to PoC objects, or calls that do not cross subsystem boundaries. Returning a null object especially over a service to me would seem very poor design.

  • There's not much diagnostic information in those responses. – Robert Harvey Nov 9 '17 at 2:08
  • There's as much content as you want (you can include an HTTP body with an HTTP 500 header, for example). But if you get a 404 I'm not sure there's much more to say, assuming the service uses a meaningful URI system. – John Wu Nov 9 '17 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.