Re: Checking return values for errors, a matter of style?
jacob navia wrote:
Unless the enthusiastic compiler optimised the variable
away (its not really needed in the above example, unless
it gets tested again).
goose,
jacob navia wrote:
Johan Tibell a écrit :
>
Using the second form allows you to easily see the return value in
the debugger.
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:
if (function(socke t, args) == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}
I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?
int succ;
succ = function(socket , args);
if (succ == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}
What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:
if (function(socke t, args) == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}
I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?
int succ;
succ = function(socket , args);
if (succ == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}
What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?
Using the second form allows you to easily see the return value in
the debugger.
away (its not really needed in the above example, unless
it gets tested again).
goose,
Comment