[SSSD] Coding standard question (was ELAPI Adding file... )

Simo Sorce ssorce at redhat.com
Fri Sep 4 19:09:51 UTC 2009


On Fri, 2009-09-04 at 12:38 -0400, Dmitri Pal wrote:
> Hm, I was testing you :-)
> I did not do the typedef exactly because the of the policy you
> mentioned.
> Simo mentioned during the coding style discussions that he would
> prefer
> defines to typedes since the debugger in such cases works correctly
> and shows the contents of the structure while the typedef
> obfuscates things.

Honestly, I don't remember saying something like that, certainly not
when it comes to structures or pointers.

In general we prefer to use the explicit structure declaration instead
of typdefing it. But we can accept typedef's if there's a very good
reason for them.

In this particular instance your use of a #define is semantically
identical to a typedef (and therefore you still need the very good
reason :-).

But a #define used instead of a typedef is in general just an error
waiting to happen because of it's unexpected behavior if you do it with
pointers.

Example:

1. typedef int * int_p;

if in a program you declare
int_p foo, bar;
then you get 2 pointers to integers as you would expect.

2. #define int_p int *
if in a program you declare
int_p foo, bar;
then you get a pointer to integer, and a simple integer. Certainly not
what you want/expect.

Use defines for static values.
And use typedefs only for basic types if there is a good reason.
Having a shorter name is in general not a good reason, if "struct
<struct name>" is too long then you probably just have a way too long
structure name, shorten that one please.

Simo.

-- 
Simo Sorce * Red Hat, Inc * New York




More information about the sssd-devel mailing list