1. 8

  2. 1

    Distilling a piece of logic into one SPOT (Single Point Of Truth) is important when that logic can change, and it’s important that all callers get the same behavior. On the other hand, some (usually trivial) code is in its final form and will never be modified. For example:

    #define STR_LEN(s) "" s, (sizeof s - 1)

    This allows me to do things like write(fd, STR_LEN("Hello world\n")); without calling strlen(). Defining the macro in the files where it’s used instead of in a header makes the code easier to follow for newcomers (who don’t have to go hunting for the definition) and faster to compile, and really doesn’t have any disadvantages.

    1. 0

      I mean, you say the meaning never changes, but perhaps including the NULL terminator is a better API, and people fight, then things get changed in a breaking way.

      1. 1

        If you want to append a NUL byte, you can easily do so with "\0" (or + 1, but the former is clearer).

        This is in my personal codebase, and I’m the only people with write privileges.

    2. 1


      A little copying is better than a little dependency.