0

I really don't like to call String.IsNullOrEmpty(str). That makes me need to think "String" class first, then call it on the object "str".

I like to call str.IsNullOrEmpty(), which doesn't need me to think "String" class.

The problem is that extension method accept null instance to call on, which is not the usual case when you call a normal method.

My question what do you think will be a convetion name for extension methods that accept null?

  1. For string, that is easy, "IsNullOrEmpty()" (anything that contains "IsNull") sounds good for me.

  2. For others, like GetDisplayName(), do we name it as "NullOrGetDisplayName" ?

0

5 Answers 5

2

Firstly, your thinking is wrong and you should adjust it. It is not correct to think of calling a method on a null object; indeed that's the very reason why you want to see if it is null!

Secondly, a method implies that it accepts null in the parameters it has. I don't think a naming convention is appropriate here. You may be able to eliminate null from your classes via, say, Spec#.

Sign up to request clarification or add additional context in comments.

9 Comments

Thx for your answering. I don't know anything for Spec#. It is ready to use right now? Also, do you mean that I should keep the old way to use String.IsNullOrEmpty(str) and suffer some inconvience in a practical environment (which I think it is totally ok to be safe/defensive programming rather than convenient programming)?
Actually, you might be able to do the same thing via Code Contracts: research.microsoft.com/en-us/projects/contracts which I think (not sure) are included in the latest version of something (.NET 4 or VS 2010 or similar). I think yes you should suffer some inconvenience in order to determine whether or not you should consider eliminating null (it will be more work). FWIW, it takes all of 1 second to type string.IsNullOrEmpty(foo) and generally, on the basis of that, I do something that is "good" (report an error, whatever). So I don't consider it wasteful, in general.
A lot of people reason that it is "evil" to make a call "on a null object", but as long as it is clear to the caller what it means, it's really elegant and safe. There is a long history of using "nulls" in computer programming (e.g. piping data to /dev/null when you want to delete it, in SQL a NULL can be thought of as meaning a database entry is "empty"), and it's a natural fit with collections: Compare the elegance/readability of "if (collection != null && collection.Count > 0)" to "if (collection.IsEmpty)". Is the meaning confusing? I think a null collection intuitively must be empty.
@Jason: Yes, strictly by your initial definition it is confusing to the majoriy. Anything I can invent can be not confusing to me because I convinced myself that it was so. The point of programming in some standard way is that your code is readable. Remember the old saying, code is read more often than written.
@Jason: Yes, for me it's really confusing. In your example I will probably end up writing something like 'if( collection.IsEmpty ) collection.Add( someObject )' Being null and being empty are two completely different things. I still fail to see how nullObject.IsNullOrWhatever() is clearer or more elegant than string.IsNullOrWhatever( nullObject )
|
1

How about NullSafeGetDisplayName or GetDisplayNameNullSafe, indicating that's it's safe to call even when the object it's being called on is null?

Comments

1

I'd go for mystring.IsNullOrEmpty() for methods that can query "null" objects - it is obviously checking if the string is null, and as long as you are consistent with your naming throughout your codebase, nobody will have any trouble understanding/learning what it means.

For methods that apply something to an object and ignore null objects, I'd use "Safe", as in SafeGetDisplayName() - this is a common and long-used standard (c.f. GetSafeHandle() in MFC)

Comments

1

I just don't understand your way of thinking here. Writing something like nullObject.IsNullOrWhatever() simply doesn't make any sense. You may or may not like the static form, but your solution feels really wrong to me.

2 Comments

even with a convention name "NullSafeDoWhatEverYouLike()"?
The problem is invoking a function on a null object. The fact that a language let you do something doesn't always mean that it's correct. bool result = (str != null) && str.AskWhateverYouLike() is something that everyone will understand. Your solution will cause people to scratch their heads until they realize what's going on. It's like drinking water to figure out if I was thirsty or not.
0

The Nullable class has a method named GetValueOrDefault. The same form may be usable for some extension methods: GetDisplayNameOrDefault or perhaps more specific GetDisplayNameOrNull or GetDisplayNameOrEmpty.

1 Comment

I have doubt on this. For example, ((string[]) arStr).FirstOrDefault() also use the "Default" keyword, but it is not safe to call that method on a null instance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.