General
- These coding rules apply to the Path Workbench. Other modules or the base system may use different coding guidelines.
- These coding rules are essentially the same well known PEP 8 guidelines;
- New code added to the workbench should adhere to these guidelines.
- All files should have the license header at the beginning.
- Unix line endings should be used.
- Use 4 spaces for a single level of indentation. Do not use tabs.
- Remove trailing white spaces.
- Keep files at less than 1000 lines in size.
- Break a module into smaller modules, or into a package (a subdirectory with various modules), as necessary.
- A big function can be broken into smaller functions.
- A big class can be broken into smaller classes. Then one class can subclass the other ones in order to inherit their methods.
- Python code should follow PEP 8 and PEP 257.
- C++ code should follow PEP 7.
- Maximum line length should be 80 characters.
- Find ways to reduce nested blocks of code by using auxiliary variables and functions that encapsulate the operations inside these blocks.
- If you have more than 3 levels of indentation, this is a sign that the code should be refactored.
- Use double quotes for `"strings"`.
- User facing strings should be translated
- Imports should be at the beginning of the file, after the license header and module docstring and they should be grouped in three types in order:
- Standard library imports
- third party imports
- FreeCAD specific imports
- External imports should use lazy loading
- Import only one module per line.
- Do not use asterisk imports, `from module import *` as this makes it hard to validate imported functions and classes.
- Do not import modules inside a function or class.
- The import of modules that require the graphical interface such as `FreeCADGui` should be guarded by an `if FreeCAD.GuiUp:` test.
- In general, the code should be structured in such a way that console-only functions are separate from their graphical interface implementations (GuiCommands).
- `snake_case_names.py` for modules.
- `variable_names_without_capitals` for variables.
- `CamelCaseClass` for classes.
- `CONSTANTS_USE_CAPITALS` for constants.
- `functions_without_capitals()` for functions and class methods.
- Functions expected to return a value should indicate what is expected so `is_mesh_valid` is a good name, but `check_mesh` is not a good name.
- Class names, method names, and variables not part of the public interface should start with an underscore like `_MyInternalClass` or `_my_small_variable`.