Unordered scopes is just MS being lazy

Mark
2 min readApr 24, 2021

There’s no reason that you shouldn’t be able to do…

if (false)
{
bool x = false;
}
bool x = true;

…but you can’t. The reason you can’t is because MS considers the second declaration of x to have overlapping scope with the first, even though there isn’t a single point in the program where an unqualified reference to x is ambiguous. This is because in the compiler’s eyes, the code is indifferent from…

bool x = true;
if (true)
{
bool x = false;
}

…as far as scope is concerned.

This is just stupid and lazy on MS’ part. They’ve made many excuses over the years, mostly revolving around bugs arising from idiots moving their variable declarations further down the scope or otherwise confusing the two uses of x, as though you can’t already do…

private bool x = true;public void DoSomething()
{
bool x = false;
if (x) KillAllHumans();
}

…and fuck it up to…

private bool x = true;public void DoSomething()
{
if (x) KillAllHumans();
bool x = false;
}

If you’re some sort of smooth-brained arse-licker you’re probably parroting the Apple-forum playbook and muttering “but why would anyone what to do that” while stroking yourself off covered in your own faeces. Well, Mr bonus chromosome, it’s because not ever scenario is that simple, sometimes the same variable name is the appropriate one, and often it doesn’t improve readability to stick scope-spawning curly braces everywhere. For example, if you’re iterating over most of a loop and want to do something special with the last element…

string[] employeeNames = new[] { "Kimmy", "Tiffany", "Dra'nakyuek, Destroyer of Worlds" };
foreach (var employeeName in employeeNames.SkipLast(1))
{
// Code that operates on all bar the last name
}
var employeeName = employeeNames.Last();
// Code that operates on the last name

…or you return early to improve maintainability…

public double Calculate(double x1, double x2)
{
// Some logic
if (x1 < x2)
{
var distance = x2 - x1;
// Some more logic
return someCalculatedValue;
}
var distance = x2 - x1;
// Different logic
return someCalculatedValue;
}

--

--