Open dibley1973 opened 8 years ago
Worth watching the "Null Object Pattern" in PluralSight Patterns and Practice.
This is a short article I was reading about avoiding nulls... No idea how authoritive the author is, but makes sense! http://gsscoder.github.io/2013/03/25/handling-nulls/
@johncollinson2001 , @jadjare
So do we agree to ALWAYS return an IEnumerable
Sorry I've not had the time to watch the links. Are you proposing IEnumerable when it's know there will be Zero or One results? If so this seems odd.
Sorry, I need to do more reading / watching Pluralsight course on the NullObject pattern, monads and the Maybe concept to comment.
Not had much time this week for openRMS, but will catch up soon.
Based on my understanding and discussions (with @dibley1973) I'm concluding that the MayBe monad is currently unnecessary and is not adding much to code readability, e.g. if(result.HasValue)... var item=result.Single() //work on item
is no more, or perhaps less readable than if(item!=null)... //Work on item
Using the NullObject pattern means that we create concrete class representations of null/empty objects and doing so negates the need for a MayBe when returning a single object, e.g.
var item = _itemRepository.Find(1);
//If we care about the presence of the object before acting then...
if(item==Item.Empty) {
//Complain about it
}
//Or else just act on the item and an Empty implementation will swallow up the action / behave appropriately
item.RemoveAllAttributes();
For methods that return a collection they should always return an instantiated collection whether empty or not.
Thus there's never (or rarely) nulls being passed around and calling methods make their own choice whether they care if something's empty/null before acting on it.
Does that meet your expectation @didley1973?
I strongly believe that NULLs are EVIL. I am happy to use the Maybe amplifier, but would also l would like to consider using the "Null Object" pattern to represent the lack of an object