6
$\begingroup$

What are the advantages of conditionals having their own scope? I know this can lead to some extra variables for some programs. But it can also be more memory efficient.

Credit to Hg0428 for the idea on A51

$\endgroup$
3
  • $\begingroup$ I recommend you to request for permission first, before putting it on the main site. $\endgroup$ Commented May 17, 2023 at 13:22
  • 1
    $\begingroup$ Which part of the conditional are your referring to? if (part1) { part2 } $\endgroup$
    – Max Heiber
    Commented May 17, 2023 at 21:51
  • 2
    $\begingroup$ Do you also want the disadvantages as well? pros-cons means both advantages and disadvantages $\endgroup$ Commented May 18, 2023 at 12:38

2 Answers 2

3
$\begingroup$

Consistency in how scopes work

There are two main ways languages handle non-global variable scoping:

  • Function scoping
  • Local (or block) scoping

In function scoping, like JavaScript's var, a variable declared anywhere in a function is accessible anywhere after. In block scoping, a variable can only be accessed in the block it's declared in.

The latter is preferable, as function scoping can easily hide bugs. For example, when multiple for-loops are used in the same function:

for (var x in 0..10) {
    for (var y in 0..10) {
        tick_farmland(x, y);

        if (is_grown(x, y)) {
            print("Finished growing crop at ({}, {})", x, y);
        }
    }
}

for (var x = 0; x < 100; y++) {
    print("{}% loaded...", x);
}

Did you catch it? Well, the (interpreter/compiler) didn't. That last loop used y++ instead of x++ by mistake. Since the scope of x and y from the previous loop escapes the first for loop, y is still 9 when the second loop runs, and the interpreter/compiler won't see anything wrong. Block scoping would fix this, since y would be undefined outside of the first loop.

For consistency's sake, it's best to handle all blocks the same. If for loops were block-scoped but if statements used their enclosing scope, this would create confusion, and possibly bugs if an if is turned into a for or vice versa.

$\endgroup$
2
  • 1
    $\begingroup$ On the other hand, there have been times that I want to access the index variable later — for example, if the loop exited by break and I want to know where. $\endgroup$
    – Bbrk24
    Commented May 18, 2023 at 14:45
  • 1
    $\begingroup$ @Bbrk24 In that case, you could just declare the index variable in a higher scope. That also makes your intentions clearer, aiding readability. $\endgroup$ Commented May 18, 2023 at 14:53
2
$\begingroup$

It invites type errors

Since an if statement may or may not run, this would mean that a variable declared in an if statement may or may not exist to the code following it. While in a dynamically typed language this could be acceptable, it would still be a likely place for bugs.

Sometimes the variable might not exist at all after the if:

if (condition) {
    var timestamp = now();
}

print(timestamp);

Sometimes the variable may have a different type:

if (condition) {
    var x = 1;
} else {
    var x = "a";
}

The above would likely be rare...intentionally, at least. It would likely start as two branches that set the same variable to two different values of the same type. Until someone updates one branch and forgets about the other, creating a mismatch between what types get returned. A more foolproof way to do this would look like this:

var x: int;

if (condition) {
    x = 1;
} else {
    x = 97;
}

Importantly, the variable is already outside the scope of the if's block. That's what makes this safe.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .