Re: [hackers] [quark] Use sizeof() instead of magic constants || FRIGN

From: FRIGN <dev_AT_frign.de>
Date: Mon, 5 Sep 2016 09:50:09 +0200

On Mon, 5 Sep 2016 09:42:47 +0200
Anselm R Garbe <garbeam_AT_gmail.com> wrote:

Hey Anselm,

> Why is typedef'ing structs bad practice?

because there's no reason for it other than syntax candy. It also hides
from the user what he is dealing with and when I want to lookup a
struct definition I often have to jungle-jump across multiple typedef
layers to finally reach the definition.

If I have a program for handling bank accounts and define an
account-struct like

        struct account {
                char *firstname;
                char *lastname;
                int balance;
        };

I think it's more descriptive in the code to declare a local variable
like

        struct account fisher;

rather than having a typdef

        #typedef struct account Account

which yields

        Account fisher;

It may look "nicer", "cleaner", "shorter", but it just hides
information from the user. Look at the Posix socket interfaces; there's
a reason why all the sock-address structs were left as-is and why a
messy project like X11 typedefs the shit out of its structs. The
ultimate reason is because X11 has become so complex that its authors
tried to at least make it visually half-appealing by "shortening" their
code with struct typedefs. If your code has become so complex and
overwhelming that plain "struct structname" constructs can't be fitted
in there any more you should fix it. That's my opinion.

Cheers

FRIGN

-- 
FRIGN <dev_AT_frign.de>
Received on Mon Sep 05 2016 - 09:50:09 CEST

This archive was generated by hypermail 2.3.0 : Mon Sep 05 2016 - 10:00:24 CEST