Which is the better construct?

  • Thread starter Thread starter v.maggs
  • Start date Start date
V

v.maggs

I am curious to know which IF statement below is better. strQCType
could be "LCS", "MS", "REG", "LD", or "LB". I want the code within the
IF statements to execute only in the event of an "LCS" or "MS".

If ((strQCType = "LCS") Or (strQCType = "MS")) Then

End If


-OR-


If (strQCType = "LCS") Or (strQCType = "MS") Then

End If

Thanks,
Vint
 
I am curious to know which IF statement below is better. strQCType
could be "LCS", "MS", "REG", "LD", or "LB". I want the code within the
IF statements to execute only in the event of an "LCS" or "MS".

If ((strQCType = "LCS") Or (strQCType = "MS")) Then

End If


-OR-


If (strQCType = "LCS") Or (strQCType = "MS") Then

End If

Thanks,
Vint

Personally, it makes no difference.
 
I am curious to know which IF statement below is better. strQCType
could be "LCS", "MS", "REG", "LD", or "LB". I want the code within the
IF statements to execute only in the event of an "LCS" or "MS".

If I am not missing something, they are the same. That brackets don't
make the difference. Quicker version would be:

If (strQCType = "LCS") OrElse (strQCType = "MS") Then

End If
 
Josip Medved said:
If I am not missing something, they are the same. That brackets don't
make the difference. Quicker version would be:

If (strQCType = "LCS") OrElse (strQCType = "MS") Then

End If

ACK, that's what I'd use too. Most developers coming from VB6 still use
'Or' even if 'OrElse' makes more sense...
 
Herfried,

Can you explain too me why you are using this and not simple.

If strQCType = "LCS" OrElse strQCType = "MS" Then

I am curious about you answer, I don't see any benefit from what (you tell)
you are doing.

Cor
 
Hi,

Beside the OrElse and removing all parenthises you have to investigate what
is used the most, that has be the first thing to investigate, that will at
least save you 1 picosecond when 1000000000000 times used.

However, all kind of investigating time used in this kind of question can in
my idea never be gained by the time the program is going faster.

Just my thought.

Cor
 
I am curious to know which IF statement below is better. strQCType
could be "LCS", "MS", "REG", "LD", or "LB". I want the code within the
IF statements to execute only in the event of an "LCS" or "MS".

Personally, I wouldn't use an If for this at all!

Select Case strQCType
Case "LCS", "MS"
' Do Useful stuff
Case Else
' Do Something Else
End Select

IMHO it's more readible and, when you later decide that you need to do
this processing for "REG" as well, it's far easier to change reliably.

HTH,
Phill W.
 
Cor Ligthert said:
Can you explain too me why you are using this and not simple.

If strQCType = "LCS" OrElse strQCType = "MS" Then

I am curious about you answer, I don't see any benefit from what (you
tell) you are doing.

I would not write the '(...)' too, but writing the '(...)' may improve
readability for those who do not have the exact rules for operator
precedence in their mind.
 
:
: >
: > Can you explain too me why you are using this and not simple.
: >
: > If strQCType = "LCS" OrElse strQCType = "MS" Then
: >
: > I am curious about you answer, I don't see any benefit from what (you
: > tell) you are doing.
:
: I would not write the '(...)' too, but writing the '(...)' may improve
: readability for those who do not have the exact rules for operator
: precedence in their mind.


I tend to do that. The (..) are superfluous, but they do clarify what is
happening. And I'm all for being clear about my intentions when coding.


These two statement are equivalent:


[1] If Not A AndAlso B Then


[2] If (Not A) AndAlso (B) Then


However, I find the second contruction to be clearer. It explicitly prevents
the reader from interpreting this as


[3] If Not (A AndAlso B) Then


If I did in fact intend this third implementation, it is immediately obvious
if that is what I achieved. I don't know how many times I've had to debug
code where I've run into just this type of error. Adding the (...) would
have prevented that right up front.


I don't always bother for small, throw away code. But when I'm working in a
team environment, I personally like using (...), superfluous or not, because
it makes my intentions clear to the other coders.


Ralf
 
Herfried,

A good question for you , all that is inside a () should be processed
first.

Is this processed before the check on OrElse.

In my opinion it should but than be not direct not wanted behaviour.

Just my thought, and a nice question for you in my idea.

Cor
 
A good question for you , all that is inside a () should be processed
first.
Is this processed before the check on OrElse.
In my opinion it should but than be not direct not wanted behaviour.
Just my thought, and a nice question for you in my idea.

Inside of all () is not processed before OrElse.

If (something1) OrElse (something2) Then

and

If something1 OrElse something2 Then

are completely the same. You can test it. :)
 
Josip,

Thanks

I would have done it, but yesterday I had no time for this, and if you say
it, why would I not believe you?

Cor
 
Back
Top