On Tuesday 11 November 2003 00:55, Duncan Sands wrote:
> On Monday 10 November 2003 15:39, Matthew Wilcox wrote:
> > On Mon, Nov 10, 2003 at 02:46:37PM +0100, Duncan Sands wrote:
> > > > > The first step would to create a tools that find statemenst like
> > > > > if ( (A=b)<0 ). I have played with a grep pipeline and found only
> > > > > a few lines of code. perhaps somebody can make a tool that will
> > > > > show automaticly such kind of code.
> > >
> > > What is wrong with "if ( (A=b)< 0)"? Did you find any incorrect uses
> > > of this kind of construct in the kernel? By the way, this is just a
> > > specific example of a more common situation: statements with side
> > > effects.
> >
> > It's doing two things in one statement. That's not conducive to readable
> > code. Look:
> >
> > A = b;
> > if (A < 0)
> >
> > can be read faster than
> >
> > if ((A = b) < 0)
>
> Readability is in the eye of the beholder. For example, often this kind of
> construction:
>
> if ( (A = result_of_some_call) < 0)
> barf;
>
> amounts to:
>
> try {
> A = result_of_some_call;
> } catch (badness: A < 0) { barf; }
>
> (sorry, I forget what the C++/java or
> whatever syntax is - I hope you get
> the idea).
I don't get the idea. The Linux kernel is written in C - not C++ or Java so:
if (((a = foo()) < 0)
barf;
and
a = foo()
if (a < 0)
barf;
amount to exactly the same thing. In fact in C++ and Java it is still the
same...
Where the side effect can be useful is in a while loop. For instance, a common
C idiom is :
while ((c = getchar()) != EOF)
This "parses" fine for my mind but the side effects in an if expression are
evil.
Steve.
-------------------------------------------------------
This SF.Net email sponsored by: ApacheCon 2003,
16-19 November in Las Vegas. Learn firsthand the latest
developments in Apache, PHP, Perl, XML, Java, MySQL,
WebDAV, and more! http://www.apachecon.com/
|