CodingStyle indentation and alignment
Hi all,
I've read Documentation/CodingStyle and it states to use 8 character tabs.
Reading several USB driver files including drivers/usb/usb-skeleton.c, I see that multi-line lists of argument and operands are often aligned on top of each other using a mixture of tabs and spaces. checkpatch doesn't complain about the mixture.
For instance from usb-skeleton.c:
static int skel_probe(struct usb_interface *interface,
const struct usb_device_id *id)
uses two tabs and 6 spaces, not just tabs like:
static int skel_probe(struct usb_interface *interface,
const struct usb_device_id *id)
or
static int skel_probe(struct usb_interface *interface,
const struct usb_device_id *id)
Is a mixture of tabs and spaces acceptable if it enhances readability? If not, which of the tabs-only forms is correct?
Similarly, what about assignment alignment in structs?
Cheers,
Laurence.
Quoted 18 lines · Laurence Rochfort
Hi all,
I've read
Documentation/CodingStyleand it states to use 8 character tabs.Reading several USB driver files including
drivers/usb/usb-skeleton.c, I see that multi-line lists of argument and operands are often aligned on top of each other using a mixture of tabs and spaces. checkpatch doesn't complain about the mixture.For instance from
usb-skeleton.c:static int skel_probe(struct
usb_interface*interface,
const structusb_device_id*id)uses two tabs and 6 spaces, not just tabs like:
static int skel_probe(struct
usb_interface*interface,
const structusb_device_id*id)or
Quoted 7 lines
static int skel_probe(struct
usb_interface*interface,
const structusb_device_id*id)Is a mixture of tabs and spaces acceptable if it enhances readability? If not, which of the tabs-only forms is correct?
Similarly, what about assignment alignment in structs?
By my observations, conventional alignment in kernel contributions nowadays is N*tab followed by M*space, where M < 8. For details, consult "checkpatch --strict" output. It's not a paramount to make all existing codebase to pass that check cleanly, but if you are polishing your new patch, you can use that as guidance.
If you use Vim, see https://github.com/vivien/vim-linux-coding-style - it will indent your code more or less in accordance with coding style, also making checkpatch happy.
checkpatch in --strict mode actually prefers trailing lines to be aligned on opening brace. Tabs and spaces are allowed for such alignment (and you _need_ spaces to be able to align like that).
So what above vim plugin makes, and what is widely used in kernel codebase, is something like this ("|tab--->" or a smaller part represents tab character, "." represents space):
static int blah_blah_blah(struct usb_interface *interface, |tab--->|tab--->|tab--->..const struct usb_device_id *id) {
|tab--->if (1) {
|tab--->|tab--->int var = blah_blah(interface->foo_bar->baz_fuzz,
|tab--->|tab--->|tab--->|tab--->....11111111111111 + 111111111111111);Also
|tab--->|tab--->|tab--->|tab--->|tab--->|tab--->|tab---> (shown for reference) #define DRIVERNAME_REGISTER1_NAMEXXX--->|tab--->0xdead
#define DRIVERNAME_REGISTER2_NAME_WHICH_IS_LONG>0xdeaf
Which looks this way with actual tab characters:
#define DRIVERNAME_REGISTER1_NAMEXXX 0xdead
#define DRIVERNAME_REGISTER2_NAME_WHICH_IS_LONG 0xdeaf
This is supposed to be displayed ALWAYS with 8-char tab (this is carved in slate of CodingStyle), thus arguments about breaking viewing with different tab sizes are to be ignored.
To my taste this is not best possible approach, and I would like all readers of this thread to check this article:
http://dmitryfrank.com/articles/indent_with_tabs_align_with_spaces
But, unfortunately, checkpatch is explicitly not happy with that approach, giving notices of ERROR and WARNING levels for first example, however accepting #define case to be aligned with spaces IIRC.
Thanks very much for the comprehensive answer.
Using spaces and tabs for alignment does make the code more readable to my mind.
The reason behind my question originally was that the CodingStyle doc states:
"Outside of comments, documentation and except in Kconfig, spaces are never used for indentation, and the above example is deliberately broken."
however most code I have seen is not compliant with that.
Should the docs be updated?
Cheers,
Laurence.
Quoted 4 lines · Andrey Utkin
Quoted 3 lines · Laurence Rochfort
Hi all,
I've read
Documentation/CodingStyleand it states to use 8 character
tabs.
Quoted 2 lines
Quoted 2 lines
Reading several USB driver files including
drivers/usb/usb-skeleton.c,
I see that multi-line lists of argument and operands are often aligned on top of each other using a mixture of tabs and spaces. checkpatch doesn't complain about the mixture.
Quoted 11 lines
Quoted 11 lines
For instance from
usb-skeleton.c:static int skel_probe(struct
usb_interface*interface,
const structusb_device_id*id)uses two tabs and 6 spaces, not just tabs like:
static int skel_probe(struct
usb_interface*interface,
const structusb_device_id*id)
Quoted 7 lines
Quoted 7 lines
or
static int skel_probe(struct
usb_interface*interface,
const structusb_device_id*id)Is a mixture of tabs and spaces acceptable if it enhances readability?
If not, which of the tabs-only forms is correct?
Quoted 24 lines
Quoted 2 lines
Similarly, what about assignment alignment in structs?
By my observations, conventional alignment in kernel contributions nowadays is N*tab followed by M*space, where M < 8. For details, consult "checkpatch --strict" output. It's not a paramount to make all existing codebase to pass that check cleanly, but if you are polishing your new patch, you can use that as guidance.
If you use Vim, see https://github.com/vivien/vim-linux-coding-style - it will indent your code more or less in accordance with coding style, also making checkpatch happy.
checkpatch in --strict mode actually prefers trailing lines to be aligned on opening brace. Tabs and spaces are allowed for such alignment (and you _need_ spaces to be able to align like that).
So what above vim plugin makes, and what is widely used in kernel codebase, is something like this ("|tab--->" or a smaller part represents tab character, "." represents space):
static int
blah_blah_blah(struct usb_interface *interface, |tab--->|tab--->|tab--->..const struct usb_device_id *id){
Quoted 7 lines
|tab--->if (1) { |tab--->|tab--->int var = blah_blah(interface->foo_bar->baz_fuzz, |tab--->|tab--->|tab--->|tab--->....11111111111111 + 111111111111111);Also
|tab--->|tab--->|tab--->|tab--->|tab--->|tab--->|tab---> (shown for
reference)
Quoted 12 lines
#define DRIVERNAME_REGISTER1_NAMEXXX--->|tab--->0xdead #define DRIVERNAME_REGISTER2_NAME_WHICH_IS_LONG>0xdeaf
Which looks this way with actual tab characters:
#define
DRIVERNAME_REGISTER1_NAMEXXX0xdead
#defineDRIVERNAME_REGISTER2_NAME_WHICH_IS_LONG0xdeafThis is supposed to be displayed ALWAYS with 8-char tab (this is carved in slate of CodingStyle), thus arguments about breaking viewing with different tab sizes are to be ignored.
Quoted 6 lines
To my taste this is not best possible approach, and I would like all readers of this thread to check this article:
http://dmitryfrank.com/articles/indent_with_tabs_align_with_spaces
But, unfortunately, checkpatch is explicitly not happy with that approach, giving notices of ERROR and WARNING levels for first example, however accepting #define case to be aligned with spaces IIRC.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20160927/3982944c/attachment-0001.html
Quoted 8 lines · Laurence Rochfort
Thanks very much for the comprehensive answer.
Using spaces and tabs for alignment does make the code more readable to my mind.
The reason behind my question originally was that the CodingStyle doc states:
Quoted 6 lines
"Outside of comments, documentation and except in
Kconfig, spaces are never used for indentation, and the above example is deliberately broken."however most code I have seen is not compliant with that.
Should the docs be updated?
There's difference between terms "indentation" and "alignment". Please see paragraph "Indentation and alignment" of previously referenced article:
http://dmitryfrank.com/articles/indent_with_tabs_align_with_spaces#indentation_and_alignment
Accounting this difference, the doc is correct in strict sense.
Good point.
Quoted 17 lines · Andrey Utkin
Quoted 9 lines · Laurence Rochfort
Thanks very much for the comprehensive answer.
Using spaces and tabs for alignment does make the code more readable to my mind.
The reason behind my question originally was that the CodingStyle doc states:
"Outside of comments, documentation and except in
Kconfig, spaces arenever
Quoted 5 lines
used for indentation, and the above example is deliberately broken."
however most code I have seen is not compliant with that.
Should the docs be updated?
Quoted 8 lines
There's difference between terms "indentation" and "alignment". Please see paragraph "Indentation and alignment" of previously referenced article:
http://dmitryfrank.com/articles/indent_with_tabs_align_with_spaces#indentation_and_alignment
Accounting this difference, the doc is correct in strict sense.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20160927/85cc00c8/attachment.html
Hia ll!
Quoted 3 lines · Laurence Rochfort
?[...]
I've readDocumentation/CodingStyleand it states to use 8 character tabs.
That means, that a <tab> uses up to eight columns and not that one should replacre some 8 spaces somewhere with a <tab> or that spaces are not allowed.
Quoted 4 lines
Reading several USB driver files including
drivers/usb/usb-skeleton.c, I see that multi-line lists of argument and operands are often aligned on top of each other using a mixture of tabs and spaces. checkpatch doesn't complain about the mixture.
Why should that be a problem.
Quoted 7 lines
For instance from
usb-skeleton.c:static int skel_probe(struct
usb_interface*interface,?????????????????????????????? ??????????????????????const structusb_device_id*id)uses two tabs and 6 spaces, not just tabs like:
Quoted 7 lines
static int skel_probe(struct
usb_interface*interface,
??????? ???const structusb_device_id*id)or
static int skel_probe(struct
usb_interface*interface,
??????? ??? ?????const structusb_device_id*id)
Which of the three versions is most readable/consistent? IMHO the first (and it is the only consistent). Consider more than 2 paramaters which I align as in ---- ?snip ?----
static int foobarfunc(struct usb_interface *interface,
const struct usb_device_id *id,
int whatever,
const char *and_a_string)?
---- ?snip ?----
Would it be "better" as
---- ?snip ?----
static int foobarfunc(
struct usb_interface *interface,
const struct usb_device_id *id,
int
whatever,
const char *and_a_string )
----
?snip ?----
(which is also somewhat used somewhere)?
That wastes two
lines IMHO.
Same for
---- ?snip ?----
static int
foobarfunc(
struct usb_interface *interface,
const struct usb_device_id *id,
int
whatever,
const char *and_a_string )?
----
?snip ?----
which has the motivation "I can grep the function body with ^functionname" (but in the light of IDEs and ctags isn't that important anymore).
And remember that we are here for 99% in the "what one is used to" department.
Quoted 2 lines
Is a mixture of tabs and spaces acceptable if it enhances readability?
Only readability is really important - not some document (describing the rough direction to go an gives some simple direct hints for the undisputed details) or output of some simple script/tool which tries to detect violations[0].
Both of them are just guidelines and do not justify a single patch stupidly and blindly just "correcting" some checkpatch complaint.
Actually "checkpatch" needs some "do not warn in the next n lines" mechanism (or similar, e.g. indent and probably all source code formatters hava that too) so that known/intended violations for good reason are silenced.
Bernd
[0]: And "checkpatch" is a good tool but it is just a tool and one? ? ? ?needs to see the source to decide if it's better - not the simple? ? ? ?warning of eome reg-exp.
Signature · 2 lines
Bernd Petrovitsch Email : bernd at petrovitsch.priv.at
LUGA : http://www.luga.at