<?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>Coffee on the Keyboard &#187; programming</title>
	<atom:link href="http://coffeeonthekeyboard.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://coffeeonthekeyboard.com</link>
	<description>by James Socol</description>
	<lastBuildDate>Mon, 30 Jan 2012 17:56:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>		<item>
		<title>Thank You, Steve</title>
		<link>http://coffeeonthekeyboard.com/thank-you-steve-614/</link>
		<comments>http://coffeeonthekeyboard.com/thank-you-steve-614/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 05:17:12 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=614</guid>
		<description><![CDATA[Thank you, Steve. I didn&#8217;t really realize until today exactly what I owe to Steve Jobs&#8217; vision and dedication. So much of my life and career has been influenced and guided by an interest in screwing about with computers that goes back to the first Mac Plus I had—with its whole 1 megabyte of RAM [...]]]></description>
			<content:encoded><![CDATA[<p>Thank you, <a href="http://www.apple.com/stevejobs/">Steve</a>.</p>
<p>I didn&#8217;t really realize until today exactly what I owe to Steve Jobs&#8217; vision and dedication. So much of my life and career has been influenced and guided by an interest in screwing about with computers that goes back to the first Mac Plus I had—with its whole 1 megabyte of RAM and 40-megabyte SCSI hard drive—and my dad&#8217;s Macintosh II at work.</p>
<p>It&#8217;s a long, meandering story and probably not terribly interesting, but it really starts in 1993 or so, with Macintosh, and leads inexorably to my entire life now.</p>
<p>Thank you, Steve.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/thank-you-steve-614/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing at Scale: Database Replication</title>
		<link>http://coffeeonthekeyboard.com/developing-at-scale-database-replication-444/</link>
		<comments>http://coffeeonthekeyboard.com/developing-at-scale-database-replication-444/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 16:10:15 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Back-end]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sumo]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=444</guid>
		<description><![CDATA[When a website is small—like this one, for example—usually the entire thing, from the web server to the database, can live on a single server. Even a single virtual server. One of the first things that happens when a web site gets bigger is this is no longer true. One reason is load. A popular [...]]]></description>
			<content:encoded><![CDATA[<p>When a website is small—like this one, for example—usually the entire thing, from the web server to the database, can live on a single server. Even a single virtual server. One of the first things that happens when a web site gets bigger is this is no longer true.</p>
<p>One reason is load. A popular website will simply require more than a single server, virtual or otherwise, can give, and the only way to keep scaling is to add more servers. For example, if the server runs out of available Apache connections and the number cannot be raised without negatively impacting performance.</p>
<p>Another reason is downtime. If a website is served from a single server, and that server goes down for any reason, planned or otherwise, then the website is down. At some point, downtime is essentially unacceptable—just ask Twitter—and redundancy is required.</p>
<h3>Enter Replication</h3>
<p>A common response is to set up database replication, where one database server operates as a &#8220;master,&#8221; and one or more other servers operate as &#8220;slaves.&#8221; In this setup, all of your <em>writes</em> to the database will go to the master, then &#8220;replicate&#8221; to the slaves, and all or most of the <em>reads</em> will come from the slaves. (Note that the slaves are doing both all the writes as well as all the reads: slaves are not a good place to recycle sub-par hardware.)</p>
<p>Replication introduces a new type of problem: if you naively send <em>all</em> reads to the slaves then data you just wrote <em>will not be there</em>.</p>
<h3>La&#8230;wait for it&#8230;g</h3>
<p>Even if the master and slave are sitting next to each other with a cable connecting them, replication will probably take more time than your code does to reach the next step. At a minimum, you need to assume that replication lag will be hundreds of milliseconds—an eternity when the time from one line in your web app to the next is measured in micro- or nanoseconds. In reality, replication in the real world may well take seconds, especially if your master and slaves are not physically next to each other.</p>
<p>The result is that <a href="http://en.wikipedia.org/wiki/ACID">ACIDity</a> is essentially broken, specifically the <strong>D</strong>urability part. You cannot simply write data and immediately rely on its existence.</p>
<p>For example, say you have a large discussion forum. If you naively send all reads to the slaves, then someone&#8217;s post may take seconds to appear on the site. This is a problem if you&#8217;re trying to show a user their post immediately after posting it.</p>
<h3>Smarter Reading</h3>
<p>The solution is to occasionally read from the master. When you need to access data that was just written, it is <em>probably</em> only available on the master, so that&#8217;s where you&#8217;ll read it. Within a single HTTP request, this is fairly simple: just force any queries that rely on recently-written data to the master.</p>
<p>Outside of a single HTTP request, this is slightly more complex. If you&#8217;re following the practice of redirecting after a POST request to a GET request (which you should) then creating a new forum post and viewing it will be on two different HTTP requests.</p>
<p>One way around this is to set a very short-lived cookie that tells your web app to continue reading from the master. If any write occurs in a request, the response should include this cookie. The exact time-to-live will depend on how long your replication lag usually is—cover at least 4 or 5 standard deviations. Any request that has this cookie should honor it by reading only from the master.</p>
<h3>A Pitch</h3>
<p>One of the hardest things for new web developers is developing large-scale applications: first, you need a large-scale application! Setting up database replication is a huge pain, and if your site isn&#8217;t getting enough traffic, it&#8217;s not worth it.</p>
<p>Mozilla is one way aspiring web developers can get some experience working with large-scale web apps. All of our web apps are open source and open to contributions from community members. To get involved, stop by <a href="irc://irc.mozilla.org/webdev">#webdev</a> in IRC!</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/developing-at-scale-database-replication-444/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Programming in Middle School?</title>
		<link>http://coffeeonthekeyboard.com/programming-in-middle-school-255/</link>
		<comments>http://coffeeonthekeyboard.com/programming-in-middle-school-255/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 20:09:18 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[curriculum]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[high school]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[middle school]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=255</guid>
		<description><![CDATA[Eating breakfast in the hotel the other morning, my father mentioned a Twitter conversation he had to join about programming courses in schools and math curricula. Programming and math education? I just had to get involved, too. Ben Grey and Colleen K had started talking about the value of programming courses in school. Ben and [...]]]></description>
			<content:encoded><![CDATA[<p>Eating breakfast in the hotel the other morning, <a href="http://speedchange.blogspot.com">my father</a> mentioned a Twitter conversation he had to join about <a href="http://twitter.com/irasocol/status/2614850096">programming courses in schools</a> and math curricula.</p>
<p>Programming and math education? I just had to get involved, too.</p>
<p><a href="http://twitter.com/bengrey">Ben Grey</a> and <a href="http://twitter.com/colleenk">Colleen K</a> had started talking about the value of programming courses in school. Ben and my father (initially) were against it, concerned that the skills would be obsolete before they could be used, and were not particularly transferable to most fields. Colleen and I took the <em>pro</em> side.</p>
<p>Ben and my father both pointed out that languages go extinct, which is true. But judiciously chosen languages have staying power. Basic can be run in a browser, but is obviously a fine starting point. C has been around since 1972, and it isn&#8217;t going anywhere soon. JavaScript has been around since the mid-90s and seems to hold a secure point.</p>
<p>What of transferability? How many professional programmers still work in the first language they learn. Anyone? Anyone? Beuller? My first language was Perl, which helped me learn JavaScript and PHP, which helped me learn Java and C. People older than me probably started with Basic. People much older than me may have used ALGOL—which was on its fourth generation by the time C was born.</p>
<p>But what about the majority of students who don&#8217;t want to be programmers?</p>
<p>Programming is a powerful, concrete interface to the two mathematical concepts that cause the most problems for students in K-12: variables and functions.</p>
<p>In the US, our K-12 math curriculum covers three broad areas—numbers, variables and functions. These roughly correspond to primary math (counting, operations, fractions, equalities) algebra and geometry, and (pre-)calculus.</p>
<p>Most students can wrap their heads around numbers, even fractions. They are relatively concrete. Some students struggle with operations like multiplying and exponents, but this is the lowest hump, the bunny hill. The majority make it down unscathed.</p>
<p>When does your school start losing people in the math program? It&#8217;s probably around 8th grade. That&#8217;s when most schools begin the dreaded algebra. Variables are intuitive to some, but abstract and meaningless to others. If you believe Myers and Briggs, some of us are predisposed to abstractions and generalizations (*NTJ) but is there a biological reason others are lost here? Or do math teachers and curricula need to change?</p>
<p>Of the people who survive algebra without hating math, how many make it past &#8220;pre-calculus&#8221; or &#8220;FST&#8221;? I&#8217;ve heard the story a dozen times myself: &#8220;I was good at math until my calculus class.&#8221; If numbers are the bunny hill of K-12 math, then variables are the green circle, and functions are the double black diamond.</p>
<p>Again, functions may be completely intuitive to a few of us, but they can strike terror in the heart of even the most dedicated students.</p>
<p>Too often, when they reach a jump in complexity and struggle, students are told &#8220;it&#8217;s OK.&#8221; They are &#8220;not &#8216;math people.&#8217;&#8221; This can come from parents (&#8220;I was never good at math, either.&#8221;) or even well-intentioned teachers (&#8220;Maybe you&#8217;re just right-brained!&#8221;). There is a deeply held belief in this country that &#8220;math&#8221; is some innate ability, a genetic gift, and either you&#8217;re the next <a href="http://www.imdb.com/title/tt0119217/">Will Hunting</a>, or you may as well not try.</p>
<p>How does this relate to programming? How did you learn programming? Here&#8217;s a common roadmap:</p>
<ol>
<li>Imperative programming; no functions or abstractions; lots of constants; instructions in linear order.</li>
<li>Using variables for input or consistency/ease-of-maintenance.</li>
<li>Using functions and subroutines to encapsulate repeated operations.</li>
<li>Using someone else&#8217;s functions—you probably don&#8217;t see the implementation.</li>
<li>Branch off to more advanced ideas like object-oriented or functional programming; lots and lots and lots of abstractions.</li>
</ol>
<p>A remarkably parallel route. And instead of saying &#8220;x is a number,&#8221; you can say &#8220;<code>var name</code> holds the name the user enters!&#8221; The results are far more immediate and interactive. Play is cheap. (&#8220;What if I change this line? Oh it doesn&#8217;t work, better undo that.&#8221;) Instead of limiting functions to scary numbers and equations, more pedestrian words can be used as arguments and return values.</p>
<p>Programming is applied mathematics. Teachers spend much of their lives looking for new examples, better applications, to drive home the theory, when there is already a wealth of application available.</p>
<p>There is also a two-pronged economic argument. To paraphrase <a href="http://www.thomaslfriedman.com/bookshelf/the-world-is-flat">Mr. Friedman</a>: anything that can be outsourced, will, and the new jobs created here will require deeper interaction with computers. A cursory understanding of the programming techniques underneath will benefit all students as they enter the job market, and the exposure may mean more prepared students entering computer science programs. Basically: we need more talented, creative programmers—how many art students harbor latent programming skills—and even non-programmers will benefit from the exposure and understanding.</p>
<p>And of course, the math skills. No subject (that is taught) is taught as badly as math in our schools. On the personal level, this translates to people who misunderstand interest and get themselves in trouble with debt; on a national level, it certainly doesn&#8217;t help when a subprime mortgage market bubbles and pops.</p>
<p>Even if you don&#8217;t use &#8220;math&#8221; on a day-to-day basis, it is another way of thinking, of solving problems. Algorithmic thinking, epitomized in computer programming, provides a layer of cognitive flexibility, and every layer we can add, we should.</p>
<p>I don&#8217;t expect every student to become a master programmer, or even explicitly use those skills—or other skills in their math courses—every day. But I do expect schools to use every tool they have to make these methods of thinking and courses of study available to everyone. We wouldn&#8217;t allow a future programmer to skip his English class, why would we allow a future writer to skip his math and programming class?</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/programming-in-middle-school-255/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JavaScript: Private Static Members, Part 1</title>
		<link>http://coffeeonthekeyboard.com/javascript-private-static-members-part-1-208/</link>
		<comments>http://coffeeonthekeyboard.com/javascript-private-static-members-part-1-208/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 14:47:54 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=208</guid>
		<description><![CDATA[A little while ago I talked about creating private variables and methods in JavaScript. This works, but is not necessarily efficient: each instance of the class creates new copies of the members. While that may be exactly what you want for instance variables (think of partNum in the old examples) it is not always ideal. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://coffeeonthekeyboard.com/private-variables-in-javascript-177/" title="A little while ago">A little while ago</a> I talked about creating private variables and methods in JavaScript. This works, but is not necessarily efficient: each instance of the class creates new copies of the members. While that may be exactly what you want for instance variables (think of <code>partNum</code> in the old examples) it is not always ideal.</p>
<p>The complexity jumps significantly, though. So I&#8217;m dividing this half into two parts.</p>
<p>To get started, we need to forget about all this Object-Oriented Programming for a minute and look at some of the neat <a href="http://coffeeonthekeyboard.com/firefox-open-in-blank-tab-197/" title="tricks">tricks</a> you can do with functions in JavaScript.</p>
<p><strong>Update:</strong> <a href="http://coffeeonthekeyboard.com/javascript-private-static-members-part-2-218/" title="Part 2">Part 2</a> is now available.<span id="more-208"></span></p>
<p>First, let&#8217;s take a look at a few ways to define a function in JavaScript:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> oneFunction <span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// function body goes here</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> anotherFunction = <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// function body goes here</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>The first example, <code>oneFunction</code> should be familiar to programmers from most languages. The second one is completely equivalent, but works slightly differently. In this case, the right-hand side, a function, is being assigned to the left-hand side, the var <code>anotherFunction</code>.</p>
<p>Remember that in JavaScript, functions are first-class objects, just like everything else, so can be declared with the <code>var</code> keyword. They can also be passed to other functions as arguments, or returned from functions.</p>
<p>Now let&#8217;s take a brief look at parentheses. What do parentheses really do? Essentially, they evaluate and return whatever expression is inside them. For example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> five = <span class="br0">&#40;</span><span class="nu0">5</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// the expression is &quot;5&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> nine = <span class="br0">&#40;</span><span class="nu0">2</span> * <span class="nu0">4</span><span class="br0">&#41;</span> + <span class="nu0">1</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// &quot;2 * 4&quot; is evaluated and returned as &quot;8&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> nottrue = <span class="br0">&#40;</span><span class="kw2">true</span> || <span class="kw2">false</span><span class="br0">&#41;</span> &amp;amp;&amp;amp; <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// &quot;true || false&quot; evalutes to &quot;true&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">var</span> thirty = <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="nu0">5</span>*<span class="nu0">5</span><span class="br0">&#41;</span><span class="nu0">-10</span><span class="br0">&#41;</span>*<span class="nu0">2</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// &quot;5*5&quot; is evaluated, then returned as 25 to 25-10,</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// which evaluates to 15, which is returned and doubled</span></div>
</li>
</ol>
</div>
<p>So parentheses are slightly more powerful than the simple grouping operation we associate with them. Sometimes we see examples like this, which may be more illustrative:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> checkName <span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="br0">&#40;</span><span class="kw3">name</span>==<span class="st0">&#8216;admin&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So what happens if we combine parentheses&#8217; ability to evaluate and return code with our ability to define functions as an expression?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> aFunc = <span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="coMULTI">/*&#8230;*/</span> <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Of course, this is just the same as <code>anotherFunction</code> above, but you can see that the right-hand side &#8220;returns&#8221; a function. Let&#8217;s do something a little different now:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Hello, &quot;</span>+<span class="kw3">name</span>+<span class="st0">&quot;!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="st0">&quot;World&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>What&#8217;s going on here? The first set of parentheses [<code>(function ... )</code>] evaluate and return the code inside, creating a function. The last set [<code>("World")</code>] are then <em>calling</em> the function created by the first set. Immediately.</p>
<p>This is a powerful technique, but has certain limits. The interior function is executed immediately on creation, which means the <abbr title="Document Object Model">DOM</abbr> will probably not be loaded yet. Once the function is executed, it is lost. Trying to save it in the left-hand side of an equation will only save the <em>return value</em> of the function, not the function itself. For example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> aFunc = <span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&quot;I&#8217;m not a function!&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span>aFunc<span class="br0">&#41;</span>; <span class="co1">// Alerts &quot;I&#8217;m not a function!&quot;</span></div>
</li>
</ol>
</div>
<p>But, remember what I said about functions as first-class objects? It means we can use one function as a return value from another function:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> bFunc = <span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co1">// 1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co1">// 2</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Hello, World!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// 3</span></div>
</li>
</ol>
</div>
<p>The outer function (1) is executed immediately (3), and the var <code>bFunc</code> stores its return value, which is the inner function (2). So now, <code>bFunc</code> is a function, and calling <code>bFunc()</code> will alert &#8220;Hello, World!&#8221;.</p>
<p>We&#8217;ll stop for now. If this technique is new to you, play with it for a while. If not, just hang tight and I&#8217;ll get to <a href="http://coffeeonthekeyboard.com/javascript-private-static-members-part-2-218/" title="Part 2">Part 2</a> soon enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/javascript-private-static-members-part-1-208/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Private Variables in JavaScript</title>
		<link>http://coffeeonthekeyboard.com/private-variables-in-javascript-177/</link>
		<comments>http://coffeeonthekeyboard.com/private-variables-in-javascript-177/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 03:28:00 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[client-side]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=177</guid>
		<description><![CDATA[Ok, enough of this social/ranting stuff. Time to write something vaguely technical. I have a love-hate relationship with JavaScript. I think anyone who works with it does. Sometimes it just doesn&#8217;t do what you expect, and it&#8217;s certainly different. One trick, especially for people from real Object-Oriented languages like Java, Ruby, or let&#8217;s even say [...]]]></description>
			<content:encoded><![CDATA[<p class="note">Ok, enough of this social/ranting stuff. Time to write something vaguely technical.</p>
<p>I have a love-hate relationship with <a href="http://www.quirksmode.org/dom/compatibility.html">JavaScript</a>. I think anyone who works with it does. Sometimes it just doesn&#8217;t do what you expect, and it&#8217;s certainly different.</p>
<p>One trick, especially for people from real Object-Oriented languages like Java, Ruby, or let&#8217;s even say PHP 5, is the lack of access control. When everything is an object, the inability to hide certain values can become a problem.<span id="more-177"></span></p>
<p>Say, for example, you create an object that represents a product you sell, and you want to use one of your part numbers to identify the object. Let&#8217;s say the format for your part number is 1234-56. You might do something like&#8230;</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Product <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">partNum</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">setPartNum</span> = <span class="kw2">function</span> <span class="br0">&#40;</span> num <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> partNumRegex.<span class="me1">match</span><span class="br0">&#40;</span>num<span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">partNum</span> = num;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>You&#8217;re expecting the rest of your team to be respectful and use the <code>Product.setPartNum()</code> method, which should stop them from using an illegally formatted part number. (<code>partNumRegex</code> is left as an exercise to the reader.)</p>
<p>But what&#8217;s stopping them from doing this?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> jacket = <span class="kw2">new</span> Product;</div>
</li>
<li class="li1">
<div class="de1">jacket.<span class="me1">partNum</span> = <span class="st0">&#8217;1234&#8242;</span>;</div>
</li>
</ol>
</div>
<p>Now when they call your Ajax-driven <code>getProductData()</code> method, they&#8217;ll get an error. Had they used the <code>setPartNum()</code> method, they would&#8217;ve seen the error much earlier.</p>
<p>One trick to JavaScript is that variables are <a href="http://en.wikipedia.org/wiki/Scope_(programming)#Static_scoping_.28also_known_as_lexical_scoping.29">lexically scoped</a>. Basically, functions use the value of a variable where they are defined, not where they&#8217;re executed, for example (or just read <a href="http://blogs.msdn.com/kartikb/archive/2009/01/15/lexical-scoping.aspx">a better article</a>):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> a = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> globalA<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>; <span class="co1">//1</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> localA <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> a = <span class="nu0">2</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>; <span class="co1">//2</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So how does this help? Well, you can effectively hide values in a deeper scope. Instead of using the <code>this</code> keyword, just define new variables within your function:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Product <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> partNum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">setPartNum</span> = <span class="kw2">function</span> <span class="br0">&#40;</span>num<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Fancy checking logic</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">getPartNum</span> = <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> partNum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> tshirt = <span class="kw2">new</span> Product;</div>
</li>
<li class="li1">
<div class="de1">tshirt.<span class="me1">partNum</span> = <span class="nu0">7</span>;</div>
</li>
<li class="li1">
<div class="de1">tshirt.<span class="me1">getPartNum</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// null</span></div>
</li>
</ol>
</div>
<p>This same thing can apply to private methods. Don&#8217;t want the new guy calling <code>superSecretInternalMethod()</code>? One common practice is to start private methods with an underscore, but a better way is to actually hide the method:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Product<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> partNum; <span class="co1">// private part number, use accessors</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">setPartNum</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// as above</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">getPartNum</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> partNum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">function</span> superSecretInternalMethod<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Whatever your coding-ninja heart desires</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// I can access partNum</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> truck = <span class="kw2">new</span> Product;</div>
</li>
<li class="li1">
<div class="de1">truck.<span class="me1">superSecretInternalMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// JS Error: truck.superSecretInternalMethod is not a function.</span></div>
</li>
</ol>
</div>
<p>We can even go a step farther and define private class methods and variables, but this post is already long enough, and it&#8217;s rare I have material for two in a row, so I&#8217;ll save that for next time.</p>
<p class="note">If you haven&#8217;t read it, go get <a href="http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X">Pro JavaScript Design Patterns</a>. You won&#8217;t regret it.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/private-variables-in-javascript-177/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>System.out.println(&#8220;Hello World!&#8221;)</title>
		<link>http://coffeeonthekeyboard.com/systemoutprintlnhello-world-79/</link>
		<comments>http://coffeeonthekeyboard.com/systemoutprintlnhello-world-79/#comments</comments>
		<pubDate>Fri, 02 May 2008 22:58:27 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[block level]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/systemoutprintlnhello-world-79/</guid>
		<description><![CDATA[So I&#8217;ve been getting used to a couple new languages lately, mostly Ruby and Java but also Python and C++ for comparison. Coming from PHP, VBScript and JavaScript has confused the hell out of me. The hardest part so far is not strict types, which I expected would mess me up. It&#8217;s block-level scope. For [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been getting used to a couple new languages lately, mostly Ruby and Java but also Python and C++ for comparison. Coming from PHP, VBScript and JavaScript has confused the hell out of me.<span id="more-79"></span></p>
<p>The hardest part so far is not strict types, which I expected would mess me up. It&#8217;s block-level scope. For example, in PHP you could:</p>
<pre><code>&lt;?php
if ( $_GET['name'] ) {
    $name = $_GET['name'];
} else {
    $name = "World";
}
echo "Hello $name!";
?&gt;</code></pre>
<p>But in C++ and Java, variables defined within <em>any</em> block (section wrapped in {}), are only visible in that block and its descendant blocks, so when I tried</p>
<pre><code>
class HelloWorld
{
    public static void main ( String[] args )
    {
        if ( args.length &gt; 0 ) {
            String name = args[0];
        } else {
            String name = "World";
        }
        System.out.println("Hello "+name+"!");
    }
}</code></pre>
<p>it wouldn&#8217;t compile, saying &#8220;name cannot be resolved.&#8221; It took me a while to figure this one out.</p>
<p>Fortunately I had some help from a friend but honestly it still seems counter-intuitive to need to declare a value before copying a fixed value. For the record, here&#8217;s a version of the last class that works:</p>
<pre><code>
class HelloWorld
{
    public static void main ( String[] args )
    {
        String name;
        if ( args.length &gt; 0 ) {
            name = args[0];
        } else {
            name = "World";
        }
        System.out.println("Hello "+name+"!");
    }
}</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/systemoutprintlnhello-world-79/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

