Re: Which kind of loop do I use, do or while?
- From: Amandil <mazwolfe@xxxxxxxxx>
- Date: Thu, 28 Feb 2008 08:27:31 -0800 (PST)
On Feb 28, 5:04 am, Steve O'Hara-Smith <ste...@xxxxxxxxxx> wrote:
On Wed, 27 Feb 2008 19:38:24 -0800 (PST)
Amandil <mazwo...@xxxxxxxxx> wrote:
Hi, I was wondering if someone could give me some advice. I'm using
C, so the constructs are those of that language, but the question is
not really language specific.
In my program, I want to process either the standard input (stdin) or
the list of files on the command line. I have two ways of doing this,
and I'm asking if there is any advantage to doing it one way or the
other.
Method 1: (using C pseudo-code)
FILE *fp = stdin;
char *filename = "standard input";
do {
if (there are arguments) {
assign filename to argument, shift args over (argv
++); open file, assign fp to FILE pointer;
Check for error while opening file
If there was an error, skip to next
iteration (continue); }
} /* Otherwise there are no files: Assemble stdin
(default) */ process(filename, fp, flags);
} while (argv[0] != NULL);
The alternative method would be not to enter the loop if there are no
files:
Method 2:
int error = 0;
FILE *fp;
char *filename;
if (there are no files) {
process("standard input", stdin, flags)
} else {
This makes the intention clearer and is therefore better IMHO. The
more clearly the code expresses the intention the less likely it is to
contain mistakes and the easier it is to read and therefore maintain.
Noting of course that sometimes clarity must be sacrificed for efficiency
but this should only be done when there is a clear need to do so.
I would be inclined even to exit after processing stdin - or
even return process("standard input", stdin, flags) - instead of using an
else but others would object on the grounds that there should only be one
exit (or return) - this is a religious matter :)
Thanks for your advice. I think I will use method 2. It was my
original choice, then I changed to method 1 because I was thinking in
a similar way as Neil, on the other hand the first iteration might be
different, therefore I asked the question. Regarding your suggestion
about dropping the else (and this answers Fred's point as well), this
code is not in main(), main() calls this function which does nothing
but process this loop, hence the declarations at the top. (C coders
will note that it could be C99, but I try to be compatible for C89 as
well.) Before posting I removed the error handling code because it was
C-specific and non-sequitur, but the function continues at the bottom
and I didn't want to return just yet.
-- Marty Amandil (Thanking all who responded)
.
- References:
- Which kind of loop do I use, do or while?
- From: Amandil
- Re: Which kind of loop do I use, do or while?
- From: Steve O'Hara-Smith
- Which kind of loop do I use, do or while?
- Prev by Date: Re: strlen(), K+1: clarification
- Next by Date: cvs and sourceforge
- Previous by thread: Re: Which kind of loop do I use, do or while?
- Next by thread: Re: Which kind of loop do I use, do or while?
- Index(es):
Relevant Pages
|