Hi!
> Le 13 sept. 2018 à 13:13, Frank Heckenbach <[email protected]> a écrit :
>
> Akim Demaille wrote:
>
>>> I do admit I usually don't declare my local variables const though
>>> they often should be
>>
>> I recently had to work on some piece of software with coroutines,
>> and known what variables are immutable was really reassuring.
>
> I haven't done coroutines, but threads, and of course, everything
> that's exposed there, I make as const as possible. But that doesn't
> usually include local variables.
Local variables can be captured by lambdas and passed to coroutines.
In fact, I enjoyed so much ‘const’ on my variables, that I started
using a lot ‘IIFE’ (immediately invoked function expressions) to
be able to have this property more often. E.g.
const auto context = [&]
{
auto res = elle::serialization::Context{};
res.set<Doughnut*>(&this->doughnut());
res.set<elle::Version>(
elle_serialization_version(this->doughnut().version()));
return res;
}();
>> I now spread `const` for similar reason: help the reader.
>
> I think it's double-edged. OT1H, when the reader is trying to
> understand the code in detail, it certainly helps; OT2H on casual
> quick reading, too much verbosity can distract, especially for code
> that is not that complex.
I agree.
> So as I said, with a more concise syntax,
> it would be a no-brainer to me, but as things are, I often prefer
> shorter code. (For a similar reason, I also prefer "i" to
> "FoobarLoopCounter"; short names are quicker to parse mentally, and
> with small scopes there's no big danger of forgetting what the
> variables are, and of course, as a mathematician I'm used to short
> index variable names anyway. :)
:) Indeed :)
I agree short lived entities should have short names. That’s
actually one benefit (the only one?) to have *.h and *.c files:
*.h files can expose intelligent meaningful names, while *.c can
use short names.
>> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
>
> But they don't have type declarations. ;)
Sure! But that’s the kind of conciseness I would love.