<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<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/"
	>

<channel>
	<title>Field Expert</title>
	<link>http://www.fieldexpert.com</link>
	<description>Field Expert Training &#038; Consulting</description>
	<pubDate>Wed, 30 Jul 2008 22:45:22 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Java Tip: Testing String Equality</title>
		<link>http://www.fieldexpert.com/2008/03/12/java-tip-testing-string-equality/</link>
		<comments>http://www.fieldexpert.com/2008/03/12/java-tip-testing-string-equality/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 01:59:50 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Journal]]></category>

		<guid isPermaLink="false">http://www.fieldexpert.com/2008/03/12/java-tip-testing-string-equality/</guid>
		<description><![CDATA[This article is part of an ongoing series entitled Java Tips: Writing Better Code.
Summary: This tip will show you a better way of comparing Java Strings with literal values.
Java Strings are object types rather than primitive types.  This means that when you test the value of a String in Java you should use the [...]]]></description>
			<content:encoded><![CDATA[<p>This article is part of an ongoing series entitled Java Tips: Writing Better Code.</p>
<p><strong>Summary:</strong> This tip will show you a better way of comparing Java Strings with literal values.</p>
<p>Java Strings are object types rather than primitive types.  This means that when you test the value of a String in Java you should use the <code>.equals()</code> method rather than the <code>==</code> operator.  Here&#8217;s an example that determines what Pennsylvanians eat for lunch:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StringEquality
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> city = <span style="color: #ff0000;">&quot;Pittsburgh&quot;</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>city.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Pittsburgh&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Your sandwiches contain french fries.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>city.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Philadelphia&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Your sandwiches contain steak and cheese.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I have no idea what you like to eat.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<pre class="console">$ java StringEquality
Your sandwiches contain french fries.</pre>
<p>
This code works just fine most of the time.  However, what would happen if the <code>city</code> variable contained a null value?  (Note: In this code example the city variable was hard coded to be null for simplicity&#8217;s sake.  I&#8217;ll leave it to your good imagination to picture a real world scenario where a value might be null.)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StringEquality
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> city = <span style="color: #000000; font-weight: bold;">null</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>city.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Pittsburgh&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Your sandwiches contain french fries.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>city.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Philadelphia&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Your sandwiches contain steak and cheese.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I have no idea what you like to eat.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<pre class="console">$ java StringEquality
Exception in thread "main" java.lang.NullPointerException
	at StringEquality.main(StringEquality.java:10)</pre>
<p>The problem with this code is that it is attempting to invoke a method on a null object reference.  So, what&#8217;s a developer to do?  Well, let&#8217;s start with a peek at what <emp>not</emp> to do.  I was recently working with a not-to-be-named 3rd party&#8217;s code and came across null checks that looked something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StringEquality
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> city = <span style="color: #000000; font-weight: bold;">null</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>city != <span style="color: #000000; font-weight: bold;">null</span> &amp;&amp; city.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Pittsburgh&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Your sandwiches contain french fries.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>city != <span style="color: #000000; font-weight: bold;">null</span> &amp;&amp; city.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Philadelphia&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Your sandwiches contain steak and cheese.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I have no idea what you like to eat.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Although this approach certainly works, it&#8217;s very unwieldy and inefficient to perform a null check every time you test a String&#8217;s value.  The simple solution to avoid null checks is to flip the order of the String variable and the String literal so that <code>city.equals("Pittsburgh")</code> becomes <code>"Pittsburgh".equals(city)</code>.  With this approach the equals method is invoked on the String literal rather than the String variable.  Since String literals can&#8217;t be null by definition, you&#8217;ll never have to worry about NullPointerExceptions on String comparisons again</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StringEquality
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> city = <span style="color: #000000; font-weight: bold;">null</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Pittsburgh&quot;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>city<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Your sandwiches contain french fries.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Philadelphia&quot;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>city<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Your sandwiches contain steak and cheese.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I have no idea what you like to eat.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<pre class="console">$ java StringEquality
I have no idea what you like to eat.</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.fieldexpert.com/2008/03/12/java-tip-testing-string-equality/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Ajax Best Practices: Don&#8217;t Break Bookmarks</title>
		<link>http://www.fieldexpert.com/2006/01/04/ajax-best-practices-dont-break-bookmarks/</link>
		<comments>http://www.fieldexpert.com/2006/01/04/ajax-best-practices-dont-break-bookmarks/#comments</comments>
		<pubDate>Wed, 04 Jan 2006 21:43:38 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Journal]]></category>

		<guid isPermaLink="false">http://www.fieldexpert.com/2006/01/04/ajax-best-practices-dont-break-bookmarks/</guid>
		<description><![CDATA[This is the second installment of a multi-part series aiming to address Ajax criticism and show how using best practices is vital to making Ajax worth the while.
The Problem
In my previous post, Don&#8217;t Break The Back Button, I explained the difference between URL changing &#8220;horizontal&#8221; links and Ajax&#8217;s non-URL changing &#8220;vertical&#8221; links.  If you [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second installment of a <a href="http://www.fieldexpert.com/ajax-best-practices">multi-part series</a> aiming to address Ajax criticism and show how using best practices is vital to making Ajax worth the while.</p>
<h4>The Problem</h4>
<p>In my previous post, <a href="http://www.fieldexpert.com/2006/01/03/ajax-best-practices-dont-break-back/">Don&#8217;t Break The Back Button</a>, I explained the difference between URL changing &#8220;horizontal&#8221; links and Ajax&#8217;s non-URL changing &#8220;vertical&#8221; links.  If you haven&#8217;t already read that post, I encourage you to do so before reading this one.</p>
<p>In addition to potentially breaking the back button, Ajax&#8217;s vertical links can ruin a browser&#8217;s ability to link to the current resource.  This is very similar to the way in which HTTP POST requests can&#8217;t be bookmarked in the same way as HTTP GET requests since they don&#8217;t include any query data in the URL.</p>
<p>Not only will this problem frustrate users who are interested in coming back to your web site; it will also make it difficult or impossible for search engines to properly link to your second-tier resources. <a href="http://www.fieldexpert.com/2006/01/04/ajax-best-practices-dont-break-bookmarks/#more-17" class="more-link">(more&#8230;)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.fieldexpert.com/2006/01/04/ajax-best-practices-dont-break-bookmarks/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Ajax Best Practices: Don&#8217;t Break The Back Button</title>
		<link>http://www.fieldexpert.com/2006/01/03/ajax-best-practices-dont-break-back/</link>
		<comments>http://www.fieldexpert.com/2006/01/03/ajax-best-practices-dont-break-back/#comments</comments>
		<pubDate>Tue, 03 Jan 2006 18:52:45 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Journal]]></category>

		<guid isPermaLink="false">http://www.fieldexpert.com/?p=16</guid>
		<description><![CDATA[This is the first installment of a multi-part series aiming to address Ajax criticism and show how using best practices is vital to making Ajax worth the while.
The Problem
The foundation of the web has been the hyperlink from one page to another.  The links between pages have been primarily bi-directional, in the sense that [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first installment of a <a href="http://www.fieldexpert.com/ajax-best-practices">multi-part series</a> aiming to address Ajax criticism and show how using best practices is vital to making Ajax worth the while.</p>
<h4>The Problem</h4>
<p>The foundation of the web has been the hyperlink from one page to another.  The links between pages have been primarily bi-directional, in the sense that visiting page A and then clicking on a link to page B would in no way prevent the user from either following a link back to page A or clicking the back button to get there.   <a href="http://www.fieldexpert.com/2006/01/03/ajax-best-practices-dont-break-back/#more-16" class="more-link">(more&#8230;)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.fieldexpert.com/2006/01/03/ajax-best-practices-dont-break-back/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
