News for hackers. Stuff that matters.
Re:My personal C coding style as of late 2023 by jpcfl on Sunday October 08, 2023 @10:39PM (#37816392)
Parameters and functions

No const.

Please don't. `const` is incredibly valuable, not only to the reader, but to the compiler.

Take for example:

  int Foo_bar(Foo const* self);
Just looking at this signature, I know that calling `bar()` will not modify the state of the object. This is incredibly valuable information to the reader.

Furthermore, if I want to create a `Foo` constant, I can only call this function if it is `const`.

  static Foo const a_foo = FOO_INIT(&some_params);
  return Foo_bar(&a_foo); // Will not compile without 'const' in function
`const` is valuable to the compiler, since `a_foo` can be placed into ROM on some platforms like MCUs, saving precious RAM.
Re:My personal C coding style as of late 2023 by paulddraper on Sunday October 08, 2023 @10:42PM (#37816411)
Agreed; const is one of those features that is so good I wish a lot of other languages (e.g. java) had it.
Re:My personal C coding style as of late 2023 by epcoa on Sunday October 08, 2023 @11:22PM (#37816608)
const in C and C++ are an abomination. On a pointer they don’t tell the compiler to do shit, because they can’t.

That I can agree with TFA. However I agree with the GP that dismissing it entirely is a little misplaced. It serves as a hint/documentation and I think the article undersells the value of rodata (not the pointer use of const which is basically shit).

I mean I have seen at least a few SIGSEGV/aborts due to attempted writes to ro memory. Also like, one of the few modern justifications for C, embedded, const still has important link time meaning.

Re:My personal C coding style as of late 2023 by skovati on Monday October 09, 2023 @12:24AM (#37816917)
FWIW, Java does have the "final" keyword.
Re:My personal C coding style as of late 2023 by flakes on Monday October 09, 2023 @02:45AM (#37817636)
Final only protects the variable from being assigned a new reference (similar to a const pointer). It doesn’t protect any of the underlying data held by the object from being changed, unless the entire hierarchy has every field declared final as well. I still use final heavily in all of my Java code, but it doesnt convey the full intent I would like it to.
Re:My personal C coding style as of late 2023 by _old_dude_ on Monday October 09, 2023 @03:47AM (#37818029)
I remember James Gosling saying, a long time ago, that the whole class should be either mutable or not so you do not need to tag some methods with const.

The consequence is that you may define two classes, one non-mutable and one mutable like String/StringBuilder.

Re:My personal C coding style as of late 2023 by layer8 on Monday October 09, 2023 @04:40AM (#37818329)
It means you have to triplicate each mutable class, because besides the immutable variant you also need the common interface (e.g. CharSequence), in order to pass mutable instances to read-only functions.
Re:My personal C coding style as of late 2023 by paulddraper on Monday October 09, 2023 @05:16PM (#37825592)
No, there are two classes -- mutable and immutable -- that both implement the immutable interface.
Re:My personal C coding style as of late 2023 by layer8 on Monday October 09, 2023 @06:09PM (#37826097)
Yes, so three classes. I’m counting a Java interface as a class, because it is the same as a purely abstract class. In any case, three different named types.

As a side note, I would say the interface is unmodifiable, not immutable, because references of the interface type may refer to mutable instances that can mutate while you use it through the interface. Immutable = doesn’t change state, unmodifiable = you can’t change it’s state via that reference (but it might change it’s state due to other concurrent code holding a mutable reference). This nomenclature comes from the “unmodifiable” collection wrappers in Java, which don’t make the underlying object immutable.

Re:My personal C coding style as of late 2023 by taylorius on Monday October 09, 2023 @05:12AM (#37818498)
Java has many great qualities, but concision is not one of them.
Re:My personal C coding style as of late 2023 by david2ndaccount on Sunday October 08, 2023 @10:49PM (#37816446)
You only know there are no mutations if Foo itself does not contain any indirections. Additionally, the compiler generally cannot assume that Foo_bar does not modify Foo as it is legal to cast away const as long as it is not originally a variable declared as const (so in your static Foo example it would be UB to cast away const).

static + const is valuable, but const parameters are merely a convention, there is no actual enforcement around them and due to aliasing the compiler generally can’t assume the parameter doesn’t actually change anyway.

Re:My personal C coding style as of late 2023 by lelanthran on Monday October 09, 2023 @03:17AM (#37817861)
> Additionally, the compiler generally cannot assume that Foo_bar does not modify Foo as it is legal to cast away const

No, but it can warn you!

The type is meant to capture programmer intention, and if you use `const` the compiler can warn you that your intention does not match the intention of the existing code (like, the intention of the author who wrote Foo_Bar).

Re:My personal C coding style as of late 2023 by jpcfl on Monday October 09, 2023 @10:30AM (#37820813)
True. I should rephrase to say `const` strongly suggests that a function does not change the observable state of a variable.
Re:My personal C coding style as of late 2023 by glitchc on Sunday October 08, 2023 @10:56PM (#37816485)
I'm afraid you are mistaken. In particular for pointers, const does not guarantee that the memory at the location pointed to won't change. Const only guarantees that the address itself doesn't change.
Re:My personal C coding style as of late 2023 by kibibu on Sunday October 08, 2023 @11:08PM (#37816534)
Should perhaps be

    int Foo_bar(const Foo * self);
Re:My personal C coding style as of late 2023 by glitchc on Sunday October 08, 2023 @11:24PM (#37816615)
Even then, some other function can change the memory at the address of self while this one is executing, especially in concurrent systems. Additionally, any other pointer pointing to the same address can also modify self's memory. const in this case is really just "scout's honour".
Re:My personal C coding style as of late 2023 by jenadine on Monday October 09, 2023 @01:14AM (#37817101)
`const Foo*` and `Foo const*` are exactly the same and just a question of style (east-const vs. west-const)

Not to be confused with `Foo *const`

Re:My personal C coding style as of late 2023 by PennRobotics on Monday October 09, 2023 @03:39AM (#37817974)
For anyone thinking, "wtf const pointer order??" fall back on the spiral rule:

https://c-faq.com/decl/spiral.anderson.html

Re:My personal C coding style as of late 2023 by jpcfl on Monday October 09, 2023 @10:08AM (#37820603)
> const does not guarantee that the memory at the location pointed to won't change

I didn't say this. I said a `const` function tells the reader that the state of an object doesn't change.

Another reader correctly pointed out that there are ways to modify the state of a `const` parameters (indirection and const cast), but I would argue that such an API is poorly-designed.

To qualify my original comment, a reader only knows a function doesn't change an object's state if the API is well-designed.

Re:My personal C coding style as of late 2023 by __MatrixMan__ on Sunday October 08, 2023 @11:49PM (#37816760)
Sorry for going off topic, but something fun I've learned lately is that in Nim (which compiles to C) changing:

let x = foo()

... to ...

const x = foo()

...runs foo at compile time to get the value. I dunno I just thought it was neat.