<?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>Mockery Blog &#187; operators</title>
	<atom:link href="http://getmockery.com/blog/archives/tag/operators/feed" rel="self" type="application/rss+xml" />
	<link>http://getmockery.com/blog</link>
	<description>GetMockery.com company blog - Mockery, UI, and more.</description>
	<lastBuildDate>Fri, 16 Dec 2011 19:48:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>An operator from Groovy that I wish was in JavaScript</title>
		<link>http://getmockery.com/blog/archives/60</link>
		<comments>http://getmockery.com/blog/archives/60#comments</comments>
		<pubDate>Thu, 22 Oct 2009 23:23:05 +0000</pubDate>
		<dc:creator>Joel Anair</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[operators]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://getmockery.com/blog/?p=60</guid>
		<description><![CDATA[First off, keep in mind that I&#8217;ve never used Groovy, just skimmed a couple of language references, but people I know have used it and like it. In case anyone is not familiar, Groovy is a dynamic language implemented on the JavaVM. It&#8217;s commonly used for web development, especially with the Grails framework.
Anyway, having established [...]]]></description>
			<content:encoded><![CDATA[<p>First off, keep in mind that I&#8217;ve never used Groovy, just skimmed a couple of language references, but people I know have used it and like it. In case anyone is not familiar, <a title="Groovy's Wikipedia entry" href="http://en.wikipedia.org/wiki/Groovy_%28programming_language%29">Groovy</a> is a dynamic language implemented on the JavaVM. It&#8217;s commonly used for web development, especially with the Grails framework.</p>
<p>Anyway, having established that I don&#8217;t know much about Groovy, I do know that it has a feature that I really wish we had in JavaScript. It&#8217;s called the <strong>Safe Navigation Operator</strong>, and it&#8217;s represented as ?. in the code. Quoting <a title="The Groovy Reference" href="http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29">the Groovy reference</a>:</p>
<blockquote><p>The Safe Navigation operator is used to avoid a NullPointerException. Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator will simply return <tt>null</tt> instead of throwing an exception[.]</p></blockquote>
<p>If it&#8217;s not clear from that, here&#8217;s a real-world example. Let&#8217;s say you have a <strong>customer </strong>object. That object has an optional attribute <strong>address</strong>, which is itself an object, and in turn has <strong>city</strong>, <strong>state</strong>, and <strong>postal code</strong> attributes, which are strings. In JavaScript, you could access the State like this:</p>
<pre name="code" class="js">var state = customer.state.address;</pre>
<p>No problem, but what if the customer object doesn&#8217;t have an address? That line will throw an exception. We can work around it like this:</p>
<pre name="code" class="js">var customerState= customer.state ? customer.state.address : null;</pre>
<p>But if we get deeply nested objects, the syntax gets really muddy. Using Groovy&#8217;s Safe Navigation Operator, we could just do this:</p>
<pre name="code" class="js">var customerState= customer?.state?.address;</pre>
<p>Now, if <strong>customer</strong> is defined, and <strong>state</strong> is defined, and <strong>address</strong> is defined, then <strong>customerState</strong> has the customer&#8217;s state. Otherwise, it contains null. Either way, it never throws an exception, even if none of those objects are defined.</p>
<p>Cases where this could come in handy are data retrieved from deep XML hierarchies, complex nested JSON objects returned by web services, and unpredictable user-entered data.  I&#8217;m sure that if JavaScript supported a similar operator I would use it all the time. Kudos to Groovy&#8217;s designer, Guillaume Laforge.</p>
]]></content:encoded>
			<wfw:commentRss>http://getmockery.com/blog/archives/60/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

