Coding Standards

2. Examples and Discussion

Coding standards promote readability and maintainability---important features for code that is likely to be used over a period of years or reused on other projects and SWE-061 - Coding Standards in NPR 7150.2 083 calls for the use of secure coding standards. A quality set of coding standards can improve the quality of code in many other ways as well. The list below specifies a minimal set of standards commonly found in standards for high-reliability software. These rules have been found to reduce the likelihood of run time errors.

  1. Restrict the use of dynamic memory allocation to one-time events at system initialization.
  2. Use the simplest flow control constructs possible, and avoid the use of “go to”, setjump/longjump.
  3. Ensure that all loop constructs have a fixed upper bound, and avoid the use of recursion.
  4. Declare data objects at the lowest scope possible.
  5. Verify the validity of input parameters at the start of each function, and take appropriate action when inputs are off-nominal. (see also Invalid Data Handling and Data Interface Integrity design principles)
  6. Implement logic to check, and possibly act, on the return value of non-void functions.

A complete set of coding standards typically includes best practices, such as strict language adherence and the use of automated tools to identify problems either at compile time or run time. Language-specific guidance, domain-specific guidance, local standards for code organization and commenting, and standard file header format are often specified as well.

Good coding standards have two things in common: (1) they are concise and relevant enough to be easily used by programmers and quality assurance professionals in their daily activities; and (2) they enable the use of automated code checking. The first is important for obvious reasons. A standard that is too cumbersome will quickly fall into disuse. The second is important because it reduces cost and increases the likelihood that violations that would lead to program errors will be caught before deployment.

A draft version of design principles produced by the Marshall Space Flight Center (MSFC) includes guidance on the use and development of coding standards. In keeping with a “keep it simple” approach, the standard had only six mandatory practices applicable across all programming languages and allows the inclusion of language-specific and project-specific aspects. The mandatory practices strongly overlapped with the minimal set above, and the standard made it clear that the fundamental practices were not to be overridden by language-specific standards, which in turn were not to be overridden by project-specific standards. This tiered approach makes it easier for an organization to implement a basic set of standards that may be tailored on a per-project basis. This approach supports a Capability Maturity Model Integration (CMMI) Level 3 and above organization.

JPL has taken a somewhat different approach. All projects are required to identify the coding standards used in the development, but no specific standard is imposed. An institutionally supported C coding standard is available. This standard is broken down into six levels of compliance, each more rigorous than the next. Projects that use the institutional standard must determine the level of compliance appropriate for their project. Projects are allowed to produce their own standard, use an alternative standard, or tailor the institutional standard. Except for a general provision concerning safety-critical software, there are no restrictions on the standards that may be used. This approach also supports a CMMI Level 3 certification.

Specific coding standards in use include JPL's C Coding Standard 331 , GSFC's C Coding Standard for Flight Software 333, and GSFC's C++ Coding Standard for Flight Software 338, which is all available in the Software Processes Across NASA (SPAN)  library. Another resource for coding standards is Gerard Holzmann's "Power of Ten" 417 paper. There are a number of static analysis tools that can help verify adherence to coding standards, including VectorCast, Polyspace, and Klocwork.

3. Inputs

3.1 NESC inputs Coding and Implementation Issues Related to Reliability 669

Definition of suitable coding standards and conventions: Coding standards and conventions can enhance reliability by considering such issues as:

  • Policies on dynamic memory allocation in safety-critical systems (generally, it should not be allowed)
  • “Defensive” coding practices for out of range inputs and response times
  • Exception handler implementation
  • Coding to enhance testability and readability
  • Documentation to support verification
  • Interrupt versus deterministic timing loop processing for safety-critical software
  • Policies on allowable interprocess communications mechanisms (e.g., point to point versus publish and subscribe)
  • Permitted use of dynamic binding (an alternative is static "case statements")
  • Policies on initialization of variables (some standards prohibit the assignment of dummy values to variables upon initialization in order to enable detection of assignment errors in subsequent execution)
  • Use of “friend” (C++) or “child” (Ada) declarations to enable testing and evaluation of encapsulated data code during development without requiring the subsequent removal of “scaffold code”.
  • For object-oriented languages, limitations on levels of inheritance in order to prevent “accidental inheritance” due to the introduction of variables with the same name or variable misspelling.

3.2 Other Inputs

4. Resources

4.1 References

  • (SWEREF-083)

    NPR 7150.2D, Effective Date: March 08, 2022, Expiration Date: March 08, 2027 Contains link to full text copy in PDF format. Search for "SWEREF-083" for links to old NPR7150.2 copies.

  • (SWEREF-331) JPL Institutional Coding Standard for the C Programming Language Version: 1.0, Date: March 3, 2009, JPL DOCID D-60411This NASA-specific information and resource is available in Software Processes Across NASA (SPAN), accessible to NASA-users from the SPAN tab in this Handbook.

  • (SWEREF-333) C CODING STANDARD GFSC - Flight Software Branch - Code 582, Version 1.1 - 11/29/05, 582-2000-005,This NASA-specific information and resource is available in Software Processes Across NASA (SPAN), accessible to NASA-users from the SPAN tab in this Handbook.

  • (SWEREF-338) C++ CODING STANDARD GSFC, Flight Software Branch - Code 582, Version 1.0 - 12/11/03, 582-2003-004This NASA-specific information and resource is available in Software Processes Across NASA (SPAN), accessible to NASA-users from the SPAN tab in this Handbook.

  • (SWEREF-417)

    Holzmann, G.J., NASA Jet Propulsion Laboratory (JPL), 2006.

  • (SWEREF-439)

    The NASA Lessons Learned system. The system provides access to official, reviewed lessons learned from NASA programs and projects.

  • (SWEREF-669)

    NASA/TM-2008- 215126/Vol II, NESC-RP-06-108/05-173-E/Part 2, April, 2008, See report section: Coding and Implementation Issues Related to Reliability

  • (SWEREF-684)

    Public Lessons Learned Entry: 0369, Date: 1995-01-24, Submitting Organization: JPL, Submitted by: B. Larman

5. Lessons Learned

5.1 NASA Lessons Learned

Lessons that appear in the NASA LLIS or Center Lessons Learned Databases. 439

  • Flight Software Deadly Embrace. Lesson Learned 0369: 684 "During a walk-through of the Galileo Spacecraft System fault protection implementation a possible "deadly embrace" in the flight software was uncovered. A deadly embrace is a continuous software looping operation that may preclude the achievement of an acceptable spacecraft state."

  • No labels