C & C++

C & C++

Made by DeepSource

Multiple declarations in single statement CXX-C2013

Style
Minor

Local variable declarations with more than one variable in a single statement can lead to confusion and make the code harder to read. It is recommended to refactor the code to have one declaration per statement, with each statement on a separate line.

Using a single statement to declare multiple variables can make it difficult to track the initialization and usage of each variable. It also increases the risk of introducing bugs due to incorrect initialization or unintended side effects.

For example,

int* x, y;
// here x is a pointer but y is not a pointer

To fix this issue, consider isolating each declaration in individual statement.

Bad practice

void f() {
  int * pointer = nullptr, value = 42, * const const_ptr = &value;
}

Recommended

void f() {
  int * pointer = nullptr;
  int value = 42;
  int * const const_ptr = &value;
}

By declaring each variable on a separate line, the code becomes more readable and easier to understand. It also helps in maintaining consistency and reduces the chances of introducing errors during future modifications.