Re:My personal C coding style as of late 2023
by avgcorrection on Monday October 09, 2023 @05:32AM
(#37818605)
Thanks for bringing this up. There is no reason for modern languages that have proper constants (not preprocessor macros which happen to sometimes be used for them) to use this tedious style.
Constants are so innocent and useful. Why indirectly discourage their use by making their usage an eye-bleed?
Re:My personal C coding style as of late 2023
by barbs on Monday October 09, 2023 @09:00AM
(#37820021)
What alternative would you suggest?
Re:My personal C coding style as of late 2023
by yakubin on Monday October 09, 2023 @09:36AM
(#37820333)
In C++ I do it Google style:
static constexpr int kConstantName = 42;
Re:My personal C coding style as of late 2023
by _moof on Monday October 09, 2023 @10:19AM
(#37820708)
This is the old Mac style too. Might just be my upbringing but I prefer it.
Re:My personal C coding style as of late 2023
by avgcorrection on Monday October 09, 2023 @10:44AM
(#37820954)
What's the point of the `k` prefix?
Re:My personal C coding style as of late 2023
by smithza on Monday October 09, 2023 @10:51AM
(#37821040)
k for konstant
Re:My personal C coding style as of late 2023
by avgcorrection on Monday October 09, 2023 @11:01AM
(#37821160)
Yes, that's what it stands for. What's the point of using any prefix at all?
Re:My personal C coding style as of late 2023
by wrs on Monday October 09, 2023 @01:42PM
(#37823078)
So you don’t have to wonder if it’s a variable. Old Mac style uses prefixes: kConstant, gGlobalVar, TType, mMemberVar. Remember this was when all coding was done in black and white in a plain text editor.
Re:My personal C coding style as of late 2023
by colejohnson66 on Monday October 09, 2023 @06:03PM
(#37826040)
Hungarian notation
Re:My personal C coding style as of late 2023
by Dylan16807 on Wednesday October 11, 2023 @02:39AM
(#37841641)
Hungarian notation is for being more specific than the type system allows.
When the type is already "constexpr int", that k isn't helping.
Re:My personal C coding style as of late 2023
by dkersten on Tuesday October 10, 2023 @07:26AM
(#37830774)
IMHO that’s much uglier than using caps.
Sometimes when I use C++, I will namespace them however:
namespace constants {
static constexpr int name = 42;
}
…
int x = constants::name;
The exact name of the namespace would depend on the use and could be something more descriptive.
Re:My personal C coding style as of late 2023
by avgcorrection on Monday October 09, 2023 @09:08AM
(#37820085)
Writing in a normal way in snake case or camel case or whatever the convention is.
Re:My personal C coding style as of late 2023
by xigoi on Tuesday October 10, 2023 @02:18PM
(#37835526)
Just use the same style as for variables. What's the point of the distinction? Maybe you'll want to change a constant into a variable later.
Re:My personal C coding style as of late 2023
by jraph on Monday October 09, 2023 @05:28AM
(#37818582)
Interesting take, but good luck with this fight against all caps (snake case) constants, at this point it's almost a consensus, a shared culture element, a deeply ingrained habit that the vast majority of developers have and recognize.
Changing this would be a huge undertake I'd be afraid of engaging, if I cared that much.
Re:My personal C coding style as of late 2023
by eviks on Monday October 09, 2023 @05:36AM
(#37818633)
Indeed, these are less readable and harder to type for no benefit
Re:My personal C coding style as of late 2023
by harpiee on Monday October 09, 2023 @06:09AM
(#37818785)
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
by swah on Monday October 09, 2023 @06:20AM
(#37818846)
Same, but maybe alternatively I'd accept:
do_thing(Constants.foo)
as equally clear...
Re:My personal C coding style as of late 2023
by sdk77 on Monday October 09, 2023 @06:34AM
(#37818917)
The case of what is a constant, and whether or not it even really is, is not always clear in C. As an embedded developer (almost always on bare metal), variables declared with the const modifier are usually (but not always, it depends on the linker script) placed in read only memory. For those kind of variables (read only ones, they're not really constants as in C++ constexpr) I don't use all caps. But for preprocessor macros, always. Even "#define MY_CONSTANT 10" is a macro, and not a constant or a variable. And it should be treated with caution, because it is dangerous (inexperienced programmers might change it to #define MY_CONST 2 * OTHER_CONSTANT, which opens up a can of worms).
Re:My personal C coding style as of late 2023
by rollcat on Monday October 09, 2023 @07:42AM
(#37819335)
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.
Re:My personal C coding style as of late 2023
by Sharlin on Monday October 09, 2023 @09:53AM
(#37820459)
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 constant `Foo` for any unit struct `Foo`).
Re:My personal C coding style as of late 2023
by afdbcreid on Monday October 09, 2023 @01:20PM
(#37822769)
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.
Re:My personal C coding style as of late 2023
by suskeyhose on Monday October 09, 2023 @09:55PM
(#37827750)
SHOUT_CASE is truly the inferior way to refer to SCREAMING_SNAKE_CASE
Re:My personal C coding style as of late 2023
by adamrezich on Monday October 09, 2023 @10:57AM
(#37821124)
nah, sorry, I'm going to keep using ALL_CAPS_CONSTANTS (and enum members, because they're also constants). I like having constants be visually distinct from the rest of my code.
Re:My personal C coding style as of late 2023
by P_I_Staker on Monday October 09, 2023 @12:10PM
(#37821864)
Yeah, I don't see the problem. I believe this is default for pylint, and I see no issue with convention.
"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 the code make no sense, but there are much better uses for shouting in a programming language.
(For an example of a good use of "ALL_CAPS": if your language ever acquires Scheme-like template-based hygienic macro transformers, "ALL_CAPS" (or "ALL-CAPS") is excellent for making template pattern variables stand out within the otherwise literal code blocks.)