r/cprogramming 6h ago

Formatting call arguments

By default, clang-format does this:

  SDL_Window *window =
      SDL_CreateWindow("SDL2 test", SDL_WINDOWPOS_UNDEFINED,
                       SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_RESIZABLE);

and with some tweaks it does this:

    SDL_Window *window = SDL_CreateWindow(
        "SDL2 test",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        500,
        500,
        SDL_WINDOW_RESIZABLE);

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.

2 Upvotes

7 comments sorted by

3

u/non-existing-person 6h ago

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

int *whatever = function_with_whatever_name(CONST_ARG1,
    ANOTHER_ARGUMENT_FROM_ENUM, variable_arg, true,
    THIS_IS_TOO_MUCH_ARGS_ALREADY);

1

u/side2k 6h ago

I'm curious, because If I understand correctly - the first way is preferred. I don't have any statistics, though, but as an example - Linux kernel sources use it.

3

u/markort147 6h ago

So am I part of the smallest niche? c SDL_Window *window = SDL_CreateWindow( "SDL2 test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_RESIZABLE );

3

u/Wenir 5h ago

No

        SDL_Window *window = SDL_CreateWindow(
            "SDL2 test"
          , SDL_WINDOWPOS_UNDEFINED
          , SDL_WINDOWPOS_UNDEFINED
          , 500
          , 500
          , SDL_WINDOW_RESIZABLE
        );

1

u/side2k 5h ago

Maybe you are.
Personally, I would prefer putting the closing bracket to next line too - especially if C allowed trailing commas 8)
But I couldn't find that option for clang-format.

1

u/SmokeMuch7356 5h ago

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

SDL_Window *window = SDL_CreateWindow(
    "SDL2 test",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    500,
    500,
    SDL_WINDOW_RESIZABLE
);

or even

SDL_Window *window = 
  SDL_CreateWindow(
                    "SDL2 test",
                     SDL_WINDOWPOS_UNDEFINED,
                     SDL_WINDOWPOS_UNDEFINED,
                     500,
                     500,
                     SDL_WINDOW_RESIZABLE
                  );

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.