Quality Starts with Code: Using Maintainable Code for Better Results

By Abhik Dey

Product Manager

ETAS

July 16, 2024

Blog

Quality Starts with Code: Using Maintainable Code for Better Results

Model-based design and auto code generation tools play a significant role in the development of software for complex embedded systems especially in domains of automotive, railways, medical technology, and other industries.

MBD promises inter-disciplinary collaboration, more efficient development on different abstraction levels, and fewer bugs, which all together lead to cost saving and faster time to market.

The reality sometimes looks different, however. Due to the need for long project lifetimes, safety requirements, and increased complexity, there are a lot of tough processes, a huge tool ecosystem, extensive verification, and important validation cycle implementation. All of these must be done to reach the necessary quality.

This implies a very high cost-of-change, which can lead to attitudes like: "avoid any unnecessary change" or "never change a running system."

Going one level deeper, the problem starts with model-based design and the conveniences of using graphical modeling. Diagrams are big, deeply nested and the auto generated code for each function can even exceed 1000 lines with challenging complexity and maintainability. The developer can run simulations and etablish confidence when the plots look correct. It then gets handed over to other teams for extensive V&V. Bugs are reported back from downstream quality gates much later. Technical debt in the model keeps rising but not tracked, gets ignored because unlike code written in text, it is an accepted deviation. Having low technical debt in code can really make a difference to the business.

You can add more value to your product by updating code over its life cycle. Adding new features and improving existing features can lead to customer satisfaction, rather than frustration. You can mitigate critical bugs in the field by shipping maintenance releases faster. For this, you need to have confidence in code, which comes from putting emphasis on quality upstream which can save lots of time and money downstream.

Why not write maintainable code with this philosophy, even with model-based design?

Code quality aspects which are important especially for graphical models:

  • The model should be easy to understand and interpret by all stakeholders, including designers, developers, and end-users.
  • Clear and consistent naming conventions: Use descriptive and meaningful names for variables, functions, and classes to make the code easier to understand.
  • Modular and well-structured code: Break down the code into smaller, manageable modules or functions that have a single responsibility, making it easier to understand and parse. This also contains small diagrams, with low nesting levels and canvas size.
  • Use proven metrics like cyclomatic complexity to check your models.
  • Encapsulate reusable logic as shared models.

Testability of models and generated code is one of the keys to quality. Without a modular design, clear separation of concerns and minimal dependencies for the module's testing is hard and expensive. So, refactoring of legacy models and embracing the ideas of test-driven development philosophy is also important to reaching the goals which MBD initially promised. It's also essential to write unit tests and execute them on every change in order to enable faster feedback loops.

For example, with ASCET, a tool for this purpose, users get the convenience of graphical programming and auto generation of high-quality C code, but it’s built on foundation of proven software engineering paradigms like test driven development, object-oriented design, static analysis, and maintainability metrics. The modeling language is text based, code centric and suitable for modern collaborative workflows.

Good tools are important enablers, and it’s unbelievably satisfying to write maintainable models and auto generate code.

17 years experience in automotive industry across projects in vehicle control systems and product development.

More from Abhik