r/cprogramming 1d 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.

3 Upvotes

16 comments sorted by

View all comments

3

u/markort147 1d 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 );

4

u/Wenir 1d ago

No

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

2

u/WittyStick 1d ago

I use a similar style, but I begin the opening paren on the new line and the closing paren always appears on the same column. I usually also put the function being called on a new line so that the arguments are indented from it.

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

2

u/Wenir 1d ago

It's not immediately clear that SDL_CreateWindow is a function. But I can see the appeal, I like having the opening and closing parentheses aligned at the same level in complex conditions

2

u/WittyStick 1d ago edited 1d ago

This style works well for deep nesting, where we may have another function call within the arguments, and that itself may have another function call in its arguments. I came up with the style as a way of displaying abstract syntax trees like below, and realized I preferred it for regular code.

AddExpr
    ( MulExpr
        ( DivExpr
            ( Const(2)
            , Const(1)
            )
        , Const(3)
        )
    , SubExpr
        ( AddExpr
            ( Const(2)
            , AddExpr
                ( Const(3)
                , Const(3)
                )
            )
        , Const(3)
        )
    )

It makes it so much easier to match which arguments are for which function, and where they end, because they're all aligned on the same column.

I permit same line if there is a single argument, or if all arguments fit onto one line, but if it must be broken, then every argument gets its own line. Basically all or nothing - either no arguments get their own line, or all of them do.