owntech-foundation / Core

A comprehensive API for power electronics based on Zephyr RTOS
https://www.owntech.org/
GNU Lesser General Public License v2.1
3 stars 9 forks source link

[RFC] Defining a code standard. #54

Open jalinei opened 3 months ago

jalinei commented 3 months ago

Is your feature request related to a problem? Please describe. Currently each modules has been written by different authors and code style is not uniform. I propose that we tend to follow Zephyr coding style as our project relies on that RTOS.

I'm creating this RFC as this applying this change requires tree-wide modifications.

Describe the enhancement you'd like Adding a clear code standard defined in a file named CONTRIBUTING.md. I propose we add a clang-format file to be able to format our code using included extensions in vscode.

To enforce these guidelines I propose to add checkpatch tool in CI for future PR using : https://github.com/webispy/checkpatch-action Below is a draft of that proposed CONTRIBUTING file :

Code format

Code format should follow https://kernel.org/doc/html/latest/process/coding-style.html

With the following exceptions:

Checking code conformity

checkpatch does not currently run on Windows.

Code conformity can be checked automatically using checkpatch. Checkpatch is used during CI to check code style after a PR.

To check code conformity :

Β Code formatting

A tool called Clang-format can be used to format the code.

clang-format automatically load coding rules contained in .clang-format at the project root.

jalinei commented 3 months ago

A preliminary draft adding required files for checkpatch CI and config. As well as the proposed skeleton of CONTRIBUTING.md https://github.com/jalinei/Core/tree/contributing_guidelines

guigur commented 3 months ago
* The line length is 100 columns or fewer. In the documentation, longer lines for URL references are an allowed exception.

I'm okay with that exception πŸ‘

* Add braces to every if, else, do, while, for and switch body, even for single-line code blocks. Use the --ignore BRACES flag to make checkpatch stop complaining.

yes πŸ‘

* Use spaces instead of tabs to align comments after declarations, as needed.

I disagree I think tabs are better for aligning comments πŸ‘Ž

* Use C89-style single line comments, /*  */. The C99-style single line comment, //, is not allowed.

I don't thing we need to ban // comments but discourage their utilization

* Use /**  */ for doxygen comments that need to appear in the documentation.

πŸ‘

* Avoid using binary literals (constants starting with 0b).

Why? Sometimes it is clearer than hexadecimal

* Avoid using non-ASCII symbols in code, unless it significantly improves clarity, avoid emojis in any case.

πŸ‘

Ayoub-Farah commented 3 months ago

Use C89-style single line comments, / /. The C99-style single line comment, //, is not allowed.

// are useful in certain case to give a short comment on the same line, they should be kept.

jalinei commented 3 months ago

I suggest that we adapt the severity of these rules as they might be annoying but that we stick to Zephyr RTOS coding style inheritance. As such we'd get warnings if not using these styles but we still try to adapt to it.

Otherwise I suggest that you propose a different strategy than rules inheritance. I don't mind choosing another set of rules, just tell me which, and why this choice would be relevant.

cfoucher-laas commented 2 months ago

As one of the main contributors to the Core Zephyr modules, here is a bit of historical context: At the time we started this project, I made the choice to separate between end-user-oriented code, that followed a CamelCase style, and the buried code, not for use by anyone outside the OwnTech team and the very advanced users, that followed a snake_case style. However, thus may be something we should re-consider if we want to harmonize code (or maybe not).

On the points you mention: