Re: A style question: repeated return value check

From: Ingo Molnar
Date: Tue Sep 30 2008 - 04:34:18 EST



* Pekka Paalanen <pq@xxxxxx> wrote:

> > kernel/trace/trace.c | 77 ++++++++++++++++++++++++++-----------------------
> > kernel/trace/trace.h | 10 ++++++-
> > 2 files changed, 50 insertions(+), 37 deletions(-)
> >
> > diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> > index 6ada059..61f33da 100644
> > --- a/kernel/trace/trace.c
> > +++ b/kernel/trace/trace.c
> [...]
> > @@ -1633,24 +1633,24 @@ static int print_trace_fmt(struct trace_iterator *iter)
> >
> > ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
> > if (!ret)
> > - return 0;
> > + return TRACE_TYPE_PARTIAL_LINE;
> > ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
> > if (!ret)
> > - return 0;
> > + return TRACE_TYPE_PARTIAL_LINE;
> > ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
> > if (!ret)
> > - return 0;
> > + return TRACE_TYPE_PARTIAL_LINE;
>
> Off-thread style question: Would it be better or worse to write the
> above as
>
> ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
> ret = ret && trace_seq_printf(s, "[%03d] ", iter->cpu);
> ret = ret && trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
> if (!ret)
> return TRACE_TYPE_PARTIAL_LINE;
>
> which would do exactly the same, but is more compact.
> Good or bad style?

in this particular case it's marginally worse style i think, even
considering that it makes the code more compact. The reason is that it
makes the code a tiny bit less obvious: the flow looks a bit unusual and
when skimming it i'd have to look once more to understand its purpose.
With the returns its more verbose but also plain obvious. YMMV.

Ingo
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/