Live data from Hacker News

My personal C coding style as of late 2023

nullprogram.com

231–240 of 466 posts

Re: My personal C coding style as of late 2023

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

Funny enough, the take on const in the only thing I agree with in the whole post…

Re: My personal C coding style as of late 2023

#232

Earlier quoted context omitted.

I agree. A lot of languages have settled on those same names or something similar. We don’t live in a world with a single word size anymore so carrying bit length in the name is critical, and so is keeping identifier names short. His trade off is exactly the one I would make.

> carrying bit length in the name is critical I beg to disagree. In D: byte - 8 bits short - 16 bits int - 32 bits long - 64 bits absolutely nobody is confused about this.

Of course plenty of people are confused, the overhead of "short/long" just makes no sense, but yet another bad design from the past carefully preserved

Re: My personal C coding style as of late 2023

#233
I came back to C after a good long time in Go, and I found that my C style had picked up some of these same good ideas, which I attributed to Go. In particular I also swore off NUL terminated strings, and started using structure returns to send back multiple values.

Re: My personal C coding style as of late 2023

#234
post #200

Earlier quoted context omitted.

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

It's more work. Not only to put all the annotations correctly, but only because it causes some real headaches. It's easy (implicit) to transition from non-const to const 1 pointer level deep. But the other way around -- it's really awkward to "remove" a const. The strstr() signature is probably the shortest example / explanation why. To implement strstr(), you have to hack the const away to create the return value. A…

> Real const data doesn't exist

Ever seen a ROM?

And the C library‘s hacks around not being able to overload functions (which is the only reason for strstr et al‘s weird signature) wouldn‘t stop me from using const. It can be really useful both for documentation and for correctness. Think memcpy, not strstr.

Re: My personal C coding style as of late 2023

#235
post #200

Earlier quoted context omitted.

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

It's more work. Not only to put all the annotations correctly, but only because it causes some real headaches. It's easy (implicit) to transition from non-const to const 1 pointer level deep. But the other way around -- it's really awkward to "remove" a const. The strstr() signature is probably the shortest example / explanation why. To implement strstr(), you have to hack the const away to create the return value. A…

> The strstr() signature is probably the shortest example / explanation why. To implement strstr(), you have to hack the const away to create the return value.

It seems to me the "hacking" is exactly the side-effect that is wanted. It's like the requirement in Rust to do certain kinds of things in an `unsafe { }` block (or using the `unsafe` package in Go): not that you want the compiler to prevent you from doing things completely, but that you want the compiler to prevent you from doing things by accident.

> One man's const data is another man's mutable data.

Yes; and the point of `const` for function parameters is to make sure that data isn't mutated unexpectedly.

Re: My personal C coding style as of late 2023

#236
post #92

Earlier quoted context omitted.

> Yes, it lets you shoot yourself in the foot, but what language doesn't? Good lord.

Well ... Name a language is which you categorically cannot "shoot yourself in the foot"!

The "categorically" part is a useless qualification, you don't program in a binary world, the ease with which a footgun is possible in a language is very important and can't be reduced to isPossible

Re: My personal C coding style as of late 2023

#237

Earlier quoted context omitted.

I mean, being a bit glib here, but a lot of programming is dealing with someone else's type system. Moreover, for those of us who write C fairly often, the mnemonics here are familiar. Actually, as custom type systems go, this one is pretty elegant. Reminds me of Rust.

Speaking of type systems, I read glib as g-lib a few times and tried to understand how you were talking about the GNU lib in that sentence.

I did too. Humans use context to resolve ambiguities in language, and in this case the context was very much statistically favouring the library; if you're using it, glib is literally "someone else's type system".

Re: My personal C coding style as of late 2023

#238
post #216

> While I still prefer ALL_CAPS for constants, I’ve adopted lowercase for function-like macros because it’s nicer to read. "ALL_CAPS" in C was not for constants, but for preprocessor macros. It's shouting in all-caps, because it means "Look out! There's a cpp macro expansion here!" Related, please stop using "ALL_CAPS" for constants in other languages. Not only does shouting constants as the most prominent syntax in…

I do it mainly as a form of namespacing and aid in readability.

  do_thing(foo); // foo is variable

  do_thing(FOO); // FOO is constant (i.e this call should always do the same thing)

  foo = FOO; // I wanna name a variable the same as a constant

Re: My personal C coding style as of late 2023

#239

IMO, defining your own types is one step too far. Now everyone who is already familiar with C types has to learn your own quirky system to understand one program. I think it does probably make sense to be specific about the sizes though e.g. using uint32_t over just uint (and expecting to receive some architecture-dependent size you might not get with uint.) These types should be defined in the right header (I think…

The reality is that C `int` is 32 bits in size. Sure, that's not true for 16 bit targets. But are you really going to port a 5Mb program to 16 bits? It's not worth worrying about. Your code is highly unlikely to be portable to 16 bits anyway. The problem is with `long`, which is 32 bits on some machines and 64 bits on others. This is just madness. Fortunately, `long long` is always 64 bits, so it makes sense to just…

Isn't "int" 64-bit on some (rare) architectures?

Re: My personal C coding style as of late 2023

#240
post #216

> While I still prefer ALL_CAPS for constants, I’ve adopted lowercase for function-like macros because it’s nicer to read. "ALL_CAPS" in C was not for constants, but for preprocessor macros. It's shouting in all-caps, because it means "Look out! There's a cpp macro expansion here!" Related, please stop using "ALL_CAPS" for constants in other languages. Not only does shouting constants as the most prominent syntax in…

I do it mainly as a form of namespacing and aid in readability. do_thing(foo); // foo is variable do_thing(FOO); // FOO is constant (i.e this call should always do the same thing) foo = FOO; // I wanna name a variable the same as a constant

Same, but maybe alternatively I'd accept:

    do_thing(Constants.foo)
as equally clear...
Post reply on HN