Coding standards for odoo modules

Importance of Coding Standards in Collaborative Software Development

Why coding standard is necessary?

In today's fast-paced business landscape, organizations are constantly seeking ways to optimize their operations, increase efficiency and derive growth. Odoo succeeded to fulfil all of these requirements. As more organizations recognize the value of Odoo in streamlining their business processes, the demand for skilled Odoo developers is on the rise. As the business grows, more developers engage in the project, and eventually developers are forced to do the teamwork. In the realm of software development, coding standards play a critical role in ensuring that a team can work together seamlessly, produce high-quality code, and achieve optimal efficiency. When multiple developers collaborate on a project, adhering to a set of coding standards becomes even more essential.

Benefits to follow coding standard

    Readability and Maintainability:

    Coding standards promote code readability by enforcing consistent formatting, naming conventions, and documentation practices. When team members can easily understand each other's code, it becomes simpler to review, modify and maintain the codebase. Clean and readable code reduces the learning curve for new team members joining the project, facilitating their integration and productivity.

    Consistency and Uniformity:

    By establishing coding standards, teams ensure that all members follow a unified approach to writing code. Consistency in coding style and structure eliminates confusion and reduces the likelihood of introducing errors. Code that follows to a common set of standards is easier to navigate, troubleshoot, and debug, leading to more efficient development cycles.

    Code Quality and Bug Prevention:

    Coding standards encourage best practices and discourage common pitfalls, By following guidelines such as proper error handling, standardized naming conventions, and code optimization, teams can improve the overall quality of their codebase.

    Any fool can write code that a computer can understand. Good developers write code the humans can understand.   ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​-Martin Fowler


Coding Standards to follow:

1) Module Structure:

  • Each module should have a clear and descriptive name, this helps other developers to understand the functionality contained by module just by looking it's name.
  • Organize module files and directories according to the Odoo module structure guidelines. Following a common approach helps others to navigate through directories and saves a lot of time. So use a consistent directory structure for models, views, security rules, data files, and static assets.
  • You can create a new module with all the directories with the scaffold command.
  • Go to the path where your odoo directory is located and enter the following command:  ./odoo/odoo-bin scaffold my_module​

2) Python Coding Standards:

Here are some general tips that you can use while developing a code in Python.

  • Use four spaces for indentation instead of tabs and a maximum line length of 80-85 for nice readability.
  • Use descriptive and meaningful names for variables, functions, and classes.
  • Use comments to explain the code's purpose, algorithms, and complex logic. Document your code using docstrings to provide clear explanations of the parameters and return values of functions and methods.
  • Keep one object in one Python file.
  • Adding README.rst would be better to recall the functionalities of the module in the future.
  • Add help text when needed.
  • Provide logs for debugging purposes.
  • Try to inherit and do super-call wherever possible. Overriding the method may break the natural behaviour of a method.
  • Follow method naming convention according to odoo guidelines, like the compute field for field_a should be def _compute_field_a or compute_field_a.
  • Use for loop or filtered method where a method expects multiple records, else use self.ensure_one() if the method expects a single record every time.
  • Utilize Odoo's translation features for user-facing strings. Use _(...)​ function for making translatable strings in Python code and XML templates.
  • If you want to add some logic in creating and updating records then use the inverse method.
  • Use list comprehension instead of a block of for loop.
  • Use the abstract model in the parent, if multiple objects share common fields, it helps reduce the load in the database.

3) XML Coding Standards:

Here are some general tips that you can use while developing a code in XML.

  • Always display the required field on a tree view use, form view, and search view, and provide the filter for the fields that are important in business logic. Use optional if the fields in the tree view are too many.
  • Use descriptive and meaningful IDs that reflect the purpose or functionality of the view.
  • Always add access rights if needed for security purposes.
  • Utilize odoo widgets for fields (e.g. selection, many2one, many2many)​ for appropriate field types to ensure consistent behaviour and user experience.
  • Include comments to explain the purpose or provide additional information about specific XML elements or block.
  • Utilize Odoo's localization features to support multilingual applications. Wrap translatable texts with the <data>​tag and use the data-translate​ attribute for automatic translation for a better user experience.

4) JS/OWL Coding Standards:

Here are some general tips that you can follow while developing code in js/owl. The formatting of syntax is same as defined in Python coding standards. 

  • Attach event listeners using addEventListener()​ in  JS rather than inline event handlers. Separate event handling code from HTML markup to improve maintainability.
  • Use arrow functions for anonymous functions or when the lexical scope is required.
  • Minimize the use of global variables and functions, most of the time we want to variable reset for every session, but global scopes of variables defied it, and because of that it may generate conflicts.
  • Use OWL Lifecycle Hooks, to perform actions at specific points in a component's lifecycle. Also, avoid performing heavy computations of making expensive requests in the mounted​ hook.
  • Avoid inline styles wherever possible.


Conclusion:

By following good coding standards brings numerous advantages, including improved code readability, maintainability, collaboration, and efficiency. It ultimately leads to higher code quality, reduced technical debt, and better overall software development outcomes.


Coding standards for odoo modules
BizzAppDev Expert July 15, 2023
Share this post
Sign in to leave a comment