<?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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Chip's Tips for Developers » Java</title>
	
	<link>http://www.chipstips.com</link>
	<description>Contains coding, but not narcotic.</description>
	<lastBuildDate>Mon, 04 Feb 2013 20:50:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ChipsTipsJava" /><feedburner:info uri="chipstipsjava" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>PHP messiness: zero isn’t false, or is it?</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsJava/~3/tYa_aIKny9Q/</link>
		<comments>http://www.chipstips.com/?p=168#comments</comments>
		<pubDate>Tue, 18 Sep 2007 20:02:14 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SynergyDE]]></category>
		<category><![CDATA[Wildly popular]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[false]]></category>
		<category><![CDATA[garyhart]]></category>
		<category><![CDATA[zero]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=168</guid>
		<description><![CDATA[Several years ago, Gary Hart said something that has stuck with me ever since: &#8220;just because it&#8217;s messy doesn&#8217;t mean you don&#8217;t want to do it.&#8221;&#160; We were discussing plans for driving throughout the&#160;UK&#160;while there on business &#8212; but time and again I find this principle applies to other situations as well.&#160; Besides the obvious, [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=168"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=168" data-text="PHP messiness: zero isn&#8217;t false, or is it?" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D168&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=168";
			reddit_title = "PHP messiness: zero isn&#8217;t false, or is it?";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=168";</script>
		<script type="text/javascript">var dzone_title = "PHP messiness: zero isn&#8217;t false, or is it?";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>Several years ago, <a href="http://www.garyhartphotography.com/">Gary Hart</a> said something that has stuck with me ever since: &#8220;just because it&#8217;s messy doesn&#8217;t mean you don&#8217;t want to do it.&#8221;&nbsp; We were discussing plans for driving throughout the&nbsp;UK&nbsp;while there on business &#8212; but time and again I find this principle applies to other situations as well.&nbsp; Besides the obvious, um,&nbsp;recreational possibilities, I can think of many more: eating sloppy sandwiches, parenting, electing public officials, starting a relationship, and coding in&nbsp;PHP.</p>
<p>PHP powers a lot of the web, including such popular frameworks as <a href="http://wordpress.org">WordPress</a>, so you really need to have it in your programming portfolio.&nbsp; But it&#8217;s a messy language.&nbsp; &nbsp;I&#8217;m not just talking about the way that PHP tends to sprawl like semicolon-laden vines around your HTML, earning&nbsp;an alternative interpretation of &#8220;Page Hacked to Pasta&#8221;.&nbsp; Nor am I referring (this time) to&nbsp;its <a href="http://sob.apotheon.org/?p=224">sucky object model</a>.&nbsp; The language itself contains many little gotchas and &#8220;almost right&#8221;s that wait like&nbsp;snares for the feet of the unwary coder.</p>
<p>One <a href="http://www.chipstips.com/?p=167#comment-20532">I ran into yesterday</a> (not for the first time) is&nbsp;PHP&#8217;s partial distinction between false and zero.&nbsp; In PHP, false is boolean, and zero is numeric.&nbsp; Fine and good.&nbsp; But if you treat a zero value as a boolean expression, the zero gets <em>converted </em>to a boolean false.</p>
<p>This seems innocent and helpful enough until you get to a function like <a href="http://www.php.net/manual/en/function.strpos.php">strpos</a>, which returns the position of one string within another.&nbsp; If the target is not found in the source, it returns boolean false.&nbsp; So, to make sure that one string doesn&#8217;t&nbsp;contain another, you&#8217;d think you&#8217;d&nbsp;do something like this:</p>
<p><code>if (!strpos($source, $target))</code></p>
<p>But there&#8217;s a problem here.&nbsp; If $target starts at the beginning of $source, strpos will return 0.&nbsp; Because it&#8217;s in a conditional, that gets converted to boolean false, and the test passes.&nbsp; So, you need to explicitly test for false instead, right?</p>
<p><code>if (strpos($source, $target) == false)</code></p>
<p>Wrong.&nbsp; Because you&#8217;re comparing it against a boolean value, the zero still gets converted to a boolean false.&nbsp; The only right way to do this is:</p>
<p><code>if (strpos($source, $target) === false)</code></p>
<p>The&nbsp;triple&nbsp;equal&nbsp;sign tells PHP to&nbsp;only evaluate to true if both operands have the same type <em>and </em>the same value, so no conversion takes place.&nbsp; Granted, the PHP docs have a great big warning on <a href="http://www.php.net/manual/en/function.strpos.php">strpos</a> to this effect, but the&nbsp;net result is something less than intuitive coding.</p>
<p>Languages like Java and C#&nbsp;solve this problem by not allowing automatic conversion between numeric and boolean values.&nbsp; That&#8217;s sort of like castrating the entire population to prevent birth defects.&nbsp; So why doesn&#8217;t this create problems for other languages?</p>
<p>In Synergy/DE, indices start at 1, so the instr function returns zero for not found:</p>
<p><code>if (!instr(1,source, target))</code></p>
<p>Quite logical and linguistically elegant (except for having the starting index as the first parameter), but then you don&#8217;t get to have the <a href="http://www.panopticoncentral.net/archive/2004/03/17/290.aspx">secret handshake</a>.</p>
<p>Even though Ruby uses a starting index of 0, it prevents confusion in routines like String#index by returning nil instead of a number for not found.&nbsp; Zero is treated as an object &#8212; and thus is <em>not</em> false &#8212; while nil <em>does</em> evaluate to false.&nbsp; Thus,</p>
<p><code>print "good" if ("abcde".index("a"))<br />print "bad" if ("abcde".index("f"))</code></p>
<p>prints &#8220;good&#8221;, even though the index of &#8220;a&#8221; within &#8220;abcde&#8221; is 0.&nbsp; Why should 0 be false anyway?</p>
<p>In C and C++, indices start at 0, and 0 can be used as a stand-in for false (in fact, all falses <em>are</em> zero), yet I hardly ever make the mistake of &#8220;if (strpos(&#8230;))&#8221; in C.&nbsp; Why?&nbsp; Because the <a href="http://www.ai.mit.edu/courses/6.863/doc/cportlib.html#SEC41">documented</a> return value for &#8220;target not found&#8221; isn&#8217;t FALSE, it&#8217;s -1.&nbsp; I&nbsp;immediately know that to test for this return value I must say:</p>
<p><code>if (strpos(source, target) &lt; 0)</code></p>
<p>The significance of the result does not rely on a distinction between returned values that can be automatically converted from one to the other.&nbsp; PHP, on the other hand, requires the programmer to explicitly prevent that conversion from happening.&nbsp; Automatic conversions are supposed to be a convenience for the programmer, not a trap.</p>
<p>Maybe PHP stands for Purposely Hampers Programming.</p>
<img src="http://feeds.feedburner.com/~r/ChipsTipsJava/~4/tYa_aIKny9Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=168</wfw:commentRss>
		<slash:comments>52</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=168</feedburner:origLink></item>
	</channel>
</rss>
