2003-08-11 20:21:30

by Sottek, Matthew J

[permalink] [raw]
Subject: RE: [Dri-devel] Re: [PATCH] CodingStyle fixes for drm_agpsupport


> if (!pointer) {
> return (whatever);
> }
>
>
>because it's consistent, and guaranteed safe from stupid
>parsing errors that can waste days of debug time when
>someone decides to add to it.
>("its just a little change that cant hurt anything", ha ha)

I've always been an "Always use brackets" evangelist for two
reasons.
1) The time it takes to write the code fragment is noise
compared to the amount of cumulative time that everyone else
will spend reading it over it's lifespan. This is more true
in open source than it ever was in the closed source world.
Making the code explicit saves everyone time in the longrun.

2) There are some very real ways that bracketless code will
get broken. Either someone adds a line that didn't notice the
lack of brackets or, someone accidentally uses a multi-line
macro.

i.e.
if(foo)
DEBUG_PRINT("Foo!\n");

works great for 100 years until someone recodes the DEBUG_PRINT
macro to be 2 lines. The Linux kernel often has plain looking
functions or variables that end up being macros (and may only
expand to multi-line on some platforms) which could easily get
you into such a situation.




2003-08-12 07:11:16

by Ryan Anderson

[permalink] [raw]
Subject: Re: [Dri-devel] Re: [PATCH] CodingStyle fixes for drm_agpsupport

On Mon, Aug 11, 2003 at 01:21:22PM -0700, Sottek, Matthew J wrote:


> 2) There are some very real ways that bracketless code will
> get broken. Either someone adds a line that didn't notice the
> lack of brackets or, someone accidentally uses a multi-line
> macro.
>
> i.e.
> if(foo)
> DEBUG_PRINT("Foo!\n");
>
> works great for 100 years until someone recodes the DEBUG_PRINT
> macro to be 2 lines. The Linux kernel often has plain looking
> functions or variables that end up being macros (and may only
> expand to multi-line on some platforms) which could easily get
> you into such a situation.

#define DEBUG_PRINT(x) do { printk((x)); printk((x)); } while (0)

I believe your example is, oh, the #1 reason for this style of macro.

--

Ryan Anderson
sometimes Pug Majere

2003-08-12 12:05:16

by Firefly

[permalink] [raw]
Subject: RE: [Dri-devel] Re: [PATCH] CodingStyle fixes for drm_agpsupport

On Mon, 11 Aug 2003, Sottek, Matthew J wrote:

> if(foo)
> DEBUG_PRINT("Foo!\n");
>
> works great for 100 years until someone recodes the DEBUG_PRINT
> macro to be 2 lines.

Then those "someone" shouldn't be allowed near a macro system.

This is the standard trick:

#define DEBUG_PRINT(x) do { \
stmt1; \
stmt2; \
} while (0)

-Peter