Unordered scopes is just MS being lazy

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;
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Who writes the Cucumber scenarios?

I am getting cheaper pricing for hosting from other providers. Why is your price high?

CSS Selectors — What you need to know

CSS Selectors, the ultimate guide with examples

Rethinking Requirements (5)

Reverse String in Swift

Configuring DKIM Entries — Sending NetSuite Emails from your Domain

Why We Should Learn a Programming Language Like Python?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mark

Mark

More from Medium

From Tiger to Bengal, Ja’marr Chase’s Impact on the Cincinnati Bengals

THE ULTIMATE GUIDE TO AT HOME FITNESS-HOW I GOT IN THE BEST SHAPE OF MY LIFE …..AND

My Sentence Has Ended

Dear Sylvia Plath,