Home All Groups Group Topic Archive Search About

Which is the better construct?

Author
21 Sep 2006 7:04 PM
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

Author
21 Sep 2006 7:24 PM
Tom Shelton
v.ma***@comcast.net wrote:
Show quoteHide quote
> 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.

--
Tom Shelton
Author
22 Sep 2006 5:07 AM
Cor Ligthert [MVP]
Tom,

:-) I am sure you missed that Or.

Cor

Show quoteHide quote
"Tom Shelton" <t**@mtogden.com> schreef in bericht
news:1158866645.901276.17120@k70g2000cwa.googlegroups.com...
>
> v.ma***@comcast.net wrote:
>> 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.
>
> --
> Tom Shelton
>
Author
22 Sep 2006 3:54 PM
Tom Shelton
Cor Ligthert [MVP] wrote:
> Tom,
>
> :-) I am sure you missed that Or.
>
> Cor

Yes, I did.  I only saw the parens.  That's what comes of not doing VB
every day :)

--
Tom Shelton
Author
21 Sep 2006 7:25 PM
Josip Medved
> 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

--
Pozdrav,
  Josip Medved
  http://www.jmedved.com
Author
21 Sep 2006 10:34 PM
Herfried K. Wagner [MVP]
"Josip Medved" <jmed***@jmedved.com> schrieb:
>> 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

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

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
22 Sep 2006 5:11 AM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
news:usvyi4c3GHA.4164@TK2MSFTNGP05.phx.gbl...
> "Josip Medved" <jmed***@jmedved.com> schrieb:
>>> 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
>
> ACK, that's what I'd use too.  Most developers coming from VB6 still use
> 'Or' even if 'OrElse' makes more sense...
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
22 Sep 2006 1:45 PM
Herfried K. Wagner [MVP]
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
22 Sep 2006 3:38 PM
_AnonCoward
Show quote Hide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:uzqWl1k3GHA.1608@TK2MSFTNGP04.phx.gbl...
:
: "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
: >
: > 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
--
--
----------------------------------------------------------
*             ^~^                   ^~^                  *
*          _ {~ ~}                 {~ ~} _               *
*         /_``>*<                   >*<''_\              *
*        (\--_)++)                 (++(_--/)             *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Author
22 Sep 2006 4:32 PM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
news:uzqWl1k3GHA.1608@TK2MSFTNGP04.phx.gbl...
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
>> 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.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
22 Sep 2006 9:06 PM
Josip Medved
> 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. :)

--
Greetings,
  Josip Medved
  http://www.jmedved.com
Author
23 Sep 2006 5:38 AM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Josip Medved" <jmed***@jmedved.com> schreef in bericht
news:1158959176.916465.283940@i3g2000cwc.googlegroups.com...
>> 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. :)
>
> --
> Greetings,
>  Josip Medved
http://www.jmedved.com
>
Author
22 Sep 2006 5:12 AM
Cor Ligthert [MVP]
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

<v.ma***@comcast.net> schreef in bericht
Show quoteHide quote
news:1158865456.926903.110450@i3g2000cwc.googlegroups.com...
>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
>
Author
22 Sep 2006 10:52 AM
Phill W.
v.ma***@comcast.net wrote:
> 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.