473,714 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if (expression) check

What is the BIG difference between checking the "if(expression) " in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ?

style A:
....
....
int a = 1;
if(0==a)
{
/* Don't write my name */
}
....
....

style B:
....
....
int a = 1;
if(a==0)
{
/* Don't write my name */
}
....
....

Nov 15 '05 #1
39 2202
ja************@ yahoo.com wrote:
What is the BIG difference between checking the "if(expression) " in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ?


Personally, I prefer the "if (a == 0)" style - though I know some people
do like the "if (0 == a)" style because it reduces the chances of
assignment (rather than comparison) errors slipping through compilation.
I would explain that rationale to your peer reviewer.

Having said that, IMHO either is fine as long as you are consistent and
clear; without knowing your circumstances, it's hard to say whether your
reviewer has the right to disagree :-)

Steve
--
Stephen Hildrey
Mail: st***@uptime.or g.uk / Tel: +442071931337
Jabber: st***@jabber.ea rth.li / MSN: fo*@hotmail.co. uk
Nov 15 '05 #2


ja************@ yahoo.com wrote:
What is the BIG difference between checking the "if(expression) " in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ?

style A:
if(0==a)

style B:
if(a==0)


There is no "BIG" difference. People who like A say
it's protection against writing `=' instead of `=='. People
who like B say the mistake can be guarded against in ways
that are less ugly. I'm a B guy, myself, but don't consider
it a "BIG" deal.

As for defending yourself: Watch "Miss Congeniality"
and pay close attention when the protagonist demonstrates
what "SING" means.

--
Er*********@sun .com

Nov 15 '05 #3
ja************@ yahoo.com wrote:
What is the BIG difference between checking the "if(expression) " in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ?
<example snipped down to the essence:> style A:
....
if(0==a)

style B:
....
if(a==0)


The one "real advantage" of style A is that a forgotten second =
leads to a compile time error: 0=a does not compile, whereas a=0
does.
In the presence of compilers and tools like splint that can
warn you about stuff like this, this "advantage" is often
unnecessary. You can find any number of arguments about which
of the two is better in the archives of comp.lang.c. Most people
arguing against it are of the opinion that it is a "less natural"
way of writing the condition.

Personally, I like style B better but put up with A if coding
guidelines, conventions for a project, or similar prefer or
prescribe A.

In the absence of such conventions or standards, this is something
where everyone should be allowed to do as he or she likes --
everything else is mainly bullying of some sort in my eyes.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #4
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
<ja************ @yahoo.com> wrote:
What is the BIG difference between checking the "if(expression) " in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ? style B: if(a==0)


Style A, 0==a, helps reduce problems in which an = is accidently
omitted. If you accidently wrote if(0=a) then the compiler would
complain.

If, though, following Style B you accidently wrote if(a=0)
then the statement is legal but with the undesired side-effect
of setting a to 0. Some compilers will warn about this, but this
is not a requirement and should not be counted on.

The trade-off is that a lot of people find it "unnatural" to
compare a constant to a variable: people tend to think of comparing
a variable to a constant, as in Style B.

There are some variations that might be acceptable, but they require
extra work that might be overlooked: e.g., if((int)a==0)
In this case, if you were to accidently write if((int)a=0)
then the compiler would complain because (int)a is not a modifiable
lvalue. Another form that works: if((a&a)==0) which has the advantage
of not having to know what the type of a is. Or perhaps
if(-a==0) since values do not change their relationship to 0 by
being negated (unless perhaps for an unsigned int on a 1's complement
machine??)
--
Look out, there are llamas!
Nov 15 '05 #5
ja************@ yahoo.com wrote:
What is the BIG difference
between checking the "if(expression) " in A and B?
I'm used to with style A, "if (0 == a)",
but my peer reviewer likes style B.
How can I defend myself to stay with style A?
Tell your "peer reviewer" to kiss your ....
It's a matter of style.
You don't need to defend it.
style A:
....
....
const int a = 1;
if (0 == a) {
// Don't write my name.
}
....
....

style B:
....
....
const int a = 1;
if (a == 0) {
// Don't write my name.
}
....
....

Nov 15 '05 #6
<ja************ @yahoo.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
What is the BIG difference between checking the "if(expression) " in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ?

style A:
....
....
int a = 1;
if(0==a)
{
/* Don't write my name */
}
....
....

style B:
....
....
int a = 1;
if(a==0)
{
/* Don't write my name */
}


I personally prefer style B (not concerned with accidentally
making an assignment as I look at the code when I write it)
but the big defense is: hey, I'm an idiot and style A prevents
me from accidentally making an assignment and thus helps
prevent me from writing buggy code as I can count on the
compiler to catch all of my mistakes! :-)

If your reviewer is truly a peer... tell him that you don't give a
s**t about his personal preferences, style should not be an
issue in a code review. If that doesn't work, refer to the 'big
defense' in the previous paragraph and recite it verbatim.

Hope that helps,
Mark
Nov 15 '05 #7
Mark B wrote:

If your reviewer is truly a peer... tell him that you don't give a
s**t about his personal preferences, style should not be an
issue in a code review. If that doesn't work, refer to the 'big
defense' in the previous paragraph and recite it verbatim.

Around these parts, style is very much part of a peer review.
Programmers are required to write code in a consistent style because in
all likelyhood someone else will end up maintaining the code.


Brian
Nov 15 '05 #8
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
ja************@ yahoo.com wrote:
What is the BIG difference between checking the "if(expression) " in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ?

style A:
....
....
int a = 1;
if(0==a)
{
/* Don't write my name */
}
....
....

style B:
....
....
int a = 1;
if(a==0)
{
/* Don't write my name */
}
....
....


Try replacing the "==" in "0==a" and "a==0" with a single "=".

The first style must produce an error when you make that change.

The second style with this change is correct C, but will produce a
warning on any C implementation that I would be willing to use. If it
doesn't produce warnings, find out how to turn warnings on in the
compiler. If you can't make the compiler produce a warning, get a
different compiler. If you can't get a different compiler, use style A,
otherwise use style B.
Nov 15 '05 #9
ja************@ yahoo.com writes:
What is the BIG difference between checking the "if(expression) " in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ?

style A:
....
....
int a = 1;
if(0==a)
{
/* Don't write my name */
}
....
....

style B:
....
....
int a = 1;
if(a==0)
{
/* Don't write my name */
}
....
....


It is, as you say, a matter of style. That doesn't mean it's trivial,
it merely means that the compiler doesn't care which way you do it --
and the compiler is only one of the many entities that have to look at
your code.

My personal opinion is that the (0==a) form is jarring and ugly. If
it weren't for the possibility of typo-ing "=" rather than "==", there
would be absolutely no reason to use that form; I've never seen
comparisons written like that in languages that don't allow
assignments in expressions. I personally don't consider the risk of
an undetected typo so great that I'm willing to mangle my code like
that. If I really wanted to avoid confusing "=" and "==", I could
define macros ASSIGN and EQUAL_TO, and write a tool that would
complain about any direct use of the operators; the result would be
only slightly uglier than (0==a). (No, <iso646.h> doesn't provide
macros for either "=" or "==".)

But the above is, as I said, only my personal opinion (and a bit
overstated at that). I understand the argument in favor of (0==a),
and I understand that some people don't find it as jarring as I do.
Any competent C programmer should be able to read code using either
form, and any C compiler should generate equivalent (if not identical)
code for both.

Style is a legitimate subject for code reviews. If your work
environment imposes style guidelines that require one form or the
other, you should probably follow those guidelines. If it's a matter
of disagreement between you and a peer, then your opinion is as valid
as his (except, of course, that your peer is right because he agrees
with me).

Ultimately it's probably more of a political question than a language
question. You can expect a lot of opinions here, but I don't think
you can expect much help.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10

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

Similar topics

3
3711
by: Rex_chaos | last post by:
Hi there, I have a question about using expression template. We know that the final calculation in expression template will end up with a series of element-by-element operations. The concept can be explained with W = X o Y o Z; (here o denotes any operator) W.operator = LOR(LOR<X, o, Y>, o, Z); and in W.operator=, we have
7
1834
by: spiros | last post by:
Hi, suppose you have the class Class1 and the main program: class Class1 { public: Class1(); ~Class1();
2
7954
by: Christian Staffe | last post by:
Hi, I would like to check for a partial match between an input string and a regular expression using the Regex class in .NET. By partial match, I mean that the input string could not yet be complete but I want to know if a match is possible so far. For instance I want to design a text box to enter a date and validate the correctness of the date as the user types character. If the user enters 1953/12/23 it will match my regex of course...
6
489
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address with a "&" character in the local part. I know I should be able to work it out myself, but I'd like to ask anyone to suggest the best way to
7
25376
by: Chris Kennedy | last post by:
Does anyone know a regular expression that will validate the file extension but also allow multiple file extensions if necessary. It also needs to be case insensitive. Basically, what I want is to validate a file input box to check if the extension is the correct type, i.e. .doc for a Word Document etc. Also I would like to check multiple file types, for instance allow a gif or a jpeg or a jpg. Regards, Chris.
9
4152
by: HS1 | last post by:
Hello Could you please help for a simple boolean expression If a is not equal to Null I tried If (a != null) Then
5
1743
by: John | last post by:
I am new in Regular Expression. Could someone please help me in following expression? 1. the string cannot be empty 2. the string can only contains AlphaNumeric characters. No space or any special characters are allowed 3. space characters at the end of string is ok 4. the string cannot contains only numeric characters, in other word, the string must contains a least one alpha character Thanks for the help
9
7360
by: a | last post by:
I need to write a regular expression to match a quoted string in which the double quote character itself is represented by 2 double quotes. For example: "beginning ""nested quoted string"" end" Any idea how to write this in boost::xpressive or boost::regex. Thanks,
5
3489
by: John | last post by:
Hi I am using RegularExpressionValidator. What expression do I need to validate these two; Field1: A9 9AA A99 9AA A9A 9AA
15
4839
by: Matt | last post by:
Hi There, Can anyone explain me the real advantages of (other than syntax) lambda expressions over anonymous delegates? advantage for one over the other. delegate int F(int a); F fLambda = a =a++; F fAnonymous = delegate(int a) { return a++; }; fLambda(1);
0
8796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9170
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7946
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3155
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.