On 23/05/18 12:09, bartc wrote:
> On 23/05/2018 07:53, David Brown wrote:
>> On 22/05/18 22:02, bartc wrote:
>
>>> If you take a working program and remove all the consts, then probably
>>> it will continue to work. So they are an embellishment.
>
>> For a language (or library, tool, whatever) to support writing correct
>> programs, there are two primary aims:
>>
>> 1. Make it as easy as possible to write correct code.
>
>> 2. Make it as hard as possible to write incorrect code.
>
> I can't see how it makes it easier. First, you still have to design,
> write, develop, and debug your application.
You can't see how /what/ makes /what/ easier? I said "const" makes it
harder to write incorrect code - not that it makes it easier to write
correct code. (I think it does to some extent, by making your
intentions clearer, but the primary point as I see it is that it helps
make some types of bugs into compiler errors.)
Do you understand that my two points above are different?
>
> That's enough work by itself. But now you're saying that the extra
> effort and headache of adding 'consts' throughout the code and getting
> it to still compile (because const-checking issues will propagate all
> over the program, even if no actual data structures are going to be
> written to), all that is supposed to make it easier?
No - read what I wrote.
And if you think "const" is something you go through and add to the code
later, you have /totally/ missed the point.
When you need to define an object (other than dynamically allocated
objects), ask yourself if its value is going to vary or if it is going
to keep the same value throughout its lifetime (program lifetime for
file-scope objects, block lifetime for local objects). If it is going
to keep the same value, define it as a "const". (And if it is
file-scope, you usually also want it to be static, as for most
file-scope objects and functions.)
When you have a pointer to something - either as a variable, or a
pointer - ask yourself if you have going to use that pointer to change
the object(s) pointed at. If not, make it a pointer-to-const.
It is /that/ simple.
Using "const" does not make it easier to write the code in the first
place. But it makes it a very much harder to write code that breaks
your rules here - you can't change the value of objects whose values
should not be changed. You have to go out of your way in order to write
code that even attempts to do so.
(Let me note that I think compilers should be harsher about catching
such attempts, with more warnings or errors by default instead of
requiring extra flags. But that is a weakness in the implementations
here, not a failing of the language.)
>
> Some of us are experienced enough that we don't let data structures be
> written to when they shouldn't be. (In any case many are too complex to
> protect merely by adding 'const'; and if you add too many 'consts', then
> you will be stuck trying to figure out how to modify them when they do
> need to be updated or created).
Some of us are experienced enough to know we are not flawless, and like
to use the help we get. Using "const" does not guarantee bug-free code,
but it is certainly a useful step towards it. Arrogance about not
needing such features is a step backwards.
>
>> The concept of "const" is /so/ important and influential in helping
>> write clear and correct code that some modern programming languages make
>> everything "const" by default - you need to explicitly mark data as
>> "variable" or "mutable".
>
> Yes, some languages even go so far as to get rid of variables altogether
> so that you left scratching your head as to how to achieve the most
> trivial operations.
By that, I take it you mean /you/ personally can't get your head around
functional programming languages? Functional programming languages
require thinking in a somewhat different way, and usually appeal more to
mathematically minded people. They make some kinds of tasks far, far
easier than imperative languages (like C, and like your languages) - but
some tasks are definitely harder.
>
> While other modernish languages (Python for one) take the opposite route
> where /everything/ can be modified [by rebinding names], even functions
> and modules.
They do indeed. There is space for a wide range of ideas in programming
- there is no single perfect language for all purposes.
>
>> So a C programmer should learn to appreciate "const" and use it widely,
>> from their first program. Some use it everywhere they can, even for
>> small local variables, while others think that is a bit verbose.
>> Certainly you should always use it for pointers whenever you can.
>
> C's const is a poor substitute for proper control over read-only data
> structures. It's too fine-grained, it's easy to get it wrong, it's
> possible to still have holes where data structures can be written to.
>
It is not perfect, by any means - but it is still very useful. You do,
of course, have to know how to use it.
> It sucks at emulating named constants.
It does a reasonable job in some cases, and fails in others. As I have
said many times, C++ const is very much better here than C const.
> It gives a false sense of
> security.
If you go out of your way to write bad code, C lets you. But you have
to make a bit of an effort. And it helps if you have proper development
tools and know how to use them. (And let me say again that I'd rather
see more compiler complaints about mixups with const, enabled by default
rather than requiring specific flags.)
> It can be heavily over-used (see some of Stefan Ram's posted
> code).
I'd rather not look at Stefan's code, if it's all the same to you.
People have different styles in their coding - I think Stefan's is
extraordinarily unclear, idiosyncratic and overly complicated. He can
write it the way he wants, of course, but don't take it as an example of
how to use "const".
>
> But most importantly it introduces so much clutter that readability can
> be affected to the extent that genuine bugs may be harder to spot.
Totally nonsense. "const" is short and to-the-point, and adds extra
information to the code.
>
> And, it has no place in beginners' code. Many might want to learn C
> /because/ it can be dangerous to use!
>
"const" is an absolute must for beginners. The same goes for a many
features of C that you are determined to hate, fear and avoid (like
"static", "extern", proper declarations, initialisations, small scopes,
pointers, arrays, standard types, multiple files, and probably many
other things).