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

2017-10-14 Thread sylvain . bertrand
Oh! I forgot to insist on the fact that I'm not against an _optional_ const
attribute, but with different semantics than the current const keyword.

-- 
Sylvain



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

2017-10-14 Thread sylvain . bertrand
On Fri, Oct 13, 2017 at 07:57:16PM -0400, Alex Pilon wrote:
> On Fri, Oct 13, 2017 at 10:29:41AM +, sylvain.bertr...@gmail.com wrote:
> > I would go #define and not direct "static const".
> >
> > Because I think "const" is part of the excess syntax of C and should be
> > optional (and treated as an optional variable attribute).
> 
> You really don't think we should enforce preventing people from doing stupid
> things? Might not matter *as much* without concurrency, and it's only
> compile-time rather than run-time meaningful for non-stack variables due to
> most processors' memory model.
> 
> But it's unfortunately still useful to prevent people from doing stupid 
> things.
> 
> I just wouldn't expect people to const everything that can or should be.

I expect coders to avoid the use of const keyword. I'm ready to take the risk
to remove this from the code, because its addition to the syntax does not
balance with the benefits of interface definition hardening, which directly
leads to intellectual masturbation which is more harmful than fixing what you
call "stupid things" over time.

Like "enum" or "typedef", all that should not be in C, as more than one loop
keyword. But this is off-topic.

-- 
Sylvain



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

2017-10-14 Thread Anselm Garbe
Hi Matthew,

On 12 October 2017 at 16:21, Matthew Parnell  wrote:
> I'm writing a header file that will contain constants required.
> Should I use:
>
> #define FOO 123.456

Depends on the kind of header file you are talking about. A CPP define
can be the right thing, if this header file is part of some API used
by many programs and you don't want to impose variable or constants
into those programs.

If the header is only included by your program (like config.h in
suckless tools) then using proper variable or constant declaration
might be the better approach.

But I wouldn't demonise the definition of constant values using CPP.
CPP starts becoming evil, if you incorporate excessive macro variable
expansion that makes it hard being tracked down in case of compile
errors. Simple #define's have never been a problem when analysing
compile errors.

-Anselm



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

2017-10-13 Thread Alex Pilon
On Fri, Oct 13, 2017 at 10:29:41AM +, sylvain.bertr...@gmail.com wrote:
> I would go #define and not direct "static const".
>
> Because I think "const" is part of the excess syntax of C and should be
> optional (and treated as an optional variable attribute).

You really don't think we should enforce preventing people from doing stupid
things? Might not matter *as much* without concurrency, and it's only
compile-time rather than run-time meaningful for non-stack variables due to
most processors' memory model.

But it's unfortunately still useful to prevent people from doing stupid things.

I just wouldn't expect people to const everything that can or should be.



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

2017-10-13 Thread Mattias Andrée
On Thu, 12 Oct 2017 15:21:14 +0100
Matthew Parnell  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.


pgp_xc0nCjpgQ.pgp
Description: OpenPGP digital signature


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

2017-10-13 Thread sylvain . bertrand
Hi,

I would go #define and not direct "static const".

Because I think "const" is part of the excess syntax of C and should be
optional (and treated as an optional variable attribute).

Then I would add simple macros than would, based on the compiler, enable the
attribute or not. It's a bit what's done with gcc attributes already. Even
though a compiler can ignore the const attribute to generate code.

The pb are devs which are hardcoding gcc attributes in their code to make an
hard dep on gcc (ported some gnu/linux code from _gcc only_ to at least tinycc,
I think kmod and/or libblkid).
But I digress from the topic.

-- 
Sylvain



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

2017-10-12 Thread Laslo Hunhold
On Thu, 12 Oct 2017 15:21:14 +0100
Matthew Parnell  wrote:

Hey Matthew,

> 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.

as a general rule of thumb I think it's valid to try to reduce the
usage of the CPP as much as possible. It's a powerful tool, but for
defines like this, something might get lost.
After all, if you compile your code with debug flags, you won't be able
to trace back the constant "FOO", it will just be a literal to the
debugger. Using it as a "static const" variable, the debugger will be
able to "see" it, providing more context.
Apart from that though, in my opinion, such defines are a misuse of the
CPP in most cases.

With best regards

Laslo Hunhold

-- 
Laslo Hunhold 



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

2017-10-12 Thread Kamil Cholewiński
Hi Matthew,

I'd go with "static const" unless you have a reason to do otherwise. As
you pointed out: type checking, scope, etc. but also: you can't take the
address of a #define and pass it around as a pointer.

Ultimately it boils down to style. Use whatever you think best
communicates the *intent*.

<3,K.



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

2017-10-12 Thread Matthew Parnell
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,

-- 
Matthew Parnell
m...@parnmatt.co.uk