<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Alice, Bob and Mallory: Get the value of and with using the null-coalescing operator</title>
    <link>http://alicebobandmallory.com/articles/2009/05/16/get-the-value-of-and-with-using-the-null-coalescing-operator</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>metasyntactics</description>
    <item>
      <title>Get the value of and with using the null-coalescing operator</title>
      <description>&lt;p&gt;I like the &lt;a href="http://msdn.microsoft.com/en-us/library/ms173224.aspx"&gt;?? operator&lt;/a&gt; that surfaced in C# 2.0. I'm often in environments where null values florish and ?? is a great way to handle them, especially for presentation. A while ago I found a use for the ?? operator in a way I'd never used it before.&lt;/p&gt;

&lt;p&gt;The problem at hand was to return an account number from data that could be described as somewhat inconsistent.&lt;/p&gt;

&lt;p&gt;The application had left room for the users to enter accounts in two by two ways. A customer could have multiple subsidiaries and in each subsidiary, by misconception, two different fields had been used as the account number. It was also possible to connect an account to all subsidairies of a customer at the same time as a specific subsidiary of that customer had an account defined.&lt;/p&gt;

&lt;p&gt;Only if a unique account could be found it should be returned. All other cases should return a cause of failure so that the users could use that information to clean up the mess.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#088;font-weight:bold"&gt;private&lt;/span&gt; &lt;span style="color:#088;font-weight:bold"&gt;static&lt;/span&gt; Account GetCustomerAccount(Customer customer)&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;  bool hasAccount = !&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt;.IsNullOrEmpty(customer.AccountNo);&lt;tt&gt;
&lt;/tt&gt;  bool hasAccountNoExtra = !&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt;.IsNullOrEmpty(customer.AccountNoExtra);&lt;tt&gt;
&lt;/tt&gt;  bool hasAllSubsAccount = !&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt;.IsNullOrEmpty(customer.AccountNoAllSubs);&lt;tt&gt;
&lt;/tt&gt;  bool hasAllSubsAccountNoExtra = &lt;tt&gt;
&lt;/tt&gt;       !&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt;.IsNullOrEmpty(customer.AccountNoExtraAllSubs);&lt;tt&gt;
&lt;/tt&gt;  var account = &lt;span style="color:#080;font-weight:bold"&gt;new&lt;/span&gt; Account();&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; ((hasAccount || hasAccountNoExtra) &amp;amp;&amp;amp;&lt;tt&gt;
&lt;/tt&gt;      (hasAllSubsAccount || hasAllSubsAccountNoExtra)) &lt;tt&gt;
&lt;/tt&gt;  {&lt;tt&gt;
&lt;/tt&gt;    account.Status = account.HasBothAllSubsAndSpecific;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; account;&lt;tt&gt;
&lt;/tt&gt;  }&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; ((hasAccount &amp;amp;&amp;amp; hasAccountNoExtra) ||   &lt;tt&gt;
&lt;/tt&gt;      (hasAllSubsAccount &amp;amp;&amp;amp; hasAllSubsAccountNoExtra)) &lt;tt&gt;
&lt;/tt&gt;  {&lt;tt&gt;
&lt;/tt&gt;    account.Status = account.HasBothAccountAndAccountExtra;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; account;&lt;tt&gt;
&lt;/tt&gt;  }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  account.AccountNo = customer.AccountNo ??&lt;tt&gt;
&lt;/tt&gt;                      customer.AccountNoExtra ??&lt;tt&gt;
&lt;/tt&gt;                      customer.AccountNoAllSubs ??&lt;tt&gt;
&lt;/tt&gt;                      customer.AccountNoExtraAllSubs;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (account.AccountNo==&lt;span style="color:#080;font-weight:bold"&gt;null&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;    account.Status=account.HasNoAccountDefined;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; account;&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;Notice how AccountNo is set to the first non null value of the four. Imagine that with plain ifs. Here I believe the ??-operator both makes the code clear and saves us from a bunch of nested ifs.&lt;/p&gt;</description>
      <pubDate>Sat, 16 May 2009 00:38:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:41954150-6016-4a3f-a30e-c1324acfc4a4</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/05/16/get-the-value-of-and-with-using-the-null-coalescing-operator</link>
      <category>C#</category>
    </item>
  </channel>
</rss>
