-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zig fmt policy #1003
Comments
Quick summary of my thoughts. In general, I just like to format things so they are < 100 lines and don't clutter each line too much. complex if-else expressionsMulti-line is infinitely more readable. I'd only do a single line personally on a single if/else, otherwise I'd always go multi-line. Further, I'd go multi-line if longer than a rough line limit. long function definitionsSingle line is usually fine. If your function is exceedingly long that'd indicate to me a possible function api change instead on how arguments are passed. long function callsSort of similar reasoning as above. If you have exceedingly long lines that'd indicate that maybe you should use a constant before or otherwise. I think the following pattern is sometimes useful:
spaces around ..I like this pattern for complex expressions (more than one expression term on either lower or upper) otherwise condensed is best. switch cases: newlines or same line?Definitely single-line. There may be a slight argument for ranges and complex expressions to use separate lines, however. nested struct initializationFirst example is much better. Much easier to see the separate nesting and what items belong to which sub-struct. space after fn and space before return type in function typesI like the space. Mirrors the usual function definition style simply without a name so feels more consistent. compact, aligned array literalsCompact 100%. These aligned details are usually done for a reason and it makes it much easier to visually line up certain details (as in the example, which we can see that the indices describe some permutations). long expressionsCould argue that we should split this into sub-expressions. Not too fussed here. comment between then block and elseI sort of like this pattern, the alternative would be to insert comments within the block. This feels a little clunky on the first if and for me distracts from the importance. I don't think this commenting style should be used fairly often as the expressions themselves are usually obvious enough anyway. same line comment after open brace (obvious fix)Can disregard this I think and prefer the above style (or this one) if-else one linersI'd go one-line for any if/else with no else if and see how that works. |
I agree almost 100% with @tiehuis, except for the longer function definitions and calls; when you have a long one it does probably suggest you should change how you call it but sometimes it just comes down to long sequences of variable names like I personally also don't like the spaces around Maybe some of this should be optional? Like have 'settings' kinda like how go-fmt works. |
No settings. The point is conformity. Also go fmt does not have any settings. |
For some reason I felt it did, like the ability to define I guess my opinion is based on the fact that everyone has different style guides (even if slightly different) and that'll it'll be extremely hard to get agreement in an open source community for style. |
Now that recursion is allowed (see #1006), I refactored |
I think I know how to resolve most of these issues. Here's my idea for how to resolve lists of things where it is unclear whether to do same line or one-item-per-line: if there are > 1 item and a trailing comma in the input, then put each item on its own line. Otherwise try to put everything on 1 line (perhaps wrapping and indenting) with no trailing comma. I don't know how to resolve the array literal problem. Maybe we need something like |
After discussing in person with @thejoshwolfe I believe we have a solution for array literals:
pub const c_digits_lut = []u8 {
'0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', //
'0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3',
'1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0',
'2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7',
'2', '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
'9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7',
'9', '8', '9', '9',
}; The empty pub const c_digits_lut = []u8 {
'0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', //
'0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3', //
'1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0', //
'2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', //
'2', '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', //
'9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7', //
'9', '8', '9', '9',
}; If the first line of the array literal does not have a pub const c_digits_lut = []u8{
'0', '0', '0', '1', //
'0',
'2',
'0',
'3',
'0',
'4',
'0',
'5',
'0',
'6',
'0',
'7',
'0',
'8',
'0',
'9',
}; transforms into pub const c_digits_lut = []u8{
'0', '0', '0', '1', //
'0', '2', '0', '3', //
'0', '4', '0', '5', //
'0', '6', '0', '7', //
'0', '8', '0', '9', //
}; No alignment is done on elements as this would introduce a dependency on unicode and be inconsistent with other not-aligned things such as variable declarations and struct declarations. |
now the first row of an array literal is the hint to zig fmt for how long each row should be. See #1003
I realized that the So now the first row of an array literal sets the row size for the rest of the array. I believe this is all solved now. Here are some conclusions I arrived at:
|
I offer the suggestion that |
@gh-4 I'm not 100% convinced on the space after the function name. This is seems counter to how most other languages do it. If there is a way to format that is likely to be preferred or expected by the majority of users I think we should favour that. Even though we expect the formatting to be opinionated and automatic, we should strive to have a reasonable default that is at least acceptable to most. Small aside is that an anonymous function (or declaration) |
Here's how I resolved most of these issues: if-else expressions
long function definitions
long function calls
spaces around
|
Everything is solved and I ran
|
I'm using
zig fmt
to convert deref code from*ptr
toptr.*
syntax in thepointer-reform
branch, usingzig fmt
from thezig-fmt-pointer-reform
branch.Here are some things I noticed that zig fmt does that we should address. Some of them have clear ways to fix them, others are less clear. Feedback welcome! Everybody gets to have an opinion about the bike shed color :)
complex if-else expressions [fixed]
long function definitions [fixed]
long function calls [fixed]
failure to parse extra comma [fixed]
spaces around
..
switch cases: newlines or same line? [fixed]
nested struct initialization [fixed]
space after
fn
and space before return type in function typescompact, aligned array literals [fixed]
long expressions [fixed]
comment between then block and else [fixed]
hex float literals [fixed]
same line comment after open brace [fixed]
if-else one liners [fixed]
same line doc comments on variable declarations [fixed]
The text was updated successfully, but these errors were encountered: