-1

i was looking around in stackoverflow whether putting null coalescing operators within an accessor method has any performance implications.

Before:

private Uri _Url;
public Uri Url
{
    if(_Url == null)
        _Url = new Uri(Utilities.GenerateUri());
    return _Url;
}

After:

private Uri _Url;
public Uri Url 
{
    get 
    {
        return _Url = _Url ?? new Uri(Utilities.GenerateUri());
    }
}

I'm not even sure if the syntax is correct, but when i debug, the private object is set.

Before anyone ask what's the point of doing it, we were debating internally whether to write for readability (the first looks more readable to me), or to write for performance.

I don't know whether the compiler will optimize ?? better than a manual null check all the time. Micro-optimization is bad, but i am just curious

Lee Gary
  • 2,357
  • 2
  • 22
  • 38
  • I am pretty sure those two produce [almost] the same IL. Unfortunately, my environment does not give me possibility to check that. As a rule, always go for readability unless performance is critical. – Leri Oct 23 '13 at 07:02
  • Pure opinion based question - pick version that you like more. If you believe there is any performance implications - measure or look at IL. (I expect almost zero difference... but will not bet on any outcome... one extra assignment unlikely to cause any measurable impact) – Alexei Levenkov Oct 23 '13 at 07:02

1 Answers1

2

You can actually write this as:

return _Url ?? (_Url = new Uri(Utilities.GenerateUri()));

As far as performance goes, it is practically the same thing as using if, so no difference.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • your syntax definitely looks neater and succinct – Lee Gary Oct 23 '13 at 07:09
  • @LeeGary This is better one-liner than yours but... Do you really want that? What if non-C# developer is going to work with your code? I used to use very language specific stuff, but when I left my first job, during first month I was spending 1-2 hours per-day explaining what was this or that operator doing to a new dev in that company. Since then I only use such features only when they really give me something important. This can lead to open discussion, however. – Leri Oct 23 '13 at 07:14
  • @Leri hmm, i disagree with with not using language specific stuff, i'm more inclined towards leveraging on what the language offers. Just like the debates about using var in .net, it makes some feel that it makes the code looks very javascripty, but it actually improves productivity.. – Lee Gary Oct 23 '13 at 07:17
  • @Leri [link](http://www.sitepoint.com/google-closure-how-not-to-write-javascript/) i think the author pretty much sum up my opinion. Google tried to make the library more java friendly – Lee Gary Oct 23 '13 at 07:20
  • @LeeGary As I said there is no right or wrong answer here. It's just personal preference. – Leri Oct 23 '13 at 07:24
  • 1
    @LeeGary Speaking of `var` keyword you should really use it only when type is clearly written on the right hand-side of statement. I am pretty sure there's some reference on msdn about this. – Leri Oct 23 '13 at 07:36