<?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>Coyote Tracks &#187; silly coding tricks</title>
	<atom:link href="http://kagan.mactane.org/blog/tag/silly-coding-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://kagan.mactane.org/blog</link>
	<description>The prints of an Internet-enabled coyote.</description>
	<lastBuildDate>Tue, 31 Jan 2012 03:26:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Beware of Optional Curly Braces&#160;&#8212; They Will Bite You</title>
		<link>http://kagan.mactane.org/blog/2012/01/30/beware-of-optional-curly-braces-they-will-bite-you/</link>
		<comments>http://kagan.mactane.org/blog/2012/01/30/beware-of-optional-curly-braces-they-will-bite-you/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 03:26:51 +0000</pubDate>
		<dc:creator>Kai MacTane</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bad ideas]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[silly coding tricks]]></category>

		<guid isPermaLink="false">http://kagan.mactane.org/blog/?p=441</guid>
		<description><![CDATA[I was looking through some PHP code from a third-party vendor recently, and saw something that made my jaw drop. It&#8217;s pretty innocent-looking, at first. Here&#8217;s a somewhat anonymized and genericized version of the code, but the thing that bothered me is still intact. It&#8217;s not really a bug, per&#160;se; the code will function as [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking through some PHP code from a third-party vendor recently, and saw something that made my jaw drop. It&#8217;s pretty innocent-looking, at first. Here&#8217;s a somewhat anonymized and genericized version of the code, but the thing that bothered me is still intact. It&#8217;s not really a bug, <i>per&nbsp;se</i>; the code will function as intended.&nbsp;But&#8230;</p>
<div class="code">$currentRow = 0;<br />
$itemId = &#8220;&#8221;;<br />
$index = 0;<br />
while ($row = mysql_fetch_object($result)) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($currentRow == 10) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;renderHeaderRow();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$currentRow = 0;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// takes an itemId and displays relevant columns<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;renderSummaryRow($row->itemId);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$currentRow++;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($index > 0)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$itemId .=  &#8220;,&#8221;;    # interpolate a comma<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$itemId .= $row->itemId;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$index++;<br />
}</p></div>
<p>See the problem? Really, there are a few ways this can go wrong. To a quick glance, the only clue that line&nbsp;14 (&#8220;interpolate a comma&#8221;) is part of a conditional is its indentation. The indentation is important to a human reader&nbsp;&mdash; but <strong>absolutely irrelevant</strong> to the PHP interpreter, which simply treats the next line after the conditional as the conditional&#8217;s block. Regardless of how it&#8217;s indented, and regardless of what else is&nbsp;around.</p>
<p><em>The way it looks to a human <strong>is not</strong> the way it looks to the&nbsp;machine.</em></p>
<p>What happens if someone wants to add some logging? What if they add it after the comma&nbsp;line?</p>
<div class="code">if ($index > 0)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$itemId .=  &#8220;,&#8221;;    # interpolate a comma<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeLog(&#8220;Added a comma&#8221;);<br />
$itemId .= $row->itemId;<br />
$index++;</div>
<p>Now the log claims the code has added a comma, even when it hasn&#8217;t. But still, it could be worse! What if you decided to add your logging <em>before</em> the other&nbsp;line?</p>
<div class="code">if ($index > 0)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeLog(&#8220;Adding a comma to itemId&#8230;&#8221;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$itemId .=  &#8220;,&#8221;;    # interpolate a comma<br />
$itemId .= $row->itemId;<br />
$index++;</div>
<p>Now it adds a comma no matter what&nbsp;&mdash; even the first time through the loop, when the string is empty. So instead of a string like <code>'123,124,125'</code>, $itemId will now have a leading comma: <code>',123,124,125'</code>. Since this value is getting stitched into a SQL query later on, it means your app will blow up with a SQL syntax&nbsp;error.</p>
<p>This is why Python makes whitespace significant to program flow. The way the indentation makes it <em>look like</em> the logical structure is, is how the structure <em>actually</em>&nbsp;is.</p>
<p>And this is also why Perl&nbsp;&mdash; of all languages, one that normally errs on the side of letting you leave out anything that can be inferred from context&nbsp;&mdash; <strong>Perl</strong> insists in its syntax documentation that <a href="http://perldoc.perl.org/perlsyn.html#Compound-Statements">in cases like this</a>, &#8220;the curly brackets are <em>required</em>&nbsp;&mdash; no dangling statements allowed.&#8221; (It then says, in typically Perlish fashion: &#8220;If you want to write conditionals without curly brackets there are several other ways to do it.&#8221;)</p>
<p>If you&#8217;re working in one of those languages that lets you omit curly braces around a single-statement conditional&nbsp;&mdash; <strong><em>DON&#8217;T DO IT!</em></strong> The potential maintenance and debugging problems are <em>not</em> worth the fun of saving two keystrokes (or just one, if you work in an editor that auto-closes your braces for&nbsp;you).</p>
]]></content:encoded>
			<wfw:commentRss>http://kagan.mactane.org/blog/2012/01/30/beware-of-optional-curly-braces-they-will-bite-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silly Coding Tricks: &#8220;Inverted&#8221; String Match</title>
		<link>http://kagan.mactane.org/blog/2010/02/13/silly-coding-tricks-inverted-string-match/</link>
		<comments>http://kagan.mactane.org/blog/2010/02/13/silly-coding-tricks-inverted-string-match/#comments</comments>
		<pubDate>Sat, 13 Feb 2010 23:20:19 +0000</pubDate>
		<dc:creator>Kai MacTane</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bad ideas]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[silly coding tricks]]></category>

		<guid isPermaLink="false">http://kai.mactane.org/blog/?p=187</guid>
		<description><![CDATA[First things first: Never actually do this. This is just a fun curiosity, for amusement value only. Because of the way JavaScript&#8217;s search() method works, you can do: var my_url = &apos;http://kai.mactane.org&apos;; if (! my_url.search(/http:\/\/kai.mactane.org/)) { &#160;&#160;&#160;&#160;alert(&#34;Your URL is http://kai.mactane.org&#34;); } else { &#160;&#160;&#160;&#160;alert(&#34;Nope, your URL isn&apos;t http://kai.mactane.org&#34;); } Try running this, and it will [...]]]></description>
			<content:encoded><![CDATA[<p>First things first: <strong>Never actually do this.</strong> This is just a fun curiosity, for amusement value only.</p>
<p>Because of the way JavaScript&#8217;s <code>search()</code> method works, you can do:</p>
<div class="code">var my_url = &apos;http://kai.mactane.org&apos;;<br />
if (! my_url.search(/http:\/\/kai.mactane.org/)) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Your URL is http://kai.mactane.org&quot;);<br />
} else {<br />
&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Nope, your URL isn&apos;t http://kai.mactane.org&quot;);<br />
}</div>
<p>Try running this, and it will correctly claim that &#8220;Your URL is http://kai.mactane.org&#8221;&nbsp;&mdash; even though there&#8217;s a ! at the beginning of the test. What gives?</p>
<p>That exclamation point <em>looks</em> like it&#8217;s saying: &#8220;If the search for <code>http://kai.mactane.org</code> within the given string fails&#8230;&#8221; But what that test is <em>really</em> saying is: &#8220;If the search for <code>http://kai.mactane.org</code> returns Boolean false&nbsp;&mdash; or <em>anything that evaluates as</em> Boolean false&#8230;&#8221;</p>
<p>What that <code>search()</code> call actually returns is <em>the character index</em> at which the needle is found, or -1 if it isn&#8217;t found. Since the needle is found at the very beginning of the haystack&nbsp;&mdash; at character index 0&nbsp;&mdash; <code>search()</code> returns zero. Which evaluates to false in a Boolean context.</p>
<p>If the needle isn&#8217;t found, the search will return -1&nbsp;&mdash; which <em>evaluates as true</em> in a Boolean context!</p>
<p>Effectively, the ! operator is reconciling the disagreement between <code>search()</code>&rsquo;s idea of falsehood and <code>if()</code>&rsquo;s idea of falsehood: <code>search()</code> returns -1 for a false result (a failed match), but <code>if()</code> considers -1 to be true, not false.</p>
<p>This trick only works with needles that you are sure will only occur at the beginning of the haystack.</p>
<p>Once again, you should <em>never, ever <strong>actually</strong> write code like this</em> for any serious purpose or in anything you intend to deploy in the real world. The maintainability problems are not worth the amusement of confusing all your co-workers with a test that looks reversed. Instead, use a positional anchor in your regex, and explicitly test against -1&#8230;</p>
<div class="code">if (my_url.search(/^http:\/\/kai.mactane.org) != -1)</div>
<p>&#8230;like any sensible JavaScript coder. (Yes, that last bit is intended to be ironic. I wrote something once about the silliness of having to add that extra test; I&#8217;ll have to see if I can find it and republish it here. )</p>
]]></content:encoded>
			<wfw:commentRss>http://kagan.mactane.org/blog/2010/02/13/silly-coding-tricks-inverted-string-match/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

