Skip to content

Clean code rules

jose-antonio-torres edited this page Aug 29, 2022 · 5 revisions

Welcome to the clean code rules page of the Swan wiki! During the development of any code, the programmer not only wants to obtain the desired results once the project is finished but also a code which is readable and easily expandable. Therefore, in Swan we have defined a set of rules to create our own clean code in a fast and efficient way.

This page will expose you different rules that might be used as a checklist while working in your personal branch of the project. The clean practices will be divided mainly in two parts: morphology and relevance.

Morphology

The morphology deals with the appearance of the code from a superficial point of view - i.e how any word must be written -, regarding the construction of variables, functions or classes. The rules concerning morphology are depicted below:

  • ...

Relevance

The relevance deals with the internal significance of the code, how it is connected with its surrounding and the degree of simplicity. For instance, the cohesion between the functions and the class that they serve, the type of access used for every property and function or the need to avoid code repetition, between others. The rules concerning relevance are depicted below:

  • When defining an independent class, the first public function must be the constructor. This takes the following form: "function obj = ClassName(cParams)". Inside the constructor, a private init function is usually instantiated to assign values to some properties by using the external cParams structure.
  • Avoid the definition of static functions. It is better to define private functions where the object interacts with its own properties.
  • Define simple atomic functions. It is preferable to use a lot of small functions (simple tasks) rather than a single huge function.
  • Validate your tests before committing your new changes, always.
  • Do not create classes with more than 100 lines.
  • Make sure that functions have some cohesion with its class. You cannot create functions which computation is not inside the scope of your class (for instance, computing the stress inside a class called StiffnessMatrixComputer).
  • Compose classes with other classes when optimizing your code.

Custom sidebar example

Clone this wiki locally