In the pseudo code below, the variable key must be declared and initialized outside of the context which it is used/relevant because there are two disparate if-blocks with the exact same condition.
I was thinking it might be cool if a language could consider two if blocks like that to have the same scope. What are the drawbacks, if any, to this approach?
Before:
class Foo
static bool CacheEnabled = true
static Foo Get(string name)
Foo result
string key = "cache_" + name
if(CacheEnabled)
if(Cache.GetFoo(key, out result))
return result
result = new Foo()
//
// rest of initialization here..
//
if(CacheEnabled)
Cache.PutFoo(key, result)
return result
After
class Foo
static bool CacheEnabled = true
static Foo Get(string name)
Foo result
if(CacheEnabled)
string key = "cache_" + name
if(Cache.GetFoo(key, out result))
return result
result = new Foo()
//
// rest of initialization here..
//
if(CacheEnabled)
Cache.PutFoo(key, result) // key is available since the condition is the same
return result
rest of initialization). Even if you restrict it to bare variables as in your example, if they're not local (the one in your example isn't) it's often -- sometimes literally impossible -- to computationally check that it isn't changed between the two conditions. – Oct 10 '14 at 21:37