Live data from Hacker News

My personal C coding style as of late 2023

nullprogram.com

361–370 of 466 posts

Re: My personal C coding style as of late 2023

#361
post #113

I wrote and still maintain an open source C project for 20+ years. Once a year I get a new guy coming in and telling me I am doing it wrong: you should typedef all data types, you should stop using const, and so on. It stopped being funny after the first couple times.

I'm getting an "I don't use const, and here's my view on it" vibe from the author much more than "you shouldn't use const". I'm really not getting any demand that you change your coding style, just someone reflecting on their work and explaining it to others. And... Whether I agree with their choices or not, I find that very cool and informative.

I will say that using const can make a huge mess of code, especially if you care about adhering to (very reasonable) guidelines.

There's a really good chance you will either have to promote or cast away the const, which I hate.

I tend to agree regarding not using const. It's been a while, so I don't have an example off the top of my head, but it's incredibly easy to break the const mechanism and have to deal with these annoying flaws.

I've just seen this go really bad with any kind of code that has a split responsibility between teams. Eventually you will have to pass to a non-const interface, that you aren't supposed to change.

So perhaps it makes sense if you have control from the top down and can ensure that the constness is maintained, or completely not, if it ends up non-const (then you could also try to move the interface to const, if it truly is)...

... I also suspect in many projects you'd just have to come to the conclusion that nothing can be const'd, because it ends up non-const anyway. Thus leading to the conclusion "just don't use const".

P.S. I'm a bad boy that didn't read TA yet. This is just based on my past experience where we didn't really have the authority to change stuff in the stack... often times there was eg an MCU interface at the end that was non-const... guess we could contact the silica manufacturer... sure they'll get right on that.

Re: My personal C coding style as of late 2023

#362

Earlier quoted context omitted.

Default signed/unsigned is not a platform convention but a compiler one. You can change it with a compiler switch, in the makefile.

Then you're better off using custom types - that way people will immediate know your type is non-default - as opposed to hiding your customization away in a makefile, pranking people who expect built-ins to behave a certain way.

The people who understand that it can be either, depending on a compiler switch, are exactly the people who use an explicit sign (typically via a typedef) to ensure their code always works.

The people who say that char is de facto signed and everyone should just deal with it, are the people who end up writing broken code.

Re: My personal C coding style as of late 2023

#363

Earlier quoted context omitted.

Also: Embedded is almost the only time you really, truly need to care about how many bits a type is, and only when you're interacting with actual hardware. For almost every other routine task in programming, I would argue that it really doesn't matter if your int is 32 bits wide or 64 bits wide. Why go through the trouble of insisting on int32_t or int64_t? It probably doesn't matter for the things you are counting.…

> 2x as many numbers Minor correction, 2^32x as many numbers. Though I agree with your point. Edit: added x to the number for consistency and clarity.

LOL yea, that's what was in my brain but somehow 2x got typed. Good catch.

Re: My personal C coding style as of late 2023

#364

Earlier quoted context omitted.

I'm getting an "I don't use const, and here's my view on it" vibe from the author much more than "you shouldn't use const". I'm really not getting any demand that you change your coding style, just someone reflecting on their work and explaining it to others. And... Whether I agree with their choices or not, I find that very cool and informative.

It wasn't clear to me if he's talking about const as a variable declaration qualifier - I never used it - or const in pointer types, which is very useful.

It's kinda both for me. If you have certainty from top to bottom that something will always be const, then maybe.

This is often not true, and even if you think so, you're often wrong. I can't recall all the consequences of the flaws in the system (promote/demote const), but it's not fun to deal with.

I've seen so many things wind up passed to a function or going through an interface eventually that's non-const (or lets not forget is "const'd for safety").

This is where some would say you should give up on practical grounds... if the mission is to determine which const scenarios can be ensured, you argue this is not practically possible and throw the whole thing out.

Re: My personal C coding style as of late 2023

#365
post #200
post #113

I wrote and still maintain an open source C project for 20+ years. Once a year I get a new guy coming in and telling me I am doing it wrong: you should typedef all data types, you should stop using const, and so on. It stopped being funny after the first couple times.

I'm not a C developer, so I have to ask: why in the world would you not use const?

You want to avoid promoting or casting away the const. If you start using const, you almost inevitably wind up with a mismatch.

This introduces flaws in the type system. I wish I had a better breakdown on the impact of these concerns, but I'd rather not worry at all.

Anyway, if you don't use const, this goes away. Bear in mind the minor amount of "safety" it provides, because you can just ignore it later, as you arguably tend to be doing anyway when you pass a const to non-const or visa versa.

Inevitably, outside of really small insular project (and often times even then), there's something down the line that winds up being non-const that you don't want to change.

C developers of this mindset tend to just come to the conclusion that you will immediately break the type system, just give up on the whole game.

Edit adding at least on example:

Example: You define as const and remove the const later. If anything writes to the non-const, this is undefined behavior

Example: I believe the above is actually true for const promotion if you modify the non-const version... I think this is only after the call (edit. ie after it become const, really interest in the answer).

No Undefined behavior

/* I imagine this would be okay */

si_non_const = si_non_const + GetMagicValue();

/* Const is promoted here */

const int fparam = si_non_const;

/* Writing to fparam is undefined past here */

f_const(&fparam);

Undefined behavior

/* I imagine writing, after using as const is also not defined, but is fine at this point */

si_non_const = si_non_const + GetMagicValue();

/* Here we now have a constant value that will never be written to */ const int fparam = si_non_const;

f_const(&fparam);

/* I think this would also be UB, even though it's accessed through a different symbol */

si_non_const = si_non_const + GetMagicValue();

Interested in other opinion, maybe will think on later... would it be valid for the compiler to remove that last assignment?

Edit: Sorry, this is unreadable, if you put a space between the not undefined, and undefined it's easier

Re: My personal C coding style as of late 2023

#366

Honest question: why C? I've been writing C++ firmware for SoCs, supposedly an area where C is supposed to be king, and C++ was just as good, except it came with all the batteries included. So, why C?

Because the can read someone else C code without needing to look up almost all the quirks they used, while I cannot read someone's C++ code without having Google handy.

Simplicity beats complexity almost every time.

Re: My personal C coding style as of late 2023

#367

Earlier quoted context omitted.

In my case : - I prefer functions over classes. - no mangling of exported names, the binary is re-usable as API. - in the long term, the C source code is more re-usable in other projects than C++ ones. and more like this.

> I prefer functions over classes. Then use functions? > no mangling of exported names, the binary is re-usable as API. Not once in my life have I seen someone use a binary as an API. > in the long term, the C source code is more re-usable in other projects than C++ ones. I don't even know what you mean by that.

> I don't even know what you mean by that.

He means that if you write your library in C it is callable from Python, Java, C++, Ruby, PHP, Python, Perl and more.

Re: My personal C coding style as of late 2023

#368

Earlier quoted context omitted.

> it came with all the batteries included Sometimes you do not want, or need, all the batteries.

Smart pointers alone make the switch worth it. Why wouldn't you want that?

Because it comes with everything else, notably it includes all the footguns or C and then adds orders of magnitudes more.

Unless you're a solo developer, this is not a win.

It's no accident that C++ is the only language in which its proponents have to self police their own teams to only use a subset of the language.

Re: My personal C coding style as of late 2023

#369
post #283

Earlier quoted context omitted.

No. It would be better if the standardization groups would have done that, but not when every developer has a different scheme

It’s not great but they’re just aliases so they’re interchangeable, which means you can keep everything consistent within a project and it won’t cause any problems when interacting with outside code

Until you include a header written by someone with the same opinion, and now you get compile errors because they both defined 'u8'.

I gotta be honest, all of those style suggestions look good until you try them in a non-solo and non-isolated project, and then you see what a mess you created.

We've all been there, as C programmers, and we've all done that in the past, which is why we don't do it anymore

Re: My personal C coding style as of late 2023

#370

Earlier quoted context omitted.

It's the reason Rust makes us use an exclamation mark with macro calls: beware! magic! here! I like this as a convention, but not necessarily as a grammar rule. Printing values is common enough that it shouldn't require shouting for constant attention, simple code shouldn't trigger sensory overload.

I do think that the default convention of SHOUT_CASE for constants in Rust is too in-your-face given that there's nothing about Rust constants that would particularly require them to stand out. I might have gone with CamelCase given that some things in Rust already straddle the "types are CamelCase, terms are snake_case" delineation (enum variants, even data-less ones, are CamelCase, as well as the implicitly defined…

There is some gotcha: they're copied on use, which means you could end up with more than one copy in your binary (unlikely with an optimizing compiler), or, worse, that if you have interior mutability in them it just won't work.
Post reply on HN