473,287 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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).
Mar 21 '06 #1
7 1378
Jim Michaels wrote:
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?


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

--
Ian Collins.
Mar 21 '06 #2
Following on from Jim Michaels's message. . .
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


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
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Mar 21 '06 #3
Peter Fox said the following on 21/03/2006 09:22:

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!!

How does that demonstrate the pitfalls of = and == ?
--
Oli
Mar 21 '06 #4
Jim Michaels wrote:
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).

One issue is that it only works if the left-hand side is a constant.

-david-

Mar 21 '06 #5
Message-ID: <yY**************@eminent.demon.co.uk> from Peter Fox
contained the following:
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){ ...


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/
Mar 21 '06 #6
Following on from Oli Filth's message. . .
Peter Fox said the following on 21/03/2006 09:22:

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!!

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

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
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Mar 22 '06 #7

"David Haynes" <da***********@sympatico.ca> wrote in message
news:%U********************@fe48.usenetserver.com. ..
Jim Michaels wrote:
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).

One issue is that it only works if the left-hand side is a constant.

-david-


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.
Mar 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

102
by: RFox | last post by:
I date back to the early days of the web when HTML was limited but very managable, and have always maintained that hand-coding HTML gives you far better control and cleaner HTML markup than any...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
7
by: Ralph Lund | last post by:
Hi. I am starting a new project with C#. I am searching for "good" coding conventions. I know that there are some coding conventions from microsoft, (but they are very extensive and not clear)....
16
by: Alfred Taylor | last post by:
I've been flipping through various coding standards documents for C#/.NET and all of them say to use: if ( boolVar ) {...} instead of if ( boolVar == true ) {...} For the life of my I can't...
0
by: Tim Dawson | last post by:
Hello, I develop commercial windows forms controls in C#. It was recently brought to my attention that the visual designer for managed c++ was having a problem with events in my controls. When...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
52
by: Sergey Zuyev | last post by:
Hello All I work at software company and we are having a really big discussion about coding styles and it seems that more people prefer statement 1 to statement2 , which was a big surprise to...
7
by: mark4asp | last post by:
How can I prevent Caching of JavaScript and CSS files ONLY when I deploy a new application? I only want to force a refresh the first time the client uses the new build. For instance, I'm told I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.