![]() ![]() However, as the poster in the SO answer noted, nulls should probably be returned if an object is expected so that there is no doubt about whether data is being returned. However, you'll need to make sure that the group that your working with will follow the same convention - otherwise the benefits of this decision won't be achieved. Personally, I like to return empty strings for functions that return strings to minimize the amount of error handling that needs to be put in place. Accessing members of an empty object will not fail Highlighting buggy code - attempting to access a member of nothing Returning null is usually the best idea if you intend to indicate thatĪn empty object implies data has been returned, whereas returning nullĬlearly indicates that nothing has been returned.Īdditionally, returning a null will result in a null exception if youĪttempt to access members in the object, which can be useful for StackOverflow has a good discussion about this exact topic in this Q&A. ![]() So why not just return NULL here and then the caller only has to check for NULL.ĭoes anyone else struggle with this? Any help or insight is appreciated. Regardless of whether or not NULL or an empty Person Object ( new Person()) is returned the caller is going to have to check to see if the Person Object is NULL or empty before doing anything to it (like calling UpdateName()). ![]() In FindPerson(), returning NULL seems like a better fit. Also, this way, the caller would not have to check to see if a NULL was returned. In ReverseString(), I would say return an empty string because the return type is string, so the caller is expecting that. Person FindPerson(int personID) // finds a Person with a matching personID. Take the following two methods as an examples: string ReverseString(string stringToReverse) // takes a string and reverses it. I am struggling with whether it is better to return NULL or an empty value from a method when the return value is not present or cannot be determined. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |