tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: [PATCH] style(5): No struct typedefs



[riastradh@]

> I propose the attached change to KNF style(5) to advise against
> typedefs for structs or unions, or pointers to them.

Pointers to them, I agree.  I don't like typedefs for pointers to
structs, except possibly for a few special cases.  I think it should be
pellucid from the declaration whether you're dealing with a pointer.

But most - all, I think - of the benefits you cite are still available
when using typedefs for the structs themselves.  Indeed, different
files do not have to agree on whether to use typedefs, and external
references, such as your

> struct vnode;
> int frobnitz(struct vnode *);

can do exactly that, even if other code does "typedef struct vnode
VNODE;" and then uses VNODE (or vnode_t, or whatever name you prefer;
personally, I like all-caps).

> There isn't even any need to define `struct bus_dma_tag' anywhere;
> the pointer can be cast in sys/arch/x86/x86/bus_dma.c to `struct
> x86_bus_dma_tag', for instance (at some risk in type safety, but a
> much smaller risk than having void * as the public interface),

But at a risk in formal nonportability, unless the struct bus_dma_tag *
was obtained by casting _from_ a struct x86_bus_dma_tag * to begin with
(which in this case it probably would have been).  I'd have to look up
the details to tell whether it's possible for casting a pointer to a
completed struct type to a pointer to a never-completed struct type to
lose information, fall afoul of alignment requirements, or the like.

[uwe@]

> Typedefs make sense when the type is *really* opaque and can, behind
> the scenes, be an integer type, a pointer or a struct.

Agreed.

> [Ab]using typedefs to save 8 bytes of "struct " + "*" just adds
> cognitive load (and whatever logistical complications that you have
> enumerated in the elided part of the quote).

Personally, I find that *in*cluding the "struct" adds cognitive load.
Perhaps it's just a question of what I'm used to, but having a noise
word present - and the "struct" is close to that, from a conceptual
point of view - means more noise to ignore.  Especially when the type
is referred to multiple times; I haven't seen it often, but I have seen
statements that look as though half the alphanumerics are "struct" (I
doubt any actually make it to the point of half, since each "struct"
needs a tag to be useful, and at least a few other identifiers to make
a useful statement, but it sure feels like it occasionally).

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index