All Posts

5 Tips for Better Code Quality

Cover Picture

Many developers today focus more on having functional code rather than writing quality-based code. Quality code must meet certain criteria, such as sustainability, reliability, and maintainability. These criteria emphasize the importance of source code structure, coding style, clarity, naming conventions, and comments.

There are software standards, such as Software Quality Standards — ISO 5055, that help ensure code quality. Without adhering to these standards, obtaining certifications can be impossible, potentially dooming a project before it is finalized.

This guide will cover practical ways to improve code quality, with examples using embedded C programming.

1. Properly Structured Project and Source Code

Before coding, break the project into submodules and define the inputs and outputs of each module. Create interfaces to connect module outputs to inputs of other modules. This isolation:

  • Separates implementation from the interface
  • Enhances maintainability and sustainability
  • Prevents changes in one module from affecting others unnecessarily

This process is part of architecture and project design. Neglecting it often leads to spaghetti code. Spending time designing a proper architecture is crucial for sustainable and reliable code.

2. Naming Convention

Choose variable, function, data type, and file names that reflect their purpose and use.

Example 1: Function returning the max of two arrays

Not recommended:

uint8_t max(uint8_t* x, uint8_t* y, size_t z)
{
  ...
}

Recommended:

uint8_t max(const uint8_t* pArg1, const uint8_t* pArg2, size_t length)
{
}
  • pArg1: pointer to the first argument
  • pArg2: pointer to the second argument
  • length: size of both arrays

Example 2: Local variable names

Not recommended:

int a = 10;
int b = arg + a;
return b;

Recommended:

int tmp = 10;
int res = arg + tmp;
return res;
  • tmp: temporary variable
  • res: stores the result

3. Maintain Consistent Naming Convention

Consistency prevents confusion for yourself and others.

Not recommended:

uint8_t max(const uint8_t* pArg1, const uint8_t* pArg2, size_t x)
{
 ...
}

Recommended:

uint8_t max(const uint8_t* pArg1, const uint8_t* pArg2, size_t length)
{
 ...
}

4. Comments

While comments are important, structured code and consistent naming are even more critical. Proper comments describe function behavior, parameters, and return values.

Example function comment:

file: source.h
/**
 * max - return max value out of two array elements.
 *
 * @pArg1: First array pointer.
 * @pArg2: Second array pointer.
 * @length: Size of both arrays (must be equal).
 *
 * @return: Maximum value out of the two arrays.
 */
uint8_t max(const uint8_t* pArg1, const uint8_t* pArg2, size_t length);

file: source.c
uint8_t max(const uint8_t* pArg1, const uint8_t* pArg2, size_t length)
{
}

5. Documentation

Documentation is essential for released software, helping users integrate software easily and reducing support effort. Tools like Doxygen, Sphinx, and MkDocs can automate documentation generation from properly formatted code comments.

Ensure that function descriptions and comments are correctly structured for these tools to parse automatically.

---

By following these practices—structured projects, meaningful naming conventions, consistent code style, proper comments, and clear documentation—you can significantly improve the maintainability, reliability, and overall quality of your embedded C code.