r/cprogramming • u/activeXdiamond • Apr 02 '26
Any reason to avoid mixing camelCase and snake_case in a single codebase or even variable name; if the mixing is done with documented and concise rules, not arbitarly?
I've been learning C at a proper/deeper level lately. One of the first issues I ran into is the lack of namespaces in C[1]. People seem to mainly solve this by just using prefixes. Examples:
utils_sign()
utils_clamp()
window_foo()
core_entities_stuff()
etc....Another example is 'vendor prefixes'. To minimize name collisions for your users when writing a library, you'd prefix everything with a short 2-3 letter name.
Example:
glCreateShader() //OpenGL
glewInit //GLEW
b2ClipSegmentToLine //Box2DC wrapper
//Box2d also does this in their Cpp code for typedef's since Cpp typedef's can't be namespaced (I think?)Now, I'm also trying to implement OOP in C; by having the method just be a function prefixed with the class name taking the instance as its first argument. Standard stuff.
Example:
Array_get()
Player_attack()
Player_getHealth()Notice the last one?
Basically I'd like to use underscores as a sort of 'logical-separator' in names, while using camelCase for 'readability-separator`.
For example in XmlParser the capital X tells you that its a class, but the capital P doesn't tell you anything, it's just there to make it easier to read multiple words strung together (since you can't say Xml parser, as you would in normal speech.)
I don't want the meaning of my pre/post fixes to be blurred by being both logical and readability separators. When I've used other languages their use for logical separation is minimal so its mostly alright (E.g. _foo in Lua for private variables; there are less than a handful of logical-separation cases, so the ambiguity isn't as bad.)
Here in C land, I will be using it a lot, so want clarity. Here's some of what I'm suggesting:
ClassName_methodName();
someStandaloneUtilFunc();
DynamicArray *array = DynamicArray_new();
DynamicArray_shrinkAndFill(array, 0, 12);
DynamicArray_doSomeFancyShtuff(array, COLOR_RED, GOOD_STUFF, 12);
DynamicArray_free(array);With the multi-word method and class names, I feel like this makes clear the distinction between what is the class, and what is the method.
Take the following instead. Isn't it much harder to reason about?
Dynamic_array_shrink_and_fill(array, 0, 12);
DynamicArray_shrink_and_fill(array, 0, 12);
DynamicArray_shrinkAndFill(array, 0, 12);I also occasionally have to add a post-fix for internal stuff, to help with macro magic, etc.. Example:
I'd name my function foo_base and have a macro called foo. The user would call foo as the macro pretends to be foo, it just does some syntactic sugar to the args.
Another example is foo_t for types.
Is there any reason not to do this? I feel like instinctively seeing a function name with both separators used feels like a beginner mistake, but also knowing the rationale behind the naming used makes it much easier to read. I'm definitely leaning towards using this, but want other people's opinion on it.
[1] C technically does have namespaces, but they are 4 built-in ones (one for structs/enums, one for goto labels, etc...); not ones that a program can create or modify. So not really relevant to the issue here.
14
u/imagineAnEpicUsrname Apr 02 '26
Sorry for not reading all that but its a matter of preference. If you ensure no collisions happen you're good. No skilled programmer would crash over name casing choice upon reading your code
4
u/sessamekesh Apr 02 '26
It's a stylistic choice, and occasionally helpful where there's added "kind" information beyond what type information can give.
E.g. - I'll occasionally introduce some kind of convention to mix the two, e.g. PascalCase for strictly POD struct members and snakeCase otherwise or something like that. There's no semantic meaning, but it helps raise code smells in code that should be... smelly.
That said, I'm apparently fairly allergic to
MixingPascal_and_snake_case. Reading that makes me angry and confused. Still subjective, still style preference, I don't have a functional argument to justify that reaction.4
u/ThigleBeagleMingle Apr 02 '26
2
u/RealJamBear Apr 04 '26
In my C personal projects decades ago I just usually stayed consistent with libraries I tended to use and how I used them. So I basically always used snake_case() for functions and if I did experiment with other styles like camelCase it was for variables and/or structs. It was just something I experimented with among other things to help remind me what something was at a glance and discover what felt natural to me. Consistency is what it boiled down to, if it's not consistent and meaningful then it's just messy.
This was obsoleted for me over time as I started using IDEs that offered ever growing features and identifying things became easy enough that I didn't feel the need to do anything special with naming and just kept things as consistent as I could across the board.
As I moved on to other languages I just did whatever was common. Professionally I just followed the code style guide and anything of mine I put up on a public repo I just stayed consistent with whatever I was used to.
Your style on your projects - do what makes sense to you and be consistent. If your code is public or you've got other devs looking at it, consider writing a basic style guide document to include with the project so others don't have to figure out what's going on with the different styles. And make sure anyone contributing to your stuff follows the same code style.
Just my 0.02, if it helps.
5
u/nbolton Apr 02 '26
It’s confusing for other developers when they see mixed or ambiguous code style. It creates extra decisions that amount to noise, slowing progress. Also it makes my eyes itch.
1
u/arkt8 Apr 04 '26
I just prefer to be pragmatic:
1
u/BigPeteB Apr 02 '26 edited Apr 09 '26
An excerpt from one of my professors (buried in a long but interesting essay about the history of T and Scheme) puts this more succinctly than I could:
That's it. It sounds easy, but it can be quite difficult to do this in practice. All of the best projects I've worked on do this well, and most of the worst projects I've worked on don't.
It doesn't matter what the naming scheme is, as long as it's clear and consistent. (Sometimes it makes sense to follow the naming scheme of other modules you're integrating with or even the language's standard libraries, but sometimes you're writing something that's fairly independent and you have more freedom to name things as you wish.) The real test is to ask multiple developers what they would name a new function, and see if they all come up with the same name.
I can imagine there might be some very large and complex projects where mixing snake_case and camelCase to create a sort of hierarchy within the names might make sense. Is it better than using only snake_case or only camelCase? I'm not so sure, but that's more of a judgment call. I'll say that in my personal experience working on some very large projects with a big hierarchy of modules and objects, names always felt pretty clear to me even though they only used snake_case or only used camelCase, but your experience (and your project) may be different.
(edit: tried to fix a missing close paren, and Reddit mobile fucked up and erased most of my comment, so I had to re-write it from memory)
2
u/kombiwombi Apr 02 '26
I would suggest that you are abusing C to look like some other language. The depth of specification within the function name is not a natural fit to the language.
The Player_getHealth() would in C be usually stripped back to player_health() and the inconsistency with player_set_health() regarded as normal.
Huge libraries have to take more care, but there are few of those.
2
u/RealisticDuck1957 Apr 03 '26
About 35 years ago I had a programming class where this was called abstract data types. Add codes to identify variation in parameters, as many of the gl* functions do, and you're close to what C++ does with name mangling behind the scenes.
I appreciate how the mix in the example isn't arbitrary, but follows a simple and clear rule. And improves readability. `
2
u/catbrane Apr 07 '26
I like the glib conventions:
Camel case and pascal case are hard to read, so you use them very little (just types). Macros (almost always) need to stand out, since there are a range of extra footguns to look for, so they are all caps. Snake case everywhere else, since it's so readable. The prefix is only for libraries, of course.
Typical class:
https://docs.gtk.org/glib/struct.HashTable.html
Your example might be:
1
u/rphii_ Apr 03 '26
I use snake_case for variables, functions, fields in structs, ... and This_Case_Thing for, structs + typedefs, ... and ALL_UPPER for enums, defines, macros...
prefixes are mainly what the project does, for every function or thing one might see from the outside. so an argument parser is arg, my string is so, my parallel worker is pw, my core is a mess....
....I mostly use my stuff, so theres not (yet) an issue with other projects. I'm also at a loss about what to do, because recently my Color type clashed with raylibs Color type XD
1
u/Traveling-Techie Apr 03 '26
I mix them on the same page. Sometimes it bothers me.
1
u/Certain-Flow-0 Apr 03 '26
Why not use double underscores to separate class from method? Player__get_health
0
u/B_bI_L Apr 02 '26
i think gl shaders use this kind of prefixes sometimes
on another note, i see people mainly recommend just switching to kind of C+ (you use C++, but you write C, just with namespaces)
0
u/zhivago Apr 02 '26
All that matters is meaningful consistency and contrained scope of meaning.
Then remember that the rest of the world exists and you need to deal with it.
Are you going to wrap it with your convention or are you going to weaken your convention by admixture?
And how much value does this convention produce after you pay for enforcement?
Does it break even?
0
u/[deleted] Apr 02 '26
[deleted] — view deleted comment