5 ms·
> 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 constant
by neilv 3y ago
> 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 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.)
- jraph 3y agoInteresting 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.
- avgcorrection 3y agoThanks 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?
- barbs 3y agoWhat alternative would you suggest?
- avgcorrection 3y agoWriting in a normal way in snake case or camel case or whatever the convention is.
- yakubin 3y agoIn C++ I do it Google style: static constexpr int kConstantName = 42;
- _moof 3y agoThis is the old Mac style too. Might just be my upbringing but I prefer it.
- avgcorrection 3y agoWhat's the point of the `k` prefix?
- smithza 3y agok for konstant
- avgcorrection 3y agoYes, that's what it stands for. What's the point of using any prefix at all?
- wrs 3y agoSo 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.
- colejohnson66 3y agoHungarian notation
- Dylan16807 3y agoHungarian notation is for being more specific than the type system allows. When the type is already "constexpr int", that k isn't helping.
- xigoi 3y agoJust 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.
- eviks 3y agoIndeed, these are less readable and harder to type for no benefit
- harpiee 3y agoI 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
- swah 3y agoSame, but maybe alternatively I'd accept: do_thing(Constants.foo) as equally clear...
- sdk77 3y agoThe 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).
- rollcat 3y agoIt'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.
- Sharlin 3y agoI 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`).
- afdbcreid 3y agoThere 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.
- suskeyhose 3y agoSHOUT_CASE is truly the inferior way to refer to SCREAMING_SNAKE_CASE
- adamrezich 3y agonah, 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.
- P_I_Staker 3y agoYeah, I don't see the problem. I believe this is default for pylint, and I see no issue with convention.