Which is the better construct?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • v.maggs@comcast.net

    #1

    Which is the better construct?

    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

  • Tom Shelton

    #2
    Re: Which is the better construct?


    v.maggs@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

    Comment

    • Josip Medved

      #3
      Re: Which is the better construct?

      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
      A personal website dedicated to technology, software development, and practical tools.


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Which is the better construct?

        "Josip Medved" <jmedved@jmedve d.comschrieb:
        >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/>

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Which is the better construct?

          Tom,

          :-) I am sure you missed that Or.

          Cor

          "Tom Shelton" <tom@mtogden.co mschreef in bericht
          news:1158866645 .901276.17120@k 70g2000cwa.goog legroups.com...
          >
          v.maggs@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
          >

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Which is the better construct?

            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.maggs@comcas t.netschreef in bericht
            news:1158865456 .926903.110450@ i3g2000cwc.goog legroups.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
            >

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Which is the better construct?

              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

              "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr eef in bericht
              news:usvyi4c3GH A.4164@TK2MSFTN GP05.phx.gbl...
              "Josip Medved" <jmedved@jmedve d.comschrieb:
              >>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/>

              Comment

              • Phill W.

                #8
                Re: Which is the better construct?

                v.maggs@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.

                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: Which is the better construct?

                  "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschri eb:
                  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/>

                  Comment

                  • _AnonCoward

                    #10
                    Re: Which is the better construct?


                    "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
                    news:uzqWl1k3GH A.1608@TK2MSFTN GP04.phx.gbl...
                    :
                    : "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschri eb:
                    : >
                    : 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.


                    Comment

                    • Tom Shelton

                      #11
                      Re: Which is the better construct?


                      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

                      Comment

                      • Cor Ligthert [MVP]

                        #12
                        Re: Which is the better construct?

                        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

                        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr eef in bericht
                        news:uzqWl1k3GH A.1608@TK2MSFTN GP04.phx.gbl...
                        "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschri eb:
                        >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/>

                        Comment

                        • Josip Medved

                          #13
                          Re: Which is the better construct?

                          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
                          A personal website dedicated to technology, software development, and practical tools.


                          Comment

                          • Cor Ligthert [MVP]

                            #14
                            Re: Which is the better construct?

                            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

                            "Josip Medved" <jmedved@jmedve d.comschreef in bericht
                            news:1158959176 .916465.283940@ i3g2000cwc.goog legroups.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
                            A personal website dedicated to technology, software development, and practical tools.

                            >

                            Comment

                            Working...