On Thu, 12 Oct 2017 15:21:14 +0100
Matthew Parnell <m...@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.

Attachment: pgp_xc0nCjpgQ.pgp
Description: OpenPGP digital signature

Reply via email to