Search
Johan Bezem

SE Radio 152: MISRA with Johan Bezem

Join the discussion
2 comments
  • “if you ever feel you need it” that’s about the right condition. Isn’t that the case for many features of C (and many other languages)?
    Consider a different example I just encountered a few days ago:
    On refactoring a parallel driver I wanted to replace a set of define-constants (typeless in C) with a set of constant variables. Since these variables were used in multiple files, I put external declarations in a header file. In my module, everything was fine, but in other modules the variables were also used as the dimension of certain arrays. And to do that (still using C90, not 99), the compiler needed to know the actual value of the constants in those modules too.
    The only other solution that worked for my current compiler was to use “static const” variables defined (not just declared!) in a header file. So basically every compilation unit thus go its own copy of the constant variables, which were then optimized-away by the compiler again.
    Now, if you know a little C you know this is ugly, if not criminal. You never define variables in a header file, just declare them. But “if you feel you need it”, I think it’s good to know your possibilities.

    FWIW,

    Johan

    PS: After a code review we reverted back to using defined constants (macros), but that’s a different issue. Both solutions are in violation of our coding guidelines, so there’s no ‘good’ way to solve this.

    Johan Bezem
    http://www.bezem.de/

  • The problem with weird code like the switch statement in this episode is that even though we are told it’s BAD, once you have seen the pattern it it’s going to be very hard to resist the temptation to use it if you ever feel you need it.

More from this show