Thursday, February 3, 2011

VB.NET - IsNothing versus Is Nothing

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?

  • 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.

    From Adam Haile
  • I'm leaning towards the "Is Nothing" alternative, primarily because it seems more OO.

    Surely Visual Basic ain't got the Ain't keyword.

    peiklk : Can VB.NET do extension methods? :)
    From deadtime
  • @Deadtime: Some guy called Jeff Atwood ain't agreeing with you.

    From GateKiller
  • 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.

    From lomaxx
  • I also tend to use the Is Nothing version partially from using it as much as I do in SQL.

  • I agree with "Is Nothing". As stated above, it's easy to negate with "IsNot Nothing".

    I find this easier to read...

    If printDialog IsNot Nothing Then
    'blah
    End If

    than this...

    If Not obj Is Nothing Then
    'blah
    End If
  • I initially used IsNothing but I've been moving towards using Is Nothing in newer projects, mainly for readability. The only time I stick with IsNothing is if I'm maintaining code where that's used throughout and I want to stay consistent.

  • GateKiller: But that post, I'm pretty sure, ain't serious:

    Microsoft is also reported to be experimenting with "AsIf", "Maybe", and "Totally". In addition, "Catch" will likely be replaced with "Doh!", and "Finally" will be replaced with "Whatever".

    Anyway, this is pretty OT so I'm stfu'ing now.

    Bill : Would love to upvote this, but I also agreed strongly with the topmost answer.
    From deadtime
  • 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.

0 comments:

Post a Comment