8

We are implementing some web services, and need to ensure that some of our methods always return non-null values.

We've investigated two ways of doing this:

Both approaches work, but I'm wondering whether there are any other approaches that we should consider.

How would you enforce non-null return values?


Looks like there was a similar question on SO, and Jon Skeet recommends the Code Contract approach.

Peter K.
  • 3,818
  • 1
  • 25
  • 34

1 Answers1

2

If you can be 100% certain that there will always be a valid value to return, then the code contracts method makes perfect sense, and you can go on your way.

Now, if there is a possibility of a situation where the method might not have a valid value to return, then you can still use the code contract method and implement the null object pattern. The issue with the pattern is that you still have to, kind of, check for null but the client will never get a NullReferenceException.

Steven Evers
  • 28,180