473,765 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recommended style

Hi all,

Which is the recommended style in production code for the following -

....
fp = fopen("xyz.txt" , "r");
if (fp == NULL) {
....
}

OR
fp = fopen("xyz.txt" , "r");
if (!fp) {
....
}

Thanks.

Nov 14 '05 #1
62 2601
how about

if((fp=fopen(fi lename,"mode")) ==NULL)
{
//failed
}

"Kelvin Moss" <km**********@y ahoo.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
Hi all,

Which is the recommended style in production code for the following -

...
fp = fopen("xyz.txt" , "r");
if (fp == NULL) {
...
}

OR
fp = fopen("xyz.txt" , "r");
if (!fp) {
...
}

Thanks.

Nov 14 '05 #2
Kelvin Moss wrote:
Hi all,

Which is the recommended style in production code for the following -

...
fp = fopen("xyz.txt" , "r");
if (fp == NULL) {
...
}

OR
fp = fopen("xyz.txt" , "r");
if (!fp) {
...
}

Thanks.

I think in a case like this, either one would be just fine; it's just
the case that the former is more explicit, while the latter employs a
well-known C idiom.

You might want to keep it consistent in a project -- but either choice
is sufficiently transparent. (I prefer the second, but that's pure opinion.)

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #3
Alex wrote:
how about

if((fp=fopen(fi lename,"mode")) ==NULL)
{
//failed
}


or, conversely:

if ( NULL == ( fp = fopen( filename, mode ) ) ) {
/* Failed */
}

Constants going on the left makes the C compiler bitch about =
instead of == which is a common programmer error that generally
takes a long time to find during runtime debugging ;}

Same with below:

fp = fopen( filename, mode );
if ( NULL == fp ) {
/* failed */
}

if ( !fp ) doesn't require constants on the left, since ! is a
unary logic operator.

-- The Dragon

Hi all,

Which is the recommended style in production code for the following -

...
fp = fopen("xyz.txt" , "r");
if (fp == NULL) {
...
}

OR
fp = fopen("xyz.txt" , "r");
if (!fp) {
...
}

Thanks.


Nov 14 '05 #4
Kelvin Moss wrote:
Hi all,

Which is the recommended style in production code for the following -

...
fp = fopen("xyz.txt" , "r");
if (fp == NULL) {
...
}

OR
fp = fopen("xyz.txt" , "r");
if (!fp) {
...
}


Anyone who tells you that either of these is "recommende d style" except
with reference to your local coding standards is blowing smoke.

For what it's worth, I prefer
if (!(fp = fopen("xyz.txt" ,"r"))) {
/* handle error */
}
Nov 14 '05 #5
Kelvin Moss wrote:
Hi all,

Which is the recommended style in production code for the following -

....
fp = fopen("xyz.txt" , "r");
if (fp == NULL) {
....
}

OR
fp = fopen("xyz.txt" , "r");
if (!fp) {
....
}


It depends who you ask.

If your employer/client/whatever doesn't impose a house style, decide
which you think is more logical, and use it.

Some people (including myself) argue that a pointer is not a boolean
variable and should not be treated like one. Other people argue
that !fp is just a shorthand for == 0, and so there's no harm in
using if(!fp), and if(fp == NULL) is only for people who love typing.

I hate typing, but I would rather type a few more characters if the
result is code which I find easier to maintain.

Personally, I use this:

fp = fopen(fn, fmode);
if(fp != NULL)
{
rc = foo(fp);
fclose(fp);
}
else
{
deal with the error
}

but there is no "right" answer to this question.
Nov 14 '05 #6
Kelvin Moss wrote on 30/12/04 :
Which is the recommended style in production code for the following -

...
fp = fopen("xyz.txt" , "r");
if (fp == NULL) {
...
}

OR
fp = fopen("xyz.txt" , "r");
if (!fp) {
...
}

Thanks.


The first one. No option!

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Nov 14 '05 #7
Alex wrote on 30/12/04 :
how about

if((fp=fopen(fi lename,"mode")) ==NULL)
{
//failed
}


Certainely not. Ugly, hard to read, hard to maintain.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Nov 14 '05 #8
infobahn wrote on 30/12/04 :
Personally, I use this:

fp = fopen(fn, fmode);
if(fp != NULL)
{
rc = foo(fp);
fclose(fp);
}
else
{
deal with the error
}


Agreed.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Nov 14 '05 #9

infobahn wrote:
Kelvin Moss wrote: Some people (including myself) argue that a pointer is not a boolean
variable and should not be treated like one. Other people argue
that !fp is just a shorthand for == 0, and so there's no harm in
using if(!fp), and if(fp == NULL) is only for people who love typing.
I too belong to the same school of thought. In fact in my whole code I
have used this syntax.

I hate typing, but I would rather type a few more characters if the
result is code which I find easier to maintain.

Personally, I use this:

fp = fopen(fn, fmode);
if(fp != NULL)
{
rc = foo(fp);
fclose(fp);
}
else
{
deal with the error
}

but there is no "right" answer to this question.

I too feel that doing this makes the code more readable.

Thanks.

Nov 14 '05 #10

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

Similar topics

0
1800
by: mikeg | last post by:
Hi, I'd like to explicitly link to the recommended php.ini file. For a long time, this link worked: http://cvs.php.net/co.php/php4/php.ini-recommended (In fact, if you google for "php ini recommended", that broken link comes up first).
6
2264
by: Huy | last post by:
Generic PHP newbie question here. May anyone please recommend books that have gotten you started on PHP (interfacing with MySQL is a plus) & also reference books? Just looking for some good quality books to purchase.
13
2393
by: Michele Simionato | last post by:
What is the recommended way of generating HTML from Python? I know of HTMLGen and of few recipes in the Cookbook, but is there something which is more or less standard? Also, are there plans to include a module for HTML generation in the standard library? I really would like to see some standardization in this area. Michele Simionato
7
4391
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a few features that you'd expect in any editor, except nearly none of them seem to have: 1 - Search and repalce with Regular Expressions. 2 - Search and Replace in an Xpath context. 3 - User specified tag-generation for either on a...
22
5147
by: Jonathan Snook | last post by:
I've been contemplating what the recommended usage of a "top of page" link should be? Should there only ever be one at the bottom of the page? Should they be sprinkled at various points on the page? Or should they be used at all? Lately, I've been leaning towards the last option because my thought is that most browsers have a method to make it back to the top of the page (home button, scroll bar, whatever). It seems I never use the...
11
1641
by: G Patel | last post by:
Can anways explain why many multi-file programs I've seen don't have a main.c file, instead main() is hidden in a file with a different name? Is this just a preference, or is there an unspoken rule (or advantage). I've been calling the file with main, main.c, and I was wondering if there are any caveats against this (that might prevent me from being laughed at or something).
3
1777
by: darren via AccessMonster.com | last post by:
Hi I'm based in the UK and I've drifted into Access from building a simple db for myself, to then being asked to build a simple db for someone else, to now spending time building increasingly more sophisticated (for me)databases. So far my learning curve has been based upon a handleful of books and this forum (which I think is fantastic and the level of help and knowledge sharing has astounded me). Aside from the very basic dummies...
2
15495
by: Mark Rae | last post by:
Hi, I've been asked to "update" an existing fairly simple website to make it XHTML-compliant i.e. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > ... </html>
13
2102
by: Michael M. | last post by:
In Perl, it was: ## Example: "Abc | def | ghi | jkl" ## -"Abc ghi jkl" ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st pipe). $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g; ## -- remove in text
0
9566
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
9393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8830
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...
0
6646
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
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3921
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
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.