Image Blog QAC Using Coding Standards
February 8, 2018

Coding Standards Best Practices

Static Analysis
Security & Compliance

Coding best practices when paired with a coding standard help you to ensure that your code is safe, secure, and reliable. This is especially important if you're developing a safety-critical product, such as an automobile.

However, writing good code can be difficult. For that reason, we explain how following best practices for using coding standards help you to write high-quality code, share nine coding best practices, and explain how to choose the best coding standards for your specific project.

Read along or jump ahead to the section that interests you the most:

➡️ start Your Free Static Code Analysis

Back to top

Coding Best Practices

The best way to write good, high-quality code is to use a coding standard.

Coding standards are guidelines and they’re often required to comply with a safety or security standard.

📕 Related Resource: Learn how to improve your code quality.
Back to top

9 Coding Standards Best Practices

Here are the top 9 coding standards best practices:

1. Know Why You're Using It

2. Choose the Best Coding Standard for Your Industry

3. Use Coding Rules and Follow Recommendations

4. Describe the Intent Behind the Rule

5. Update Coding Standards With Care

6. Consider Open vs. Closed Standards

7. Prioritize Coding Rules

8. Plan for Rule Deviations

9. Educate Programmers

Back to top

Coding Standards Best Practices: A Closer Look

1. Know Why You're Using It

A coding standard’s purpose is to restrict use of problematic areas of the programming language.

So, using coding standards prevents undefined or unspecified behavior. And it limits the use of error-prone constructs, such as "goto".

And using coding standards also improves the code’s readability, maintainability, and portability. One example is the use of “typedef” to simplify the syntax of complex structures. This reduces overall cyclomatic complexity

2. Choose the Best Coding Standard for Your Industry for Coding Standards Best Practices

The coding standard must be recognized and accepted by the industry for which it is being used. Choosing one recognized by your industry is extremely important. It reduces the amount of qualification work to satisfy your end users.

A good example is MISRA C and C++. These were written for the automotive industry and have since become the de facto standards for safety-critical applications. They're recognized as one of the best coding standards for embedded industries.

📕 Related Resource: Learn more about how you can benefit from using MISRA.

3. Use Coding Rules and Follow Recommendations

Coding standards usually include two elements — rules and recommendations (or directives).

Rules are the backbone of a coding standard.

Code compliance is normally defined in term of rules. Modern rules are often stated in such a way that makes it easy to verify them with a static analyzer. For example, the rules are “statically enforceable”.

Recommendations (or directives) are usually advisory.

It’s up the engineer whether to follow them or not. Normally they do not impact compliance. They usually aren’t statically enforceable.

Most coding standards have both rules and recommendations. However, some coding standards only have rules — such as ISO/IEC TS 17961: 2013 C secure coding rules and MISRA C:2004.

4. Describe the Intent Behind the Rule

Each rule should have an identification string, a headline, and a description.

The identification string identifies the rule. For example, “Rule 10.3” or “EXP30-C”.

The headlines summarize the intent of the rules. For example, “There shall be no dead code” or “Freeing memory multiple times”.

The description elaborates on the headline and provides suitable context.

The description can also discuss how it is applied. For example, whether it can be assessed by analyzing a single transaction unit or the entire project.

And it could include code examples of compliant and noncompliant behavior — and exceptions to the rule.

5. Update Coding Standards With Care

Frequent updates to a coding standard may not be a good thing. This is especially true in safety-critical applications where long lifecycles are the norm. And these industries tend to be suspicious of using new language constructs that have not yet been proven reliable.

The sequence of updates to the MISRA standard is a good example of having significant delays between each version. C99 was finalized in 2011. But it wasn’t until 2013 that MISRA C: 2012 updated the standard to C99. And MISRA C++ — published in 2008 — marked the update to the 2003 version of C++.

6. Consider Open vs. Closed Standards

Some coding standards are open to community participation — such as CERT. Others are closed and deciding by working groups — such as MISRA.

So, CERT puts the emphasis on community cooperation. The result is a coding standard in the form of an open, freely available web-based wiki. Users can comment on single guidelines. These comments are considered when the guidelines are reviewed.

MISRA is developed through working groups that create and update standards according to a pre-decided roadmap. This top-down model leads to the work not usually being publicly available. There is normally a price to pay, which helps cover the expenses of the working group. However, MISRA is influenced by community feedback, too.

The main difference between open versus closed standards is how quickly they change.

Open standards tend to be extremely dynamic. Guidelines change quickly. Closed standards, however, are better for safety-critical industries. They provide a stable reference to meet mandatory requirements.

7. Prioritize Coding Rules

When you check your code against a coding standard, you might get a wall of bugs. Categorizing the rules with a severity matrix helps you assess code quality and prioritize bug fixes.

Assessing the code quality helps you decide whether to move into production. If there are major problems, it’s not ready. If the problems are minor, then maybe it can go into production and be fixed later.

Defects need to be prioritized so the high severity ones are fixed first. This is particularly useful for coding standards with many rules.

8. Plan for Rule Deviations

Coding rules don’t fit all situations. There will always be deviations.

The three key factors for planning for rule deviations:

  • Decide which guidelines can be deviated from and which cannot
  • Specify how a deviation can be done properly
  • List what needs to be documented or recorded when a deviation takes place.

Having a plan for rule deviations will help you if you need to provide evidence to an auditor.

9. Educate Programmers on Coding Standards Best Practices

One of the goals of coding standards is to educate programmers. And supporting material to a coding standard can provide that education.

Supporting material helps the programmer understand the background or give extra information for those who want a more detailed knowledge.

For example, this might include links to documentation such as ISO standards and books. Or there might be references to other coding standards or reports of real-world problems caused by violations of the guidelines. There might also be information on risk mitigation actions or solutions to specific problems.

Back to top

Choosing the Best Coding Standards for Coding Best Practices

So, how do you choose a coding standard?

You can use these coding standards best practices as a starting point. The next step is to evaluate coding standards based on your needs.

For instance, the embedded systems industry primarily uses C and C++ programming languages. There are several coding standards that support these languages — and each one is different.

📕 Related Resource:Learn how to choose the right coding standard.

Make Sure Coding Standards Best Practices Are Followed

A coding standard only works if it's followed. You can make sure coding standards best practices happen by using a static analyzer, such as Helix QAC or Klocwork. Register for a free trial.

➡️ static analysis free trial

 

Back to top