tag:blogger.com,1999:blog-2629608947246129428.post4754797712017874718..comments2024-02-18T05:01:43.733-07:00Comments on Reference Bits: Chaining the C# Null Coalescing OperatorRudy Lacovarahttp://www.blogger.com/profile/15639782534216467791noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-2629608947246129428.post-596663726052790192013-09-14T02:58:05.252-06:002013-09-14T02:58:05.252-06:00perfect !!perfect !!Johhnynoreply@blogger.comtag:blogger.com,1999:blog-2629608947246129428.post-80082156625468703952013-06-27T00:36:41.308-06:002013-06-27T00:36:41.308-06:00Nice post very helpful
DBAKingsNice post very helpful<br /><a href="http://dbakings.com/CSharp/csharpoperators.aspx" rel="nofollow">DBAKings</a>Anonymoushttps://www.blogger.com/profile/14178155909391788234noreply@blogger.comtag:blogger.com,1999:blog-2629608947246129428.post-26354112251607937882013-01-15T17:34:04.908-07:002013-01-15T17:34:04.908-07:00I like the coalescing operator better. Much less v...I like the coalescing operator better. Much less verbose without sacrificing readability. The only thing I can think of improving it is to return the result immediately without setting it to the _currentUser.<br /><br />// CurrentUser<br /> private WebUser _currentUser;<br /> public WebUser CurrentUser<br />{<br /> get<br /> {<br /> return _currentUser ??<br /> GetWebUserFromSession() ??<br /> GetWebUserFromTrackingCookie() ??<br /> CreateNewWebUser();<br /> }<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2629608947246129428.post-89311603207202243292009-09-23T17:16:33.586-06:002009-09-23T17:16:33.586-06:00I use the pattern quite often. As Xeno pointed out...I use the pattern quite often. As Xeno pointed out, the readavility can be improved (of course this is subjective) if the operator is at the BEGINNING of the line (where the eye tends to scan).David V. Corbinhttp://www.dynconcepts.comnoreply@blogger.comtag:blogger.com,1999:blog-2629608947246129428.post-67185292504744857002009-08-05T12:05:18.192-06:002009-08-05T12:05:18.192-06:00I like it better, though a compromise on readabili...I like it better, though a compromise on readability between the two might be better:<br /><br />if (null == _currentUser) { _currentUser = foo ?? bar ?? ...; }<br />return _currentUser;<br /><br />Some people take a while to grok the coalesce operator (in any language). You can do the same thing in JavaScript with the logical or operator: var foo = var1 || var2 || bar || 0; Or in SQL with the coalesce and isnull functions: select coalesce(col1, col2, 42) from ... or select isnull(col1, 42) from ...<br /><br />What I don't like though is 1) the lack of "this" usage and 2) the lack of defense programming.<br /><br />Using "this" when referencing instance fields, properties, and methods keeps the code much more tidy, easier to understand by newcomers, lessens the chance of variable/scope confusion, and saves you a mouseover in your IDE to see where the variable originates.<br /><br />The defensive programming technique of placing the constants or unassignables on the left side of an [in]equality statement keeps you from accidentially creating tautalogies: if (null = foo) vs if (null == foo)<br /><br />One other syntax optimization I've found is to always start the line of a multiline statement with the operator instead of ending the line with it:<br />foo = bar<br /> || stuff<br />|| 42;<br />doStuff(<br />foo<br />, bar<br /> , stuff<br />);<br />I've found this makes debugging and maintenance take less keystrokes, introduce less mistakes, and is easier to read due to the quick notice that it is a multiline statement, and exactly what the operators are.<br /><br />But maybe that's just me.<br /><br />PS - Is there some reason why can I not cut, copy, paste, or use arrow keys in this comment textarea?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2629608947246129428.post-1949892310835576312009-07-29T11:56:05.147-06:002009-07-29T11:56:05.147-06:00I was unaware of this operator, and I like it. Wh...I was unaware of this operator, and I like it. While I agree with your addendum decision that the original code is more readable to the majority of developers, the null coalescing operator is clear once one internalizes its meaning, much like the ternary operator.<br /><br />Put me down in the 'like' category.Darrylhttp://howcouldyoubesostupid.comnoreply@blogger.comtag:blogger.com,1999:blog-2629608947246129428.post-75870968929308699922009-07-22T01:14:40.348-06:002009-07-22T01:14:40.348-06:00I think you should encapsulate the WebUser "f...I think you should encapsulate the WebUser "find" strategy as well using a method that gets a lambda array passed an returns the first found WebUser.<br /><br />Something like this:<br /><br />private WebUser FindWebUser(Func<WebUser>[] webUserFinders)<br />{<br /> foreach (var webUserFinder in webUserFinders)<br /> {<br /> var webUser = webUserFinder();<br /> if (webUser!=null)<br /> {<br /> return webUser;<br /> }<br /> }<br /><br />throw new InvalidOperationException("Could not find a valid web user");<br />}Saintedlamahttps://www.blogger.com/profile/09572484757741600331noreply@blogger.com