<?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>uxebu » blog &#187; style guide</title>
	<atom:link href="http://uxebu.com/tag/style-guide/feed/" rel="self" type="application/rss+xml" />
	<link>http://uxebu.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 19 Jan 2012 14:53:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>One var statement for one variable!</title>
		<link>http://uxebu.com/blog/2010/04/02/one-var-statement-for-one-variable/</link>
		<comments>http://uxebu.com/blog/2010/04/02/one-var-statement-for-one-variable/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 16:15:12 +0000</pubDate>
		<dc:creator>wolfram</dc:creator>
				<category><![CDATA[frontend engineering]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[style guide]]></category>
		<category><![CDATA[var statement]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1135</guid>
		<description><![CDATA[JavaScript allows to comma-separate multiple variable declarations, like so: 1var i=0, j=1 . Declaring multiple variables using one var-statement accross multiple lines is a NO GO! I consider this evil. And I learned it the hard way. It might look pretty nice, looks like less code and more efficient. But it definitely is not so [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript allows to comma-separate multiple variable declarations, like so:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var i=0, j=1</div></td></tr></tbody></table></div>
<p>. Declaring multiple variables using one var-statement accross multiple lines is a NO GO! I consider this evil. And I learned it the hard way. It might look pretty nice, looks like less code and more efficient. But it definitely is not so when writing code. If it results in more speed let your build tool, compressor or compiler do it. But don&#8217;t write code which spreads multiple variable declarations in var-statement across multiple lines!<br />
PERIOD.<br />
<span id="more-1135"></span></p>
<p>This article is quite long for such a simple topic, but anyways I just feel like writing it down, since this is how I initially used <a href="http://wolfram.kriesing.de/blog">my previous blog</a> and I must say it was very helpful for me to look up things again but obviously also for others to find information. I also feel that showing what coding guide lines to use and the reasoning behind it is really important especially also for those who have to apply them.</p>
<h2>Good and Bad</h2>
<p>Let me just quickly show examples for DOs and DON&#8217;Ts.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><br />
y <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><br />
z <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The above looks pretty slick. But not always! Especially when working in a team and when you will rewrite or touch the later code again, which basically is the case with all the useful code out there. The second and the third line depend on the first line. That means if a coworker comes along and pastes in his variable from his awesome and dead-simple code</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> y0 <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><br />
y1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>into my code above. We might get the following:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><br />
y <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><br />
y1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
z <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>And ouch, we have a global variable &#8220;z&#8221;. Of course he should change the trailing semicolon to a comma when pasting in his code. But his IM beeped, phone rang, coffee was done or he simply felt more like skiing and stopped hacking &#8211; and he forgot :). You know what I mean!<br />
If the code hadn&#8217;t contained multiline variable declarations it wouldn&#8217;t have happened, like here:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DO</span><br />
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> y1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> z <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Though there are cases when I still think that listing multiple variable declarations is ok, like the following. But just not on multiple lines!</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DO</span><br />
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> y <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> z <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> l<span style="color: #339933;">=</span>arr.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> k<span style="color: #339933;">;</span> i</div></td></tr></tbody></table></div>
<h2>Real Life Examples</h2>
<p>Let me show you three examples where I really felt the pain of the described problem, which finally triggered me to write this article. Those are real life examples, exactly the code I experienced the problem with. And at some point it was just enough.<br />
The first example shows that a pretty complex piece of code is just not as easy to validate by humans. There is no bug in it, it is just hard to read code.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;group.name&quot;</span><span style="color: #339933;">,</span> test<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> groupName<span style="color: #339933;">,</span><br />
<span style="color: #006600; font-style: italic;">// Trim and replace all special chars, to have a proper test ID.</span><br />
group <span style="color: #339933;">=</span> g.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^s*/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/s*$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^0-9a-zA-Z]/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<span style="color: #006600; font-style: italic;">// Replace multiple underscores with just one.</span><br />
ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ID_&quot;</span> <span style="color: #339933;">+</span> group<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/_+/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><br />
.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[_]+$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>You can see on line 3 no comma is needed, because I just split up the chained calls for better readability. This may mislead the one who wants to understand the code. On the next line though a comma has to end the line because it is the end of the variable declaration. Additionally the code above has a comment after each variable declaration, which makes it easy to read and can be understood faster. If that was not the case it would be much harder to read the code. Just remember the one reading the code doesn&#8217;t want to read every line carefully, this is just very very time consuming. Therefore following certains rules can help to make this easier.<br />
Another issue this code shows very well is that you first have to scan the code and understand the indentions, this sometimes means reading all the way up to the first variable declaration. It&#8217;s just unnecessary brain work.<br />
So let&#8217;s make the source code easier to read, use a var statement for every variable:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DO</span><br />
<span style="color: #003366; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;group.name&quot;</span><span style="color: #339933;">,</span> test<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> groupName<span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// Trim and replace all special chars, to have a proper test ID.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> group <span style="color: #339933;">=</span> g.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^s*/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/s*$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^0-9a-zA-Z]/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// Replace multiple underscores with just one.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ID_&quot;</span> <span style="color: #339933;">+</span> group<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/_+/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><br />
.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[_]+$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This also makes understanding the indentions much easier. Doesn&#8217;t it? I just looked at the draft of this blog article and thought that the &#8220;wrong&#8221; source code above was wrong formatted because I didn&#8217;t understand the indention just by looking at the code (without thinking too much). Looking at the &#8220;fixed&#8221; source made my brain not start to think, it just looked all very clear to me.</p>
<p>In another real life example it took me three hours (three fucking hours) to find the bug. It was the following code in the dojo-mobile <a href="http://github.com/wolframkriesing/dojo-mobile/blob/eae475575b0b1991fa66597c4faed94513e4a14c/build/build.js#L25">build script you can find on github</a>. And I was debugging the code, reading it over and over again. I just didn&#8217;t find the bug. Try the following, scan the code quickly just once and read on.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> parts <span style="color: #339933;">=</span> feature.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ns <span style="color: #339933;">=</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The namespace of the feature, like &quot;oo&quot; in &quot;oo&quot; or &quot;oo-declare.</span><br />
f <span style="color: #339933;">=</span> parts.<span style="color: #660066;">length</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The exact feature if given (&quot;oo-declare&quot;).</span><br />
ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
data <span style="color: #339933;">=</span> globals.<span style="color: #660066;">profileData</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Did you see it on first sight? Scan the code again. Believe me I read the code over and over again, I even thought there might be a variable declaration issue. But since my return variables are always named &#8220;ret&#8221; and my code was heavily asynchronously programmed I didn&#8217;t find out which declaration was the faulty one. It is fixed now (<a href="http://github.com/wolframkriesing/dojo-mobile/blob/master/tools/build.js">on github</a>)!</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DO</span><br />
<span style="color: #003366; font-weight: bold;">var</span> parts <span style="color: #339933;">=</span> feature.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> ns <span style="color: #339933;">=</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// The namespace of the feature, like &quot;oo&quot; in &quot;oo&quot; or &quot;oo-declare.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> f <span style="color: #339933;">=</span> parts.<span style="color: #660066;">length</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// The exact feature if given (&quot;oo-declare&quot;).</span><br />
<span style="color: #003366; font-weight: bold;">var</span> ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> globals.<span style="color: #660066;">profileData</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Yet another example. This one actually just proved again that I definitely have to forget about the old way. You can also <a href="http://github.com/wolframkriesing/dojo-mobile/blob/eae475575b0b1991fa66597c4faed94513e4a14c/build/build.js#L67">find it on github</a> and feel free to follow the path of commits. My learning curve, if you will :).</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> depsData <span style="color: #339933;">=</span> _depsDataStack.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
dir <span style="color: #339933;">=</span> depsData.<span style="color: #660066;">directory</span><span style="color: #339933;">;</span><br />
file <span style="color: #339933;">=</span> depsData.<span style="color: #660066;">file</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This one was not so hard to find, I agree. But still it was a bastard and stole my time. By sticking to a better convention I could have avoided it. Lesson learned.</p>
<h2>Closing Thoughts</h2>
<p>Just when preparing this article (which was supposed to be much shorter) I read through Douglas Crockford&#8217;s <a href="http://www.JSLint.com/lint.html">JSLint rules</a> and funnily I found this one in there:</p>
<blockquote><p>It is recommended that a single var statement be used per function.</p></blockquote>
<p>I can say I disagree and I guess I gave some reasoning.</p>
<p>Though on the other hand <a href="http://javascript.crockford.com/code.html">I found this</a> on the Crockford pages :)</p>
<blockquote><p>It is preferred that each variable be given its own line and comment. They should be listed in alphabetical order.</p></blockquote>
<p>Performance-wise, as stated above, I would leave the task to a compiler or alike tool to concatenate multiple variable statements into one, if it has an effect at all. The productivity and happiness of the programmers and not to loose time which could have been saved by just sticking to a simple convention is worth a lot more than pre-optimized code.<br />
I am looking forward to feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2010/04/02/one-var-statement-for-one-variable/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

