coding tip to prevent comparisons becoming assignments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Michaels

    coding tip to prevent comparisons becoming assignments

    This is a simple coding tip I learned to prevent comparisons from becoming
    assignments (and this becoming a hair-pulling session during debugging):

    in comparisons, try to put your constants on the left hand side of the
    operator and variables on the right hand side.
    example
    if (3==$x) {
    //do something
    }



    if ($x=3) {
    //do something
    }
    if this were intended as a comparison, it would never be flagged as an error
    by the interpreter or compiler (in PHP's case, interpreter).
    With this line, I missed an = and the interpreter won't complain. it simply
    assigns 3 to $x and does something every time. With the top line, you can
    fix your code quickly because of the error message generated if you miss an
    =.
    Consider it a good coding habit? Then again there are books on this (if
    they are still published).


  • Ian Collins

    #2
    Re: coding tip to prevent comparisons becoming assignments

    Jim Michaels wrote:[color=blue]
    > This is a simple coding tip I learned to prevent comparisons from becoming
    > assignments (and this becoming a hair-pulling session during debugging):
    >
    > in comparisons, try to put your constants on the left hand side of the
    > operator and variables on the right hand side.
    > example
    > if (3==$x) {
    > //do something
    > }
    >
    >
    >
    > if ($x=3) {
    > //do something
    > }
    > if this were intended as a comparison, it would never be flagged as an error
    > by the interpreter or compiler (in PHP's case, interpreter).
    > With this line, I missed an = and the interpreter won't complain. it simply
    > assigns 3 to $x and does something every time. With the top line, you can
    > fix your code quickly because of the error message generated if you miss an
    > =.
    > Consider it a good coding habit?[/color]

    It's required in some shops. In others, it's a matter of taste. Decent
    unit tests also help prevent this problem.

    --
    Ian Collins.

    Comment

    • Peter Fox

      #3
      Re: coding tip to prevent comparisons becoming assignments

      Following on from Jim Michaels's message. . .[color=blue]
      >This is a simple coding tip I learned to prevent comparisons from becoming
      >assignments (and this becoming a hair-pulling session during debugging):
      >
      >in comparisons, try to put your constants on the left hand side of the
      >operator and variables on the right hand side.
      >example
      >if (3==$x) {
      > //do something[/color]

      Here is a precis of my standard lecture (shortly to be a book) on
      equals.


      The use of = and == is *full* of traps

      $totalCake = 100;
      $slices = 7;
      $slice = $totalCake / $slices;
      $cakeLeft = $totalCake;
      while ($cakeLeft != 0){ // will this loop terminate? Always?
      $cakeLeft = $cakeLeft - $slice;
      }
      // Crumbs!!



      A way to 'burn-in' mental processes in order to distinguish = and == is
      to 'say' "=" as one word and "==" as two. For example "equals" and "is
      equal" (or "equals" and "equal to") and to get into the habit of
      'saying' conditional statements using the two word form.
      $a =[equals] $b;
      if($a =[equal]=[to] $b){ ...



      Are we asking if $foo and $bar are the same object or do all the
      properties of $foo match those of $bar? Do you have the same car as me?
      Does this mean "Do we own the same bit of metal?" I have a document
      which I photocopy and give the copy to you. Do we have the same
      document? Yes and No. Yes : Same words. No : Different bit of paper.





      --
      PETER FOX Not the same since the deckchair business folded
      peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
      2 Tees Close, Witham, Essex.
      Gravity beer in Essex <http://www.eminent.dem on.co.uk>

      Comment

      • Oli Filth

        #4
        Re: coding tip to prevent comparisons becoming assignments

        Peter Fox said the following on 21/03/2006 09:22:[color=blue]
        >
        > Here is a precis of my standard lecture (shortly to be a book) on equals.
        >
        >
        > The use of = and == is *full* of traps
        >
        > $totalCake = 100;
        > $slices = 7;
        > $slice = $totalCake / $slices;
        > $cakeLeft = $totalCake;
        > while ($cakeLeft != 0){ // will this loop terminate? Always?
        > $cakeLeft = $cakeLeft - $slice;
        > }
        > // Crumbs!![/color]


        How does that demonstrate the pitfalls of = and == ?


        --
        Oli

        Comment

        • David Haynes

          #5
          Re: coding tip to prevent comparisons becoming assignments

          Jim Michaels wrote:[color=blue]
          > This is a simple coding tip I learned to prevent comparisons from becoming
          > assignments (and this becoming a hair-pulling session during debugging):
          >
          > in comparisons, try to put your constants on the left hand side of the
          > operator and variables on the right hand side.
          > example
          > if (3==$x) {
          > //do something
          > }
          >
          >
          >
          > if ($x=3) {
          > //do something
          > }
          > if this were intended as a comparison, it would never be flagged as an error
          > by the interpreter or compiler (in PHP's case, interpreter).
          > With this line, I missed an = and the interpreter won't complain. it simply
          > assigns 3 to $x and does something every time. With the top line, you can
          > fix your code quickly because of the error message generated if you miss an
          > =.
          > Consider it a good coding habit? Then again there are books on this (if
          > they are still published).
          >
          >[/color]
          One issue is that it only works if the left-hand side is a constant.

          -david-

          Comment

          • Geoff Berrow

            #6
            Re: coding tip to prevent comparisons becoming assignments

            Message-ID: <yYY8TIACX8HEFw 1+@eminent.demo n.co.uk> from Peter Fox
            contained the following:
            [color=blue]
            >A way to 'burn-in' mental processes in order to distinguish = and == is
            >to 'say' "=" as one word and "==" as two. For example "equals" and "is
            >equal" (or "equals" and "equal to") and to get into the habit of
            >'saying' conditional statements using the two word form.
            >$a =[equals] $b;
            >if($a =[equal]=[to] $b){ ...[/color]

            A better way is not to mention equals at all

            = is the assignment operator
            == is the comparison operator

            $a=[takes the value of]$b;
            if($a ==[has the same value as] $b){ ...

            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • Peter Fox

              #7
              Re: coding tip to prevent comparisons becoming assignments

              Following on from Oli Filth's message. . .[color=blue]
              >Peter Fox said the following on 21/03/2006 09:22:[color=green]
              >>
              >> Here is a precis of my standard lecture (shortly to be a book) on equals.
              >>
              >>
              >> The use of = and == is *full* of traps
              >>
              >> $totalCake = 100;
              >> $slices = 7;
              >> $slice = $totalCake / $slices;
              >> $cakeLeft = $totalCake;
              >> while ($cakeLeft != 0){ // will this loop terminate? Always?
              >> $cakeLeft = $cakeLeft - $slice;
              >> }
              >> // Crumbs!![/color]
              >
              >
              >How does that demonstrate the pitfalls of = and == ?
              >
              >[/color]
              The same way as "In tonight's programme we'll talk to Hugh Jampton,
              captain of Britain's five-a-side archery team *and* later visit the new
              indoor hang gliding centre at Little Bagworth. But first here are the
              results of today's Woolworth's League (division 2) downhill crochet
              matches."

              Hope that explains the use of "and".


              --
              PETER FOX Not the same since the adhesive company came unstuck
              peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
              2 Tees Close, Witham, Essex.
              Gravity beer in Essex <http://www.eminent.dem on.co.uk>

              Comment

              • Jim Michaels

                #8
                Re: coding tip to prevent comparisons becoming assignments


                "David Haynes" <david.haynes2@ sympatico.ca> wrote in message
                news:%UQTf.1142 39$kp3.46045@fe 48.usenetserver .com...[color=blue]
                > Jim Michaels wrote:[color=green]
                >> This is a simple coding tip I learned to prevent comparisons from
                >> becoming assignments (and this becoming a hair-pulling session during
                >> debugging):
                >>
                >> in comparisons, try to put your constants on the left hand side of the
                >> operator and variables on the right hand side.
                >> example
                >> if (3==$x) {
                >> //do something
                >> }
                >>
                >>
                >>
                >> if ($x=3) {
                >> //do something
                >> }
                >> if this were intended as a comparison, it would never be flagged as an
                >> error by the interpreter or compiler (in PHP's case, interpreter).
                >> With this line, I missed an = and the interpreter won't complain. it
                >> simply assigns 3 to $x and does something every time. With the top line,
                >> you can fix your code quickly because of the error message generated if
                >> you miss an =.
                >> Consider it a good coding habit? Then again there are books on this (if
                >> they are still published).[/color]
                > One issue is that it only works if the left-hand side is a constant.
                >
                > -david-
                >[/color]

                yeah. I know. the rest is visual and normal debug methods, like the point
                you mention. but, it's there for whatever it's worth.


                Comment

                Working...