Multiple or Single ifs???

Kokoshaggy
03-31-2004, 06:24 AM
:confused:
Which is better?
1. One If Statement with multiple comparisons such as the following

if X>0 and X<2 and Y=2 and IsEmpty = true then
'statement
end if
OR

if X>0 then
if X<2 then
if Y=2 then
if IsEmpty = true then
'statement
end if
end if
end if
end if

NOT ACTUAL CODE
I want to optimize my code as much as possible

Chris J Locke
03-31-2004, 06:36 AM
The first is easier to read and debug, which in my opinion outweighs any performance gains / losses! Sometimes multiple IFs (although not in this case) can be replaced with Select Case ... End Case structure, which is far superior.

malloc
03-31-2004, 06:40 AM
The first method produces smaller code and looks nicer, the second is a bit faster.

Malloc

Ultimate
03-31-2004, 06:57 AM
:confused:
Which is better?
1. One If Statement with multiple comparisons such as the following

if X>0 and X<2 and Y=2 and IsEmpty = true then
'statement
end if
OR

if X>0 then
if X<2 then
if Y=2 then
if IsEmpty = true then
'statement
end if
end if
end if
end if

NOT ACTUAL CODE
I want to optimize my code as much as possible
Btw, IsEmpty = True isn't a useful comparison.
(I don't know wheter you just did this in the example, or also in your actual code). If IsEmpty then ... Will have the same results, it's a programming 'mistake' which is made very often. The extra comparison with True is just slowing down your application.

Chris J Locke
03-31-2004, 07:55 AM
Sorry - got to pick you up on most of your post!

Btw, IsEmpty = True isn't a useful comparison. Yes it is. If a routine has multiple optional variant parameters, this detects if any are empty, or whether set to an empty string (""). There is a difference.

it's a programming 'mistake' which is made very often. The extra comparison with True is just slowing down your application. Not true. The 'mistake' is actually the proper and correct way of doing it. By removing it, you are taking advantage of 'default' properties, which may change as the software environment changes (some default properties changed from VB5 to VB6 for instance.) Its actually quite bad programming practice, so the original post shows the correct format.
Its also down to personal taste, and how easy you can read this code.

Rick_Fla
03-31-2004, 08:03 AM
This might be more of a question than anything, but if IsEmpty is a boolean, then I was under the impression the following logic is the same:


If IsEmpty = true Then


Same as writing:


If IsEmpty Then


Now if it is NOT boolean, then I can see why you would use the first of the two.

Chris J Locke
03-31-2004, 08:11 AM
It does, but thats not to say that will always be the case...

'If IsEmpty Then' assummes the default option for booleans is TRUE. What happens if this changes to FALSE in the next release of VB?

Optikal
03-31-2004, 08:15 AM
If IsEmpty Then

doesn't assume anything, it looks at the current value of IsEmpty whatever that may be, the default value for a boolean doesn't come into play. If you are just dimming it as boolean and never setting it, then you are assuming it is initialized to false, but reglardless of what method you use in your if statement the result will be the same.

Ultimate
03-31-2004, 08:16 AM
Sorry - got to pick you up on most of your post!

Yes it is. If a routine has multiple optional variant parameters, this detects if any are empty, or whether set to an empty string (""). There is a difference.

Not true. The 'mistake' is actually the proper and correct way of doing it. By removing it, you are taking advantage of 'default' properties, which may change as the software environment changes (some default properties changed from VB5 to VB6 for instance.) Its actually quite bad programming practice, so the original post shows the correct format.
Its also down to personal taste, and how easy you can read this code.
I still can't see any difference (assuming that IsEmpty is a boolean). And I'm not using the environment's default. It's the value of the expression that counts.
As I see it, it's the same as replacing the following:


'this if statement...
If txtMyTextItem.Text = "" then
IsEmpty = true
Else
IsEmpty = False
End if

'could be replaced with...
isEmpty = (txtMyTextItem.Text = "")


The value of the expression (txtMyTextItem.Text = "") will be assigned to isEmpty in this case.

Iceplug
03-31-2004, 08:19 AM
The default option for boolean IS false.
This:
If IsEmpty = True Then
simply makes sure that IsEmpty is True... if IsEmpty is True, then the If statement is taken... if not, then the If statement is avoided.
If IsEmpty Then
takes advantage of the fact that the If Then loop evaluates the previous (IsEmpty = True)...
The return value from IsEmpty = True (if IsEmpty is True) is True... not 1, not 0, not False, but True.
The If IsEmpty Then takes off the extra evaluation condition and goes into the if statement
I don't see it as bad programming practice... because it reads like a sentence when the variable is named nicely:
If Door.IsClosed Then
instead of
If Door.IsClosed = True Then :-\
But that's my opinion. Both statements do the same thing... one just does an extra check. :)

lebb
03-31-2004, 08:34 AM
Back to your original question, I agree with malloc. However, if there is a chance that one of those checks could produce an error, or only needs to be done under certain conditions, then you need to break it apart into separate If statements because VB doesn't do short-circuit evaluation like some other languages such as C. All the conditions will be evaluated, regardless of the others.

For example,
If Not MyObject Is Nothing And MyObject.Value > 0 Then
would obviously produce an error when MyObject hadn't been initialized, because even if (MyObject Is Nothing) evaluated to true, it would still try to access the .Value property which would then fail. So in this case, you would need to use
If Not MyObject Is Nothing Then
If MyObject.Value > 0 Then

Kokoshaggy
03-31-2004, 10:57 AM
:) Thanks Guys. I think I'll go for a combination of both.

It's amusing to see how a simple question turned into a debate about programming practices.

Once again thanks.

Anthony

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum