Re: [dev] [general][discussion] constants: `#define` or `static const`

From: Mattias Andrée <maandree_AT_kth.se>
Date: Fri, 13 Oct 2017 18:07:11 +0200

On Thu, 12 Oct 2017 15:21:14 +0100
Matthew Parnell <matt_AT_parnmatt.co.uk> wrote:

> Afternoon suckless community.
>
> It is made clear in the suckless coding style guide when to use
> #define and enums; however, it doesn't mention general global
> constants.
>
> I would search through the mailing list to see if this has been asked
> before; but it seems that gmane fails to search.
>
> I'm writing a header file that will contain constants required.
> Should I use:
>
> #define FOO 123.456
>
> or
>
> static double const foo = 123.456;
>
> (or `static const double`, for those who prefer the inconsistent const
> style; doesn't matter to the question)
>
> There are pros and cons to both; pre-processor could go either way,
> "static const" has scope and type safety, etc.
>
> But specifically about the suckless style; I have seen a lot of
> `#define`s and a few `static const` in suckless code.
>
> What is more in line with the suckless style, and why?
>
> Cheers,
>

In theory I like `static double`, however I cannot think of any
time I needed a constant that didn't need to be a #define and
should have been hardcoded. Often you want the constant to be
configurable via CPP when compiling or provide the ability
check its existence with the CPP.

Something I don't quite understand is why people make unnamed
enums and add #defines for each constant (I do get why would
do this for a named constant, but I which C had a better mechanism
for this). Like this:

        enum
        {
          MM_NOTOK = -1,
        #define MM_NOTOK MM_NOTOK
          MM_OK = 0,
        #define MM_OK MM_OK
          MM_NOMSG = 1,
        #define MM_NOMSG MM_NOMSG
          MM_NOCON = 4
        #define MM_NOCON MM_NOCON
        };

Of course, perhaps they should just name their enums, it always
looks like the right thing to do.

Received on Fri Oct 13 2017 - 18:07:11 CEST

This archive was generated by hypermail 2.3.0 : Fri Oct 13 2017 - 18:12:20 CEST