Function parameters style

In std I’ve seen both:

pub fn foo(arg1: u8, arg2: usize) Result {

AND

pub fn foo(
   arg1: u8,
   arg2: usize,
) Result {

I did not see a preference mentioned in the guide and read on down to “zen”, where I do note “Focus on code rather than style”… nevertheless, any leaning here? In particular, WITHIN std. (I have not looked hard and deep at std code, but even if 90% was one way and only 10% the other, I wouldn’t necessarily know if there’s a tide shift underway.)

I like how “way one” corresponds to function calling (most often), and I like how “way two” corresponds to structs. I don’t love how indentation works out in “way two”, but would be happy to get used to it.

(Note: tagged this ‘language’ because I didn’t see a ‘style’ tag and wasn’t sure what might be more appropriate)

I don’t have a problem with either as long as the latter has the trailing comma and is formatted with zig fmt.

I usually only use the multi-line one if the signature has many parameters or if I want to add documentation comments to the parameters.

2 Likes

I prefer the first unless I feel the line is getting too long or I feel that it gives clarity. Either way, it is completely arbitrary, and I have no problem using both within the same project and file.

I don’t think either is “preferred”, and zig fmt supports both by the presence or lack thereof of a trailing comma.

Perfect. Answers that clearly manifest the “Focus on code rather than style” goal. :slight_smile:

I’ve come to prefer putting each thing on its own line for the sole reason that it makes diffs between versions more readable. It’s not an absolute, though, it’s a balance, brevity often wins.

2 Likes