• log
  • whoami
  • uses
  • code
  • feeds
  • $ Macros, GOTOs and Skill Issues

    Macros, GOTOs and Skill Issues

    by dweller - 2024-11-15

    #c #programming #rant


    I often heard people repeat that C macros are bad and you shouldn’t use them. Same with gotos and I never really knew why. I mean, yes, I know it can be hard to debug macros or even read them sometimes. And gotos are evil because they are considered “harmful”. Even so, to me, they clearly have their use. gotos can be useful for cleanup:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    #include <stdio.h>
    
    #define fail(ret, label) do{rc=ret; goto label;}while(0)
    
    int func(void)
    {
        int rc = 0;
        FILE* a;
        FILE* b;
    
        a = fopen("a", "r");
        if(!a) fail(1, fail_a);
    
        b = fopen("b", "w");
        if(!b) fail(2, fail_b);
    
        /* lots of code here */
    
        fclose(b);
    fail_b:
        fclose(a);
    fail_a:
        return rc;
    }
    

    And macros can be used for all sort of things, like X macros, intrusive or otherwise generic data structures, generic “functions”, etc. Are they a perfect too? No of course not, I’d rather have defer and real meta-programming. BUT not only we don’t have that in C, just because they aren’t perfect doesn’t mean they can’t be useful at all and need total ban in the codebase. As long as you don’t see everything as a nail for your new goto/macro hammer.

    My point, SKILL ISSUE! No but really, people are afraid of what they don’t know. If you’re not skilled enough to differentiate good vs bad use of nuanced features, you’ll probably find a consensus from someone you trust enough (which is a problem if you’re new, because you don’t know who’s trustworthy since you have no experience to see who talks bullshit since… Well you got it.) and stick to their opinion. Which is in modern day “X IS CONSIDERED HARMFUL!” Which is true for X, formerly known as Twitter, but not true for many things in programming that you hear on “teh interwebz”.

    I realized it only now, when I was reading about m4. To me it was always this arcane thing that graybeards used and abused before switching to Perl or something. But it was because I didn’t know it. I mean I knew it’s a macro processor, but I didn’t knew how it worked. Now I know, now I see that it’s cool, and that it can be useful, and that you need to treat it as you’d treat any nuanced tool, with due diligence and research.

    So, TL;DR, skill issue, as always. Learn your tools, stop being afraid of things, “X is considered harmful” is considered harmful.

    P.S.

    Dammit, this was supposed to be a short and sweet #ulog entry, but as always I am a bad writer who cannot condense text down to the crux of the issue. In the “wise” words of whoever the fuck wrote Dragon Age: Veilguard - “Sorry. I ramble sometimes. I am a rambler.” (Don’t look it up, it’s… it’s dumb.)