Barr Group FacebookBarr Group TwitterBarr Group LinkedInBarr Group Vimeo

Rules

The following C coding rules relate to the use of functions in embedded software:

Rule 6.2.a.) All reasonable effort shall be taken to keep the length of each function limited to one printed page, or about 50-100 lines.

Rule 6.2.b.) Whenever possible, all functions shall be made to start at the top of a printed page, except when several small functions can fit onto a single page.

Rule 6.2.c.) All functions shall have just one exit point and it shall be at the bottom of the function. That is, the keyword return shall appear a maximum of once. (Note: [IEC61508 requires it.)

Rule 6.2.d.) A prototype shall be defined for each public function in the module header file.

Rule 6.2.e.) All private functions shall be defined static.

Rule 6.2.f.) Each parameter shall be explicitly declared and meaningfully named.

Example

uint8_t 
state_change (uint8_t event)
{
    uint8_t result = ERROR;

    if (EVENT_A == event)
    {
         result = STATE_A;
         // Don’t return here.
    }
    else
    {
        result = STATE_B;
    }

    return (result);
} 

Reasoning

Code reviews take place at the function level. Each function should be visible on a single printed page, so that flipping back and forth (a distraction) is not necessary. Similarly, multiple exit points are distracting to reviewers and thus do more harm than good to readability.

Exceptions

None.

Enforcement

Compliance with these rules shall be checked during code reviews.

What’s happening and how it’s done. Get in the know.

Sign up for our newsletter today!

Receive free how-to articles, industry news, and the latest info on Barr Group webinars and training courses via email. 

To prevent automated spam submissions leave this field empty.