<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>kushalm.com &#187; Javascript</title>
	<atom:link href="http://kushalm.com/category/programming/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://kushalm.com</link>
	<description></description>
	<lastBuildDate>Wed, 08 Sep 2010 18:37:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The Null Coalescing Operator (Or how to make Default values sound frightening)</title>
		<link>http://kushalm.com/the-null-coalescing-operator-or-how-to-make-default-values-sound-frightening</link>
		<comments>http://kushalm.com/the-null-coalescing-operator-or-how-to-make-default-values-sound-frightening#comments</comments>
		<pubDate>Fri, 15 Jun 2007 21:11:11 +0000</pubDate>
		<dc:creator>kushal</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kushalm.com/the-null-coalescing-operator-or-how-to-make-default-values-sound-frightening</guid>
		<description><![CDATA[C# C# 2.0 introduced a little known, and somewhat useful new operator called the Null Coalescing Operator. Its like the ternary conditional operator, except less powerful (but admittedly a little neater to look at). Here&#8217;s an example of coaless coolesc that new feature: //assuming formValue is of type string string nickName = formValue ?? &#34;Dr. [...]]]></description>
			<content:encoded><![CDATA[<h3>C#</h3>
<p>C# 2.0 introduced a little known, and somewhat useful new operator called the Null Coalescing Operator.<br/><br />
Its like the <a href="http://msdn2.microsoft.com/en-us/library/ty67wk28(VS.80).aspx">ternary conditional operator</a>, except less powerful (but admittedly a little neater to look at). <br/>Here&#8217;s an example of <strike>coaless</strike> <strike>coolesc</strike> that new feature:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//assuming formValue is of type string</span>
<span style="color: #FF0000;">string</span> nickName <span style="color: #008000;">=</span> formValue <span style="color: #008000;">??</span> <span style="color: #666666;">&quot;Dr. Zoidberg&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>&#8230; which is the same as this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> nickName <span style="color: #008000;">=</span> 
        <span style="color: #000000;">&#40;</span>formValue <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;Dr. Zoidberg&quot;</span> <span style="color: #008000;">:</span> formValue<span style="color: #008000;">;</span></pre></div></div>

<p>Its just easiest to think of it as the &#8216;default&#8217; operator. i.e.<br />
<span class="km_code">nickName</span> is being set to <span class="km_code">formValue</span>, but with a default.</p>
<p>Note however, that if you try to change this code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> nickName <span style="color: #008000;">=</span> 
        <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>formValue<span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;Dr. Zoidberg&quot;</span> <span style="color: #008000;">:</span> formValue<span style="color: #008000;">;</span></pre></div></div>

<p>&#8230; to sprinkle some freshly-made coalescing goodness, you could be introducing a subtle bug. (Think empty string)</p>
<h3>SQL</h3>
<p>I&#8217;ve never quite understood why people have to come up with the most intimidatory name possible for a simple feature.<br />
Maybe the C# developers wanted to stress similarity with the <a href="http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt">ANSI SQL</a> function which pretty much does the same thing:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> COALESCE<span style="color: #66cc66;">&#40;</span>@nickaname<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Dr. Zoidberg'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>&#8230; in which case I can somewhat understand. After all, the SQL guys had to spend their time dealing mostly with simplistic sounding keywords like SELECT, CREATE, UPDATE etc &#8230; and some guy probably just snapped. Lawyers have their indictments, plaintiffs, subpoenas and what-not. Doctors regularly get to say words like haemoglobin, pericardium and streptokinase. So someone must have looked up the <a href="http://dictionary.reference.com/browse/coalesce">dictionary</a> and come up a random word.</p>
<h3>Javascript</h3>
<p>Interestingly enough, even though this feature isn&#8217;t supported by Java (as of <a href="http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html">Java 5</a>), Javascript has long supported this. Of course Javascript really has nothing to do with Java. But its hard not to form an association in one&#8217;s <a href="http://en.wikipedia.org/wiki/Transderivational_search">head</a>.<br />
Anyway, here&#8217;s the equivalent in Javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> nickName <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>formValue <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;Dr Zoidberg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>While on the topic of Javascript and null coalescence, beware though. Don&#8217;t get confused with this Javascript statement:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> returnValue <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>myObject <span style="color: #339933;">&amp;&amp;</span> myObject.<span style="color: #660066;">myProperty</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;which is called the &#8220;Guard&#8221; operator apparently. You would use this when you really want to return <span class="km_code">myObject.myProperty</span>, but you aren&#8217;t sure if <span class="km_code">myObject</span> is null or not, and want to avoid a null pointer error<sup><a href="#fn1">1</a></sup>. Kinda hacky, I know.</p>
<p>If you&#8217;re wondering how come all this doesn&#8217;t conflict with Javacript&#8217;s implementation of the logical <a href="http://msdn.microsoft.com/library/en-us/script56/html/95295331-6269-4311-8391-dc1c68e116ab.asp">OR</a> and <a href="http://msdn.microsoft.com/library/en-us/script56/html/4714dea9-1999-444a-8acd-72f0851e4f65.asp">AND</a> operators, its because they dont necessarily return booleans and Javascript evaluates all objects, non-empty strings and non-zero numbers to <span class="km_code">true</span>. So both the &#8220;guard&#8221; and &#8220;default&#8221; operators are really Javascript&#8217;s own peculiar implementation of logical AND and OR operators.<br />
Javascript often strikes me as the Ferris Bueller of programming languages. Not always taken seriously, but still surprisingly inventive and most of all &#8211; very, very annoying.</p>
<div class="km_footnotes">
<div class="km_footnote">
<a name="fn1">1</a>: The specific error message varies from browser to browser. In IE this would show up as &#8220;myProperty is null or not an object&#8221;, in Mozilla based browsers the error message would be &#8220;myObject has no properties&#8221; (which makes a little more sense, no?)
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://kushalm.com/the-null-coalescing-operator-or-how-to-make-default-values-sound-frightening/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

