473,378 Members | 1,346 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,378 software developers and data experts.

if (f() != FAIL) or if (FAIL != f())?

Hello,
I have a question concerning style related to conditional
decision:

#define FAIL -1
int f();

if (f() != FAIL) or better if ( FAIL != f())? Why?
Thank you!
Wenjie
Nov 13 '05 #1
3 2485
go****@yahoo.com (Wenjie) wrote:
#define FAIL -1
int f();

if (f() != FAIL) or better if ( FAIL != f())? Why?


The former, since it is the clearer by far.

There are people who advocate using the second, because it can prevent
an error caused by carelessness in completely different circumstances
which cannot possibly occur here. Ignore them; writing unclear code
usually produces more bugs than silly tricks like that solve.

Richard
Nov 13 '05 #2

"Wenjie" <go****@yahoo.com> wrote in message
news:d2*************************@posting.google.co m...
Hello,
I have a question concerning style related to conditional
decision:

#define FAIL -1
int f();

if (f() != FAIL) or better if ( FAIL != f())? Why?

The second style is used to avoid the common mistake of assigning to a
variable instead of comparing.

For example...
if(a == 5) could be wrongly written as if(a = 5), which would return true,
but instead of comparing a with 5, it would assign the value of 5 to a.

But, if you use the second style, using an if(5 = a) would give you an
error.

In your case, it doesnt matter.
--
-Ashish
--------------------------------------------------------------------------
Hi! I'm a shareware signature! Send $5 if you use me, send $10 for manual!
http://www.123ashish.com http://www.softwarefreaks.com
Nov 13 '05 #3
Wenjie wrote:

I have a question concerning style related to conditional
decision:

#define FAIL -1
int f(void);

if (f() != FAIL)

or better

if ( FAIL != f())


int i = f();
if (FAIL == i)

is better than

int i = f();
if (i == FAIL)

because a typical typographical error

if (i = FAIL)

will assign i the value of FAIL
and the conditional will evaluate to true
even if 0 == f().

Both of example should cause a diagnostic message
to be logged by the compiler if you replace == with =
because both f() and FAIL are right-hand sides.

Nov 13 '05 #4

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

Similar topics

27
by: Chess Saurus | last post by:
I'm getting a little bit tired of writing if (a = malloc(...) == NULL) { // error code } I mean, is it really possible that a malloc call could fail, except in the case of running out of...
1
by: xxxxyz | last post by:
Hi, I want to create a class with a constructor which can fail (for example if (..) fail;). When constructor fail I want the variable to point to null. Example: class PosInt{ int i; public...
1
by: Robbie Hatley | last post by:
Say I have an ifstream object, like so: #include <iostream> #include <fstream> int main(int, char* Sam) { std::ifstream Bob; Bob.open(Sam); std::string buffer; while (42)
5
by: marccruz | last post by:
Hi, I am writing a Windows Service in C#. I want to gracefully fail the "protected override void OnStop()" function. To do this, I first tried throwing an Exception in the function. However,...
18
by: Dilip | last post by:
This thing is driving me nuts. why would a simple output file stream like this: ofstream ofs; ofs.open("c:\temp\somefile.txt") set the fail bit? calling ofs.fail() right after the open...
31
by: banansol | last post by:
Hi, I just want to get this right. A call to realloc() will return NULL on error and the original memory is left untouched, both when requesting a larger or a smaller size that the original,...
34
by: niranjan.singh | last post by:
This is regarding to test an SDK memory stuff. In what situation malloc gets fail. any comment/reply pls.... regards
5
by: marshmallowww | last post by:
I have an Access 2000 mde application which uses ADO and pass through queries to communicate with SQL Server 7, 2000 or 2005. Some of my customers, especially those with SQL Server 2005, have had...
6
by: meLlamanJefe | last post by:
I am not sure what else to try in order to address this issue. #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.