Main Content

CERT C: Rec. EXP07-C

Do not diminish the benefits of constants by assuming their values in expressions

Since R2024a

Description

Rule Definition

Do not diminish the benefits of constants by assuming their values in expressions.1

Polyspace Implementation

The rule checker checks for Use of hard coded numeric value in operations.

Examples

expand all

Issue

Polyspace® reports a violation of this rule if you use a hard-coded numeric literal in an operation instead of symbolic constants. Polyspace checks operations involving the operators +, -, *, /, <<, and >>. Violations are not reported on the use of 0, -1, 1, 2 and their floating point representations.

Risk

When you hard-code the numerical value instead of using the symbolic constant in operations, the code loses the context of the value and becomes difficult to maintain. For example, consider this code:

double velocity;
double elapsed_time;

//...
double distance = 2.5 * 6.8;
//...
distance = 2.5 * 7.2;
While the value of distance can be accurate, this code does not communicate that the value of distance is the product of velocity and elapsed time. Furthermore, changing the velocity requires updating each hardcoded value in all the expression involving velocity, instead of updating the variable velocity.

Fix

To fix this violation, replace each hard-coded numeric literal with a symbolic constant.

If you do not want to fix the issue, justify the result by adding a comment to the result or to the code. See Address Results in Polyspace User Interface Through Bug Fixes or Justifications.

Example – Use of Numerical Constant

In this example, the use of a hard-coded numeric literal violates the recommendation.

double change2Natural(double log10) {
	return log10 * 0.4342944819;  //Noncompliant

}
Correction – Use Symbolic Constant

The function converts a base 10 logarithm to a natural logarithm by returning the input multiplied by log10(e). This is clarified when the return statement uses the symbolic constant log_e instead of a hard coded value.

double change2Natural(double log10) {
	double log_e = 0.4342944819;
	return log10 * log_e; //Compliant

}

Check Information

Group: Rec. 03. Expressions (EXP)

Version History

Introduced in R2024a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.