<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>kushalm.com</title>
	
	<link>http://kushalm.com</link>
	<description />
	<lastBuildDate>Mon, 09 Jul 2007 12:33:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/KushalM" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="kushalm" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Internet Explorer Security Zone Manager</title>
		<link>http://kushalm.com/internet-explorer-security-zone-manager</link>
		<comments>http://kushalm.com/internet-explorer-security-zone-manager#comments</comments>
		<pubDate>Mon, 09 Jul 2007 12:33:07 +0000</pubDate>
		<dc:creator>kushal</dc:creator>
				<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://kushalm.com/internet-explorer-security-zone-manager</guid>
		<description><![CDATA[If you ever wanted to see a fantastic example of a bad UI, have a look at IE&#8217;s Securty Zone management dialog. (Accessed via Tools->Internet Options -> Security(tab), then click on a Security Zone (like Restricted sites) -> Sites button.
This is especially annoying if you use Spybot &#8211; Search &#038; Destroy which shoves in ever [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever wanted to see a fantastic example of a bad UI, have a look at IE&#8217;s Securty Zone management dialog. (Accessed via Tools->Internet Options -> Security(tab), then click on a Security Zone (like Restricted sites) -> Sites button.</p>
<p>This is especially annoying if you use <a href="http://www.safer-networking.org/en/spybotsd/index.html">Spybot &#8211; Search &#038; Destroy</a> which shoves in ever increasing hoards of scum into this bottomless pit of doom.<br />
That of course, is Spybot S &#038; D&#8217;s job. Trouble is, the IE dialog wasn&#8217;t designed for this. (From the looks of things, the IE developers didn&#8217;t expect you to add any more than &#8230; say 10 sites to this list)<br />
The dialog isn&#8217;t resizable, (Yes, no matter how long and how hard you hover your mouse at the bottom right hand corner, the resize icon just will not appear)<br />
the scrolling area is frustratingly tiny.</p>
<p>See for yourself:</p>
<p><img src='/wp-content/uploads/2007/07/ie-security-zone-dialog.png' alt='Internet Explorer Security Zone Manager' height="342" width="384"/></p>
<p>So if you happen to accidentally add a legitimate site in this list, good luck pulling it out of there &#8211; you&#8217;re doomed to scrolling around the thing for days.<br />
Thats where a tool like <a href="http://www.funkytoad.com/content/view/15/33/">ZonedOut</a> from the boys at FunkyToad comes in really handy. Its nothing really fancy, just useful. Not only is it easier to use than IE&#8217;s own dialog, it also comes with a search function.</p>
<p>Incidentally <a href="http://www.google.com/search?hl=en&#038;q=Internet+Explorer+Security+Zone+Manager">googling</a> for such a tool seems to bring you to some degenerate crowd who actually charge for the same sort of tool.</p>
]]></content:encoded>
			<wfw:commentRss>http://kushalm.com/internet-explorer-security-zone-manager/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Perils of XPath Expressions (Specifically, Escaping Quotes)</title>
		<link>http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes</link>
		<comments>http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes#comments</comments>
		<pubDate>Thu, 28 Jun 2007 22:26:15 +0000</pubDate>
		<dc:creator>kushal</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes</guid>
		<description><![CDATA[Escaping a single/double quote in an XPath expression such as this: "books/book[@publisher = 'publisher name here']";]]></description>
			<content:encoded><![CDATA[<p><P><br />
The other day, I was grappling with a particularly irritating problem with XPaths. I was using <a href="http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.selectsinglenode.aspx">SelectSingleNode</a> to dig some info out of an XML document.<br />
</P></p>
<h3>The problem:</h3>
<p>&#8230; was simple. Escaping a single/double quote in an XPath expression such as this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> myXPathExpression <span style="color: #008000;">=</span>
    <span style="color: #666666;">&quot;books/book[@publisher = 'publisher name here']&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>If the publisher name were to have an apostrophe in it (e.g. <span class="km_code">O&apos; Reilly</span>) I&#8217;d be in trouble.</p>
<h3>Lazy Hack #1:</h3>
<p>The simple, straightforward solution would be the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> myXPathExpression <span style="color: #008000;">=</span>
    <span style="color: #666666;">&quot;books/book[@publisher = <span style="color: #008080; font-weight: bold;">\&quot;</span>O'Reilly<span style="color: #008080; font-weight: bold;">\&quot;</span>]&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>&#8230; i.e. enclose the <a href="http://www.w3.org/TR/xpath#NT-PredicateExpr">PredicateExpr</a> in double quotes instead of single quotes.<br />
But of course as is often the case, words like &quot;simple&quot; and &quot;straightforward&quot; are merely a replacement for words like &quot;short-sighted&quot;.<br />
<br />
The problem with that solution of course, was what if that blasted <span class="km_code">publisher name</span> had a double quote in it?<br />
Would I go back to enclosing it in single quotes? What if it had both? What if I simply didn&#8217;t know, and I was building up the string like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> myXPathExpression <span style="color: #008000;">=</span>
    <span style="color: #666666;">&quot;books/book[@publisher = '&quot;</span> <span style="color: #008000;">+</span> publisherName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;']&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>.. assuming <span class="km_code">publisherName</span> was a user-entered string I had no control over. (which was in fact, the case)</p>
<h3>Lazy Hack #2:</h3>
<p>I could of course, wimp out and prevent the user from entering double or single quotes (or worse, both). I could even rationalise it by pretending this was really because I was thinking of the &quot;bigger picture&quot; and that resources and time aren&#8217;t really worth fixing this issue. But I decided not to. Mostly because its irritating enough listening to pseudo-managerial-cop-out-speak when it isn&#8217;t coming from me; I really didn&#8217;t need to add to it.</p>
<h3>Wrong Solution <strike>Lazy Hack</strike> #3:</h3>
<p>My first thought was that I should replace single quotes with &amp;apos; (or its hex equivalent &amp;#39;) and double quotes with &amp;quot; (or &amp;#34;) according to the XML 1.0 <a href="http://www.w3.org/TR/2006/REC-xml-20060816/#syntax">markup rules</a>. That should have worked right?<br />
<br />
But apparently that isnt the case. Even though the guys at W3C <a href="http://www.w3.org/TR/xpath">recommend</a> it.<br />
<br />
It turns out that I didn&#8217;t need to escape any of the standard XML <a href="http://www.w3.org/TR/2006/REC-xml-20060816/#dt-entref">entities</a><sup><a href="#fn1-28Jun07">1</a></sup> in my XPath query at all. (Even though I positively <I>do</I> need to do this in my XML markup)<br />
<br />
So not only is this a valid XPath expression:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> myXPathExpression <span style="color: #008000;">=</span>
    <span style="color: #666666;">&quot;tvshows/tvshow[@name = 'Starsky &amp; Hutch']&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//no need to use &amp;amp; in place of ampersand.</span></pre></div></div>

<p>
&#8230; but also this would <I>not</I> return the result I would expect:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> myXPathExpression <span style="color: #008000;">=</span>
    <span style="color: #666666;">&quot;tvshows/tvshow[@name = 'Starsky &amp;amp; Hutch']&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">// this will *not* return the tvshow node with an attribute</span>
    <span style="color: #008080; font-style: italic;">//called &quot;Starsy &amp; Hutch&quot;</span></pre></div></div>

<h3>Solution:</h3>
<p>It turned out the only solution was to use the <a href="http://www.w3.org/TR/xpath#function-concat">concat function</a> defined in the W3C XPath recommendation.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> myXPathExpression <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;books/book[@publisher = &quot;</span> <span style="color: #008000;">+</span>
   <span style="color: #666666;">&quot;concat('Single', &quot;</span><span style="color: #666666;">'&quot;, '</span>quote. <span style="color: #FF0000;">Double</span><span style="color: #666666;">', '</span><span style="color: #666666;">&quot;', 'quote.')]&quot;</span><span style="color: #008000;">;</span>
   <span style="color: #008080; font-style: italic;">//looks for a publisher called Single'quote. Double&quot;quote</span></pre></div></div>

<p>i.e. break up my search string around single and double quotes, and concatenate all the bits using this concat function (it takes a variable number of string arguments) &#8211; thereby enclosing the single quotes in double quotes, and the double quotes in single quotes.<br />
<br />
Pretty crazy, huh? BTW, this is true in .Net, Java<sup><a href="#fn2-28Jun07">2</a></sup>, Mozilla&#8217;s implementation of XPaths, as well as Internet Explorer&#8217;s. (In IE, you would be using the MSXML parser. More on this below).<br />
<br />
So, since I was building up a string like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> myXPathExpression <span style="color: #008000;">=</span>
    <span style="color: #666666;">&quot;books/book[@publisher = '&quot;</span> <span style="color: #008000;">+</span> publisherNameHere <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;']&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>I had no alternative but to write a method that would generate the required concat function call for me. i.e.:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> myXPathExpression <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;books/book&quot;</span> <span style="color: #008000;">+</span>
  <span style="color: #666666;">&quot;[@publisher = &quot;</span> <span style="color: #008000;">+</span> GenerateConcatForXPath<span style="color: #000000;">&#40;</span>publisherNameHere<span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;]&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Here is the method written in C#. </p>
<div class="km_collapsible">
    <a name="GenerateConcatForXPath" href="#GenerateConcatForXPath" onclick="km_collapse(this);return false;" title="GenerateConcatForXPath" border="0"><img src="/images/plus.gif" height="19" width="20" border="0"></img>GenerateConcatForXPath</a>
<div class="km_collapsible_content">

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//you may want to use constants like HtmlTextWriter.SingleQuoteChar and</span>
<span style="color: #008080; font-style: italic;">//HtmlTextWriter.DoubleQuoteChar intead of strings like &quot;'&quot; and &quot;\&quot;&quot;</span>
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> GenerateConcatForXPath<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> a_xPathQueryString<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">string</span> returnString <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">string</span> searchString <span style="color: #008000;">=</span> a_xPathQueryString<span style="color: #008000;">;</span>
    <span style="color: #FF0000;">char</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> quoteChars <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">char</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\'</span>'</span>, <span style="color: #666666;">'&quot;'</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #FF0000;">int</span> quotePos <span style="color: #008000;">=</span> searchString.<span style="color: #0000FF;">IndexOfAny</span><span style="color: #000000;">&#40;</span>quoteChars<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>quotePos <span style="color: #008000;">==</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        returnString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;'&quot;</span> <span style="color: #008000;">+</span> searchString <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;'&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">else</span>
    <span style="color: #000000;">&#123;</span>
        returnString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;concat(&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>quotePos <span style="color: #008000;">!=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> subString <span style="color: #008000;">=</span> searchString.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, quotePos<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            returnString <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot;'&quot;</span> <span style="color: #008000;">+</span> subString <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;', &quot;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>searchString.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>quotePos, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;'&quot;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                returnString <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>'<span style="color: #008080; font-weight: bold;">\&quot;</span>, &quot;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//must be a double quote</span>
                returnString <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot;'<span style="color: #008080; font-weight: bold;">\&quot;</span>', &quot;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            searchString <span style="color: #008000;">=</span> searchString.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>quotePos <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span>,
                             searchString.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> quotePos <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            quotePos <span style="color: #008000;">=</span> searchString.<span style="color: #0000FF;">IndexOfAny</span><span style="color: #000000;">&#40;</span>quoteChars<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        returnString <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot;'&quot;</span> <span style="color: #008000;">+</span> searchString <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;')&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> returnString<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</div>
</div>
<h3>The Exception (there&#8217;s always one):</h3>
<p>Microsoft&#8217;s <a href="http://msdn2.microsoft.com/en-us/library/ms763742.aspx">MSXML</a> parser (the COM implementation, not the .Net one &#8211; and they <I>are</I> different) is still widely in use. Mostly in Visual Studio 6 based apps (like VB6), on apps with client-side XML processing done on IE, and those glorified batch files written in <a href="http://msdn2.microsoft.com/en-us/library/ms950396.aspx">Windows Scripting Host</a>. Also, there are probably more than a few .Net apps using MSXML via the COM Interop Services.<br />
<br />
This problem of escaping quotes exists for MSXML too of course, and the solution is the same &#8211; but only for MSXML4 and later. For versions 3 and before, you would have to escape single and double quotes with C-style backslashes.<br />
This naturally also means that you would have to escape backslashes themselves with two backslashes &#8211; something you need to be aware of if you are porting your application from MSXML 1, 2 or 3 to anything later than that.</p>
<p>Sigh! Sometimes I miss the old XPath-free days when shoot&#8217;em ups were still innovative, they actually ran on two megabytes of RAM, and no-one had heard of Paris Hilton. </p>
<div class="km_footnotes">
<div class="km_footnote">
<a name="fn1-28Jun07">1</a> Predefined XML Entities: &amp;, &lt;, &gt;, &quot; and &apos;
</div>
<div class="km_footnote">
<a name="fn2-28Jun07">2</a> XPaths in Java: I tested it using Apache&#8217;s <a href="http://xml.apache.org/xalan-j/">Xalan</a> XSLT Processor. And using the <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPath.html#compile(java.lang.String)">compile</a> method which of course adheres to Sun&#8217;s <a href="http://java.sun.com/webservices/jaxp/">JAXP</a> specification.</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<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. Zoidberg&#34;;

&#8230; which is the [...]]]></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>
		<item>
		<title>Are Pro Web Developers somewhat under-informed?</title>
		<link>http://kushalm.com/are-pro-web-developers-somewhat-um-under-informed</link>
		<comments>http://kushalm.com/are-pro-web-developers-somewhat-um-under-informed#comments</comments>
		<pubDate>Mon, 11 Jun 2007 20:59:30 +0000</pubDate>
		<dc:creator>kushal</dc:creator>
				<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://kushalm.com/?p=3</guid>
		<description><![CDATA[Everything he said seemed pretty basic to me. I mean anyone other than an absolute script/html kiddie should be making any of those mistakes.]]></description>
			<content:encoded><![CDATA[<p>I was over at a Microsoft User Group conference the other day and was pretty amazed at one of the presentations. It was entitled &#8220;Hacking websites for fun and profit&#8221;<br />
It was about (what seemed to me to be) pretty basic stuff. Things like:</p>
<ul>
<li>Don’t rely on javascript based client side validation.</li>
<li>Don’t rely on HTML based client side validation (things like the MAXLENGTH attribute on texboxes)</li>
<li>Avoid XSS attacks by HTMLEncoding your data when displaying them &#8211; especially if its user-entered.</li>
<li>Escape data that you will be concatenating in an SQL statement to avoid SQL injection attacks.</li>
<li>When it comes to data that is your own (e.g. price of a certain item in a shopping cart app) as opposed to data entered by the user (e.g. how many items the user wished to buy) don&#8217;t rely on form based or Querystring based data as these can be changed just by using a tool like Fiddler, or just saving the HTML locally and editing it.</li>
<li>Don’t rely on HTTP headers like <a href="http://tools.ietf.org/html/rfc2616#section-14.36" title="http://tools.ietf.org/html/rfc2616#section-14.36" target="_blank">Referer</a> (sic) for any kind of validation.</li>
</ul>
<p>I was hoping for somewhat more from the presentation. In fact, it would be fair to say I was quite disappointed. <strike>Almost</strike> Everything he said seemed pretty basic to me. I mean anyone other than an absolute script/html kiddie should be making any of those mistakes.<br />
Are paid professionals actually stupid enough to make such basic mistakes that it would warrant a whole presentation just on these fundamentals? (He was saying how he&#8217;s been making several presentations on the same subject all over the place. And this was a conference arranged for professionals .. certainly not a bunch of n00bs.) Most of the audience seemed impressed. Well certainly not as unimpressed as I was.<br />
So what’s going on?</p>
<p>I don’t mean to take anything away from the presenter <a href="http://idunno.org" target="_blank">Barry Dorrans</a>, though.</p>
<hr />
EDIT:<br />
BTW, if that sounds like I was saying that the presentation was ineffectual or a waste of time or something like that, its mostly because my writing skills are about as well developed as <a href="http://www.craigslist.org">Craig Newmark&#8217;s</a> aesthetic talent.<br />
I was merely amazed that so many folks exist who make their living writing applications for the web, and yet don’t know this stuff. But, because that <em>is </em>a sad fact, (and also because not everyone is <strike>amassing a vast fortune by</strike> writing web apps &#8230; yet) people ought to be paying more attention to what guys like <a href="http://idunno.org" target="_blank">Barry</a> have to say.</p>
]]></content:encoded>
			<wfw:commentRss>http://kushalm.com/are-pro-web-developers-somewhat-um-under-informed/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
