Main Content

MISRA C++:2023 Rule 10.0.1

A declaration should not declare more than one variable or member variable

Since R2024b

Description

Rule Definition

A declaration should not declare more than one variable or member variable.

Rationale

Declaring more than one variable in a declaration statement can make the code confusing to read and makes determining the variable type more difficult. Declare each variable using a separate declaration statement.

Polyspace Implementation

Polyspace® reports a violation if you declare more than one variable in a single declaration.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, the member variables x and y are declared in the same declaration. In foo(), the objects p1 and p2 are also declared in the same declaration. Polyspace reports violations of this rule for both of these statements.

class Point {
private:
    double x,y; //Noncompliant
public:
    Point(double xCoord, double yCoord) : x(xCoord), y(yCoord) {}

    //...
};
double calculate_distance(const Point& A, const Point& B);
void foo(){
	Point p1(2.0,3.0), p2(5.0,7.0); //Noncompliant
	calculate_distance(p1,p2);
}

Check Information

Group: Declarations
Category: Advisory

Version History

Introduced in R2024b