by gambler · 12y ago · 0 comments
So far null propagation seems like the most useful feature for cleaning up cluttered code I'm used to dealing with.
Auto-property initializers are nice, but the syntax looks a bit ugly and I don't see them making that big of a difference in readability. I would rather see something that tackles the ugly-but-necessary construct I see the most: lazy-loading getters. Generic memorization of methods and properties would be even more useful. And yet even more useful would be something that could be used for both memoization and transparent caching:
[Cache(For = 1000 /*milliseconds*/)]
public GetSomethingFromDB(){ ... }
That would be tough to implement well, though. Unless they added Python-like attributes. Which would give us memoization, caching an a myriad other things.The first one is an answer I wrote on SO and the other is a library I wrote a couple of years ago. Both are are different solutions to the caching problem you describe.
So in your example you could create one to detect your attribute and inject custom logic instead. You can unleash the full power of AOP at the compilation level.
private Lazy<string> _someVariable =new Lazy<string>(SomeClass.IOnlyWantToCallYouOnce);
public string SomeVariable {
get { return _someVariable.Value; }
}
Imagine the same things with a long lambda inside.