Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing variables in an "if" statement

Tags:

c++

c++17

I read that in C++17 we can initialize variables in if statements like this

if (int length = 2; length == 2)     //execute something 

Instead of

int length = 2; if (length == 2)     //do something 

Even though it is shorter, it affects the code readability (especially for people who don't know this new feature), which I suppose is a bad coding practice for large software development.

Is there any advantage of using this feature other than making the code shorter?

like image 313
Arne Avatar asked Jul 03 '19 08:07

Arne


People also ask

Can we initialize variable in IF statement?

C++17 If statement with initializer C++17 has extended existing if statement's syntax. Now it is possible to provide initial condition within if statement itself. This new syntax is called "if statement with initializer". This enhancement simplifies common code patterns and helps users keep scopes tight.

Can you initialize a variable in an if statement Java?

Java allows you to declare variables within the body of a while or if statement, but it's important to remember the following: A variable is available only from its declaration down to the end of the braces in which it is declared.

Can you initialize variable in IF statement Python?

Control blocks like If statements in Python do not count and the variables used or initialized inside the block of an If statement can also be used and accessed outside its scope.

What is the correct way of initializing the variable?

Generally, all variables should be explicitly initialized in their declaration. The descriptive comment is optional. In most cases, variable names are descriptive enough to indicate the use of the variable. However, provide a comment if further description is appropriate or if an initial value is unusual.


1 Answers

It limits the scope of length to the if alone. So you get the same benefits we originally got when we were allowed to write

for(int i = 0; i < ... ; ++i) {    // ... } 

Instead of the variable leaking

int i; for(i = 0; i < ... ; ++i) {    // ... } 

Short lived variables are better for several reasons. But to name a couple:

  1. The shorter something lives, the less things you need to keep in mind when reading unrelated lines of code. If i doesn't exist outside the loop or if statement, then we don't need to mind its value outside of them. Nor do we need to worry its value will interact with other parts of the program that are outside of its intended scope (which may happen if i above is reused in another loop). It makes code easier to follow and reason about.

  2. If the variable holds a resource, then that resource is now held for the shortest period possible. And this is without extraneous curly braces. It's also made clear the resource is related to the if alone. Consider this as a motivating example

    if(std::lock_guard _(mtx); guarded_thing.is_ready()) { } 

If your colleagues aren't aware of the feature, teach them! Appeasing programmers who don't wish to learn is a poor excuse to avoid features.

like image 188
StoryTeller - Unslander Monica Avatar answered Sep 22 '22 13:09

StoryTeller - Unslander Monica