Does anyone here use VB.NET and have a strong preference for or against using IsNothing
as opposed to Is Nothing
(for example, If IsNothing(anObject)
or If anObject Is Nothing...
)? If so, why?
EDIT: If you think they're both equally acceptable, do you think it's best to pick one and stick with it, or is it OK to mix them?
I find that Patrick Steele answered this question best on his blog: Avoiding IsNothing()
I did not copy any of his answer here, to ensure Patrick Steele get's credit for his post. But I do think if you're trying to decide whether to use Is Nothing or IsNothing you should read his post. I think you'll agree that Is Nothing is the best choice.
Edit - VoteCoffe's comment here
Partial article contents: After reviewing more code I found out another reason you should avoid this: It accepts value types! Obviously, since IsNothing() is a function that accepts an 'object', you can pass anything you want to it. If it's a value type, .NET will box it up into an object and pass it to IsNothing -- which will always return false on a boxed value! The VB.NET compiler will check the "Is Nothing" style syntax and won't compile if you attempt to do an "Is Nothing" on a value type. But the IsNothing() function compiles without complaints. -PSteele – VoteCoffee
I'm leaning towards the "Is Nothing" alternative, primarily because it seems more OO.
Surely Visual Basic ain't got the Ain't keyword.
If you take a look at the MSIL as it's being executed you'll see that it doesn't compile down to the exact same code. When you use IsNothing() it actually makes a call to that method as opposed to just evaluating the expression.
The reason I would tend to lean towards using "Is Nothing" is when I'm negating it becomes "IsNot Nothing' rather than "Not IsNothing(object)" which I personally feel looks more readable.
You should absolutely avoid using
IsNothing()
Here are 4 reasons from the article IsNothing() VS Is Nothing
Most importantly,
IsNothing(object)
has everything passed to it as an object, even value types! Since value types cannot beNothing
, it’s a completely wasted check.Take the following example:
This will compile and run fine, whereas this:
Will not compile, instead the compiler will raise the error:
IsNothing(object)
is actually part of part of theMicrosoft.VisualBasic.dll
.This is undesirable as you have an unneeded dependency on the VisualBasic library.
Its slow - 33.76% slower in fact (over 1000000000 iterations)!
Perhaps personal preference, but
IsNothing()
reads like a Yoda Condition. When you look at a variable you're checking it's state, with it as the subject of your investigation.i.e. does it do x? --- NOT Is
x
ing a property of it?So I think
If a IsNot Nothing
reads better thanIf Not IsNothing(a)
I agree with "Is Nothing". As stated above, it's easy to negate with "IsNot Nothing".
I find this easier to read...
than this...
VB is full of things like that trying to make it both "like english" and comfortable for people who are used to languages that use () and {} a lot. For example, on the "like english"side...VB has the "Ain't" keyword...and no, I'm not joking. And on the other side, as you already probably know most of the time you can use () with function calls if you want to, but don't have to.
I prefer IsNothing()...but I use C and C#, so that's just what is comfortable. And I think it's more readable. But go with whatever feels more comfortable to you.