r/cprogramming • u/side2k • Sep 10 '26
Formatting call arguments
By default, clang-format does this:
SDL_Window *window =
;
and with some tweaks it does this:
SDL_Window *window = ;
Second one has some practical advantages - it easier to read diff on arg change and easier to add comments:
SDL_Window *window = SDL_CreateWindow(
"SDL2 test",
SDL_WINDOWPOS_UNDEFINED, // just some comment explaining why
SDL_WINDOWPOS_UNDEFINED,
- 400,
+ 500,
SDL_WINDOW_RESIZABLE);
While the first one seems to have mainly historical reasons to keep around. Perhaps, there is something I don't see? Because it feels like the first way is still used the most. And its the default one for clang-format.
I'm genuinely curious.
4
Upvotes
4
u/markort147 Sep 10 '26
So am I part of the smallest niche?
3
u/non-existing-person Sep 10 '26
Curious about what are you?:p I myself prefer second way. First one is getting very messy very quickly if you have longish function and parameter names. It's quite often hard to keep within 80 lines of code.
But most often than not I do hybrid of these two, like
2
u/jason-reddit-public Sep 10 '26
Density vs readability. If you have good eyesight (so you can use small fonts) and a big monitor, readability is the way to go. If you have bad eyesight and a small screen (so big font), density might actually be the ergonomic choice as you can fit more code on screen and don't have to flip around as much.
There are lots of choices in clang-format (and even more in some other formatter whose name escapes me but I use for "javascript" but handles C and C++ as well).
I like my in decls to go with the type then space then varname though because of C's umm unique way of declaring things, putting the star with the varname is more in the spirit of things (I also like one decl a line instead of big decls so my choice isn't that bad).
1
u/SmokeMuch7356 Sep 10 '26
For people working in editors without a built-in formatter such as vim, the first way is simply faster to type - lining every argument up like that takes a little time.
And there are countless variations: some people prefer
or even
or some other variation.
For my part I tend to write the unformatted version, but sometimes I will do one argument per line if the call is especially hairy, or something I'm using for the very first time, or something like that.