Spot the newbie error?

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
166
Location
Chicago
Programming Experience
10+
Hi

Ok, here is my strange problem that i know I must be spacing out on: :D

I am trying to detect when an object is nothing, and if so, just skip along (It is in a loop that is not shown). What is the difference between these 2 code samples?

Sample 1:
VB.NET:
If link.InnerHtml Is Nothing = True Then
   Console.WriteLine("Object was nothing")
Else
   If link.InnerHtml.Contains("Pro") = False Then
     ' Do stuff
   End If
End If

Sample 2:
VB.NET:
 If link.InnerHtml Is Nothing = False And link.InnerHtml.Contains("Pro") = False Then
     ' Do stuff
End If

I am preferring to use Sample 2 because it's a bit cleaner.

Sample 1 is working as intended, when it comes into an object that is nothing, it does nothing and we end up looping back around again.

Sample 2, which seems to be the same thing to me as Sample 1, is not working as intended, and in my try Catch block, goes to the exception which tells me I need to either use New or check for Null.

How come? :p
 
Use AndAlso operator.
In a Boolean comparison, the And operator always evaluates both expressions, which could include making procedure calls. The AndAlso Operator performs short-circuiting, which means that if expression1 is False, then expression2 is not evaluated.
Also use IsNot operator.
VB.NET:
If (link.InnerHtml IsNot Nothing) AndAlso (link.InnerHtml.Contains("Pro") = False) Then
 
Kudos! I'm learning something new everyday! :)

tip: You dont need to equate a boolean something to true or false to determine if it is true or false.

Think about it.. You write:

If person.Age < 18 Then 'not old enough

you dont write:

If (person.Age < 18) = True Then 'not old enough
 
Perhaps, I didn't give the expressions much thought, except the operators. But I think "Not Contains" is easier to read than "Contains = False" when I look at it now. I use Boolean expressions (properties & methods) all the time without writing "= True/False" and think the expressions are easier to read when the members have meaningful names.
 
Correct, you don't have to but it can make code a little easier to read. As there is no performance hit, it doesn't matter either way.

How about this for clutter:

If (Not ((person.Age > 18) = False)) = True Then

;)


-



This is generally more how we'd say a particular phrase in english:

If x IsNot Nothing AndAlso (Not x.Contains("x")) Then

Than this:

If x Is Nothing = False AndAlso x.Contains("x") = False Then



Generally booleans should be positive or optimistic.. Dont write boolean names witha negative or pessimistic tone:

Dim isValid as Boolean

'if it's valid
If isValid Then


versus:

Dim isntValid As Boolean

'if its valid
If Not isntValid Then
If isntValid = False Then


The human brain isnt typically used to dealing woth double negation, so the meaning can be more easily assessed if the tone is positive and negated once, than using a double negation..
I do take the point that sometimes testing for equality with false can make code more readable, but generally, you'd assign a positive slant and just say:

If positiveSlant Then
 
Back
Top