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
62 2607
A benefit of including spaces is if you don't, then when you do Ctrl-left or
Ctrl-right to skip words, you skip about half a line if you haven't left
spaces. Although I admit I can never be bothered.
"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:cr******** **@oravannahka. helsinki.fi... Lawrence Kirby <lk****@netacti ve.co.uk> scribbled the following: On Thu, 30 Dec 2004 03:14:26 -0400, The Dragon wrote: Alex wrote: how about
if((fp=fopen(fi lename,"mode")) ==NULL)
That's a clear, concise C idiom, but please locate your space bar.
I realise I'm treading on religious issues here, but I have a strong rule about spaces before the first opening paranthesis. "Strong" as in "written in stone". The rule consists of two parts: 1) If the statement starts with a keyword (such as "if", "for", "while" etc.) then include *ONE* space. 2) If the statement starts with a function or macro name then include *NO* spaces whatsoever. This rule applies to both C and Java.
I am amazed how many people either break one of the above parts, or even worse, switch them around.
-- /-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\ \-------------------------------------------------------- rules! --------/ "No, Maggie, not Aztec, Olmec! Ol-mec!" - Lisa Simpson
Bonj wrote: A benefit of including spaces is if you don't, then when you do Ctrl-left or Ctrl-right to skip words, you skip about half a line if you haven't left spaces. Although I admit I can never be bothered.
Just use w or b instead.
"Bonj" <a@b.com> writes: Neither: fp = fopen("xyz.txt" , "r"); if (fp == NULL) { ... } is nearly right, but that's java style. Since your question is titled 'Recommended style', the correct style is fp = fopen("xyz.txt" , "r"); if (fp == NULL) { ... }
is correct,
[...]
First, please don't top-post. Your response belongs after any quoted
text, not before it. See most of the articles in this newsgroup for
examples.
Your indentation is messed up in the second example, but I'll assume
that's a typo. If you're saying that
if (fp == NULL) {
...
}
is incorrect, but
if (fp == NULL)
{
...
}
is correct, I disagree. Both styles are popular (and both predate
Java). The former is used in K&R and in examples in the standard, and
is sometimes referred to as the One True Brace Style (and I personally
prefer it). If you prefer the latter, that's fine.
--
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.
On Thu, 30 Dec 2004 02:41:37 -0500, Martin Ambuhl <ma*****@earthl ink.net>
wrote: Kelvin Moss wrote: Hi all,
Which is the recommended style in production code for the following -
[...] Anyone who tells you that either of these is "recommende d style" except with reference to your local coding standards is blowing smoke.
That is correct. For what it's worth, I prefer if (!(fp = fopen("xyz.txt" ,"r"))) { /* handle error */ }
One problem with this code - some compilers (specifically some old ones)
are a bit overzealous and might suggest that you should use "==" insteaed
of "=". It's possible to disalbe the warning, but they either require a
bit more typing or prevent you from immediatly detect another problem
caused by confusing the operators. jj*@bcs.org.uk writes: Bonj wrote: A benefit of including spaces is if you don't, then when you do Ctrl-left or Ctrl-right to skip words, you skip about half a line if you haven't left spaces. Although I admit I can never be bothered. Just use w or b instead.
Which, of course, depends on which text editor you use, which is
off-topic.
--
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.
Raymond Martineau wrote: On Thu, 30 Dec 2004 02:41:37 -0500, Martin Ambuhl <ma*****@earthl ink.net> wrote:
Kelvin Moss wrote:
Hi all,
Which is the recommended style in production code for the following - [...]
Anyone who tells you that either of these is "recommende d style" except with reference to your local coding standards is blowing smoke.
That is correct.
For what it's worth, I prefer if (!(fp = fopen("xyz.txt" ,"r"))) { /* handle error */ }
One problem with this code - some compilers (specifically some old ones) are a bit overzealous and might suggest that you should use "==" insteaed of "=".
You might be right, but name one.
It's possible to disalbe the warning,
It should not be needed. The form of the statement is
if (!(p))
not
if (p)
When p is 'a = b',
if (a = b)
might trigger such a warning, but
if (!(a = b))
should never do so.
but they either require a bit more typing or prevent you from immediatly detect another problem caused by confusing the operators.
Bonj wrote: Neither: fp = fopen("xyz.txt" , "r"); if (fp == NULL) { ... } is nearly right, but that's java style.
Where do you get that idea from?
Since this brace style is preferred by the guy who invented C a
decade or two before Java was invented, and used in his seminal
book on the language, it's hard to see why it would be described
as "java style".
E. Robert Tisdale wrote on 30/12/04 : I might write:
FILE* fp = fopen("xyz.txt" , "r"); if (NULL != fp) { // read from xyz.txt } else { // (NULL == fp) fprintf(stderr, "Couldn't open file xyz.txt!\n"); }
I prefer to deal with the normal, usual or expected condition first and handle the error (exception) condition in the else part.
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++
On Thu, 30 Dec 2004 11:28:25 -0700, Alan Balmer
<al******@att.n et> wrote: On 30 Dec 2004 18:00:21 GMT, Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
I realise I'm treading on religious issues here, but I have a strong rule about spaces before the first opening paranthesis. "Strong" as in "written in stone". The rule consists of two parts: 1) If the statement starts with a keyword (such as "if", "for", "while" etc.) then include *ONE* space. 2) If the statement starts with a function or macro name then include *NO* spaces whatsoever. This rule applies to both C and Java.
I am amazed how many people either break one of the above parts, or even worse, switch them around.
Why would you be surprised that someone would violate *your* rule? I didn't even know about it until now. (And, to tell the truth, still don't feel any particular obligation to conform to it.)
Actually, his 'rules' are the ones I use as well, but I've seen so many
coding styles that I'm not at all surprised at code which is differently
laid out. I've had to conform to a number of them in different jobs,
and each time my own coding style (as in code I write for myself) has
either been re-validated or has altered slightly. Thank gnu for indent
and other 'beautifiers'.. .
Chris C
On Thu, 30 Dec 2004 23:32:16 -0000, Bonj
<a@b.com> wrote: A benefit of including spaces is if you don't, then when you do Ctrl-left or Ctrl-right to skip words,
.... nothing at al happens in the editor I use.
you skip about half a line if you haven't left spaces. Although I admit I can never be bothered.
On the other hand, if I use W and B to skip to words delimited by spaces
I get the effect you describe (if I use w or b then it stops at non
alphanumeric characters).
Saying anything about what keys do what in an editor is even less
portable than specific implementations of C, because there are no
standards at all...
(Oh, and top-posting doesn't help either...)
Chris C This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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).
|
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.
|
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
|
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...
|
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...
| |
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).
|
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...
|
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>
|
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
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |