Skip to content
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

Update our style guide #5500

Merged
merged 3 commits into from
Apr 18, 2019
Merged

Update our style guide #5500

merged 3 commits into from
Apr 18, 2019

Conversation

skullydazed
Copy link
Member

Description

Update our style guide to reflect a couple recent things that have been coming up.

  • Officially move to 4 space indent
  • Document our stance on #ifdef vs #if defined()

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Keyboard (addition or update)
  • Keymap/layout/userspace (addition or update)
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@skullydazed
Copy link
Member Author

Before we merge this I'd like to invite the community to weigh in. I know there are some 2 space fans out there but the majority of people seem to prefer 4 space indents, and a lot of our code is already indented that way.

@pelrun
Copy link
Contributor

pelrun commented Mar 28, 2019

4 spaces? noooooooooo! :)

Ah, whatever. I'm happy enough just letting clang-format deal with it.

@drashna
Copy link
Member

drashna commented Mar 28, 2019

I'm going to need a bigger screen⸮

Copy link
Member

@noroadsleft noroadsleft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I still prefer two spaces, but at least this isn't something infuriating like eight... or... dare I say it... three.

@alex-ong
Copy link
Contributor

4 space master race wow it's me vs 3 others so far

@vomindoraan
Copy link
Contributor

vomindoraan commented Mar 28, 2019

Could you summarize what the reasoning behind the switch from 2 to 4 is? I think most of the codebase is currently indented using 2 spaces, but I'll try to get some numbers to back that up.

@nooges
Copy link
Member

nooges commented Mar 28, 2019

I used to be a 2 spacer, but do 4 now, as it’s visually easier to distinguish, especially since I like to use a relatively narrow font.

@skullydazed Can we also get some guidance on indentation of #ifdefs/#ifs? I’ve ran into different styles for that lately.

@LostQuasar
Copy link
Contributor

4 spaces FTW for visual clarity and easy for beginners because this is also used by python

@pelrun
Copy link
Contributor

pelrun commented Mar 28, 2019

Don't reference other things as an authority, as there's a strong example for every possible sensible combination you're after (also, Python happily uses whatever indentation you want, as long as it's consistent in the block.)

It's more important that whatever is chosen minimises the amount of work required. Right now, most of the codebase uses 4 spaces, and it would have to be entirely reformatted to change that.

The other concern is being able to automate most of the reformatting. It's far easier to maintain a style when nobody has to do it manually. But clang-format isn't perfectly configurable, so either you fight it constantly, or you get it as close as possible and then accept that as the baseline.

@vomindoraan
Copy link
Contributor

vomindoraan commented Mar 28, 2019

A quick and dirty scan over the .c and .h files in the project produced this:

Indentation # of files
2 spaces 1596
3 spaces 179
4 spaces 1070
8 spaces 44
ambig. spaces 1203
tab 343
none 1010
total 5445

There's no clear winner, but 2 spaces is noticeably ahead (possibly because that's been the recommended size for a while).

I used this package for detecting indentation. Submodule files were ignored, and I did the scan on a fresh clone of the project. The method I used isn't perfect, and there are more files that could've been ignored (e.g. the third-party files in tmk_core/protocol/usb_hid), but it should give a fairly accurate idea of the current state of indentation in the project.

The “ambiguous spaces” category is mostly header files that have no indentation, but were detected as 1 space due to a leading space being used for alignment in block comments. 1144 out of the 1203 files are like this. The rest are oddball files that were detected as 12 spaces, 7 spaces etc.

With the objective part out of the way, I personally prefer 4 spaces for most projects. However, I also think that 2 spaces works well in QMK (keymaps are one example), and that a lot of files would need to be updated over time if the recommended indentation size were to change. So it might be wise to think this over a bit more.

Right now, most of the codebase uses 4 spaces, and it would have to be entirely reformatted to change that.

This is incorrect, as you can see in the table above. A lot of reformatting will need to be done either way if we are to eventually achieve consistency.

@XScorpion2
Copy link
Contributor

2 spaces has made my eyes bleed. I welcome the 4 space overlords.
Now if we could just get with this century on bracket styling as well, that would be great.

docs/contributing.md Outdated Show resolved Hide resolved
@skullydazed
Copy link
Member Author

Per @nooges' comment and after some discussion on discord I've updated the #if section a bit. There's still room to revise that if anyone has more comments.

* When deciding how (or if) to indent directives keep these points in mind:
* Readability is more important than consistency.
* Follow the file's existing style. If the file is mixed follow the style that makes sense for the section you are modifying.
* When choosing to indent you can follow the indention level of the surrounding C code, or preprocessor directives can have their own indent level. Choose the style that best communicates the intent of your code.
Copy link
Contributor

@vomindoraan vomindoraan Mar 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the suggestion from Discord:

Suggested change
* When choosing to indent you can follow the indention level of the surrounding C code, or preprocessor directives can have their own indent level. Choose the style that best communicates the intent of your code.
* When choosing to indent you can follow the indention level of the surrounding C code, or preprocessor directives can have their own indent level. Choose the style that best communicates the intent of your code.
* In either case, preprocessor indentation should not introduce additional levels of indentation to actual code. Otherwise, you can end up with code that's very hard to read (see [here](https://gist.github.com/vomindoraan/7934814ef7a6301f4ef1f462df3386df) for an example).

I welcome any suggestions to my suggestion 😄

@skullydazed skullydazed merged commit 068571b into master Apr 18, 2019
foosinn pushed a commit to foosinn/qmk_firmware that referenced this pull request May 6, 2019
* Update our style guide

* Clarify muiltple condition ifs

* update the ifdef section
@drashna drashna deleted the 4_space_indent branch May 21, 2019 22:55
noroadsleft added a commit to noroadsleft/kbf_qmk_converter that referenced this pull request May 24, 2019
- Fixes the vertically-offset key issue.
  - Layout macros and keymap.c layers actually look like the keyboards now. :)
- Code output now uses four-space indent
  - because qmk/qmk_firmware#5500

(I really should go through this and pull out the dead code. Feel like there's a lot.)
shimesaba-type0 pushed a commit to shimesaba-type0/qmk_firmware that referenced this pull request Jun 22, 2019
* Update our style guide

* Clarify muiltple condition ifs

* update the ifdef section
Timbus pushed a commit to Timbus/qmk_firmware that referenced this pull request Jun 23, 2019
* Update our style guide

* Clarify muiltple condition ifs

* update the ifdef section
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants