Coding Style

Code is read more often than it is written. This pages provides guidelines to improve the readability of code and make it consistent across the wide spectrum of LibreCube projects.

Python / MicroPython Coding Style

Please adhere to the coding style as defined in PEP8 (https://pep8.org).

A recall of the basic rules:

  • Indentation is four spaces, don't use tabs
  • Line length should be kept to 79 characters, if possible
  • Surround top-level function and class definitions with two blank lines.
  • Method definitions inside a class are surrounded by a single blank line.

The guideline on documenting your Python code is here: https://realpython.com/documenting-python-code/

Please also add type annotations at least for functions and methods.

Zed Editor Configuration

To ensure automatic code formatting for Python we use Ruff:

  • install ruff via package manager
  • go to settings (->"Open Settings" in the top toolbar) and add the following:
{
  ...
  "languages": {
    "Python": {
      "formatter": {
        "external": {
          "command": "ruff",
          "arguments": ["format", "-"]
        }
      }
    }
  }
}

Code OSS Configuration

Please add the following extensions to Code OSS IDE:

  • Python (ms-python): IntelliSense support for Python syntax.
  • Ruff: Formats and lints your code. Ensure that you enable the "format on save" option (Settings -> Format on Save).

Examples

Here are some code examples for reference:

import os
import sys


def long_function_name(
        var_one: int, var_two: int, var_three: float,
        var_four: str) -> None:
    print(var_one)


class FooBar:
    """This is the summary line

    This is the further elaboration of the docstring. Within this section,
    you can elaborate further on details as appropriate for the situation.
    Notice that the summary and the elaboration is separated by a blank new
    line.
    """

    def a_instance_method(self, var1: int) -> int:
        """This is a quick summary line used as a description of the object."""
        # write some code here
        return 0

    @classmethod
    def a_class_method(cls) -> None:
        # TODO: implement it
        pass


if (this_is_one_thing and
    that_is_another_thing):
    do_something()


my_list = [
    1, 2, 3,
    4, 5, 6,
    ]


result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
)


with open('/path/to/some/file/you/want/to/read') as file_1, \
     open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read())


income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

Rust Coding Style

When using Zed Editor as IDE, the coding style is automatically applied.

C / C++ Coding Style

When using Zed Editor as IDE, the coding style is automatically applied.