Returning an empty list and Null Continued
Ok, you live and learn. In my earlier post I wrote that I wanted to return null Lists when getting tournaments for an owner.
For example, the functions List<Tournament> GetTournamentsByOwner(Guid ownerGuid) should return NULL if no tournaments where found for that owner in the database. This is wrong, in this case.
First of all, I'm doing this project as a hobby project, and one of the reasons for doing this project is to challenge standard ways of doing things, and instead start with a new, and empty, mind set of how things should be done. I stand corrected, but I still think that in many cases it depends. It is not a given rule all the time. Obviously I have some learning to do about how, and when, I write about stuff in this blog, but I was also wrong about some things. After I read the comments, and answered to them, I continued to discuss this issue with a former colleague of mine. And I also thought about this when I was out on the pub and have a few beers with my girlfriend and our friends.
So my conclusion is this, regarding the former post, and how I will handle it as standard way in the application, but there will be exceptions to that! When I discuss with my former colleague we usually don't settle with arguments as "it is the standard". We want to change the standard. I don't make any stakes that I change the standards, but I want to remind my self while doing this project why a standard is a standard, and how and if it can be evolved. When a function that returns a List<T>, like List<Tournament> GetTournamentsByOwner(Guid ownerGuid) and that functions doesn't have any items, it will return an empty List, not NULL! In this case I was wrong.
When a function returns an entity, like Tournament GetTournamentByGuid(Guid tournamentGuid) and that entity is not found, it will return NULL. My conclusion was that a function that returns a list, is a search function in a way. GetTournamentsBy.. actually searches for a set of tournaments, and if no tournaments are found, a search results is returned, with zero elements. So you might see it like that function returns a search result, not a list of tournaments.
But if I call the function GetTournamentByGuid(Guid tournamentGuid) , I expect a specific tournament, and if this tournament does not exists, NULL will be returned. Some people would argue that I should return a Tournament object with empty values, but I think that is a bad idea. Because I would rather check for NULL than check for the state of the object if it is a real entity or a "placeholder". So in that perspective I rather "break" the standard of never returning NULL.
Labels: c#, Code design
