<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="http://feedproxy.google.com/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feedproxy.google.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" version="2.0">

<channel>
	<title>Semicolon</title>
	
	<link>http://www.thunderguy.com/semicolon</link>
	<description>Software, the Internet and you.</description>
	<pubDate>Tue, 02 Sep 2008 09:23:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feedproxy.google.com/semicolon" type="application/rss+xml" /><item>
		<title>P is not for telephone</title>
		<link>http://www.thunderguy.com/semicolon/2008/08/08/p-is-not-for-telephone/</link>
		<comments>http://www.thunderguy.com/semicolon/2008/08/08/p-is-not-for-telephone/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 10:07:55 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[Recommendations]]></category>

		<category><![CDATA[Usability]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/?p=121</guid>
		<description><![CDATA[There&#8217;s a trend towards indicating contact details by a single letter. You see this on business cards, email signatures and letterheads:
T (01) 234 5678 - F (01) 234 5678 - E bob@boztangle.com
It looks quite nice and clean, especially in a vertical format on a screen as the single letters all line up in nice columns. [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a trend towards indicating contact details by a single letter. You see this on business cards, email signatures and letterheads:</p>
<p>T (01) 234 5678 - F (01) 234 5678 - E bob@boztangle.com</p>
<p>It looks quite nice and clean, especially in a vertical format on a screen as the single letters all line up in nice columns. And it&#8217;s pretty obvious what the letters stand for if they stick to the obvious words: telephone, fax, mobile, email. But too often people try to be clever and end up creating confusion. <span id="more-121"></span>Here are some examples.</p>
<p>P (09) 876 5432</p>
<p>P is meant to stand for &#8220;phone&#8221;. This is a real howler; it reminds me of the Simpsons episode that mentioned a horror movie called &#8220;P is for Psycho&#8221;. P is obviously not a natural abbreviation of &#8220;phone&#8221; or &#8220;psycho&#8221; because these words don&#8217;t sound as if they begin with P. And &#8220;phone&#8221; is already an abbreviation. For telephone. Which begins with T.</p>
<p>D (02) 468 1357</p>
<p>D stands for &#8220;direct&#8221;. In this case, there&#8217;s probably a T that gives the number of the company reception desk. But wait a minute. This is Bob&#8217;s business card, so when I see a T, I expect it to be Bob&#8217;s phone number. His direct phone number. The number of the reception desk is a secondary number, and we must face the fact that there&#8217;s no clear single-letter abbreviation for this.</p>
<p>C (021) 468 1357</p>
<p>C stands, of course, for &#8220;cellphone&#8221;. That&#8217;s &#8220;of course&#8221; only if you live in the USA or some other country where mobile phones are called cellphones. I think that M for mobile is more universal than C for cell, as well as being more descriptive. I&#8217;ll bet that most people who call their phone a cellphone don&#8217;t know why it&#8217;s called this.</p>
<p>DDI (02) 468 1357</p>
<p>This stands for Direct Dial In. You also sometimes see DID for Direct Inward Dial. This is pretty common in New Zealand &#8212; it&#8217;s a quaint reminder of a simpler time, when most companies had only a few incoming phone numbers and having a direct line was a big deal; of course this concept required its own special abbreviation. Nowadays I think we can just stick with T for telephone.</p>
<p>So here&#8217;s a handy guide to the single-letter abbreviations you can safely use for your English-language contact details.</p>
<p><strong>T</strong> Telephone &#8212; the person&#8217;s phone number. This is the direct number if there is one, otherwise the general reception number.</p>
<p><strong>M</strong> Mobile &#8212; the person&#8217;s mobile phone number.</p>
<p><strong>F</strong> Fax &#8212; the person&#8217;s fax number. (Some people like to write &#8220;FAX&#8221; instead of &#8220;fax&#8221; for some reason, but I&#8217;m sure you&#8217;re not one of these people.)</p>
<p><strong>E</strong> Email &#8212; the person&#8217;s email address.</p>
<p><strong>W</strong> Web &#8212; typically the general company website. Of course you should leave off the http:// from the website. But then you knew that already.</p>
<p>Anything else is going to need more than just a single letter. For consistency in these cases, just spell everything out rather than have funky single-letter abbreviations for some details but not others.</p>
<p>Some of these contact details don&#8217;t really need any indicator at all: bob@boztangle.com is obviously an email address. Mobile phone numbers are often distinguishable by the number of digits, but this only works for people who are familiar with that country&#8217;s numbering plan. (Even then it doesn&#8217;t always work: Hong Kong has no area codes and uses eight digits for all phone numbers, fixed and mobile. Then again, in my experience nobody used fixed line phones in Hong Kong anyway.) Given that, if there&#8217;s only a fixed line or a mobile or both, and also maybe an email and a website, you can just give them unadorned:</p>
<p>(09) 234 5678 - ( 021) 234 5678 - bob@boztangle.com  - www.boztangle.com</p>
<p>Now that&#8217;s clean and elegant. But more importantly, it&#8217;s clear. Adding &#8220;P&#8221; would just confuse the matter.</p>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=121&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_121" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/6YcVqcRh0TY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2008/08/08/p-is-not-for-telephone/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Lotus Notes is rubbish</title>
		<link>http://www.thunderguy.com/semicolon/2008/07/10/lotus-notes-is-rubbish/</link>
		<comments>http://www.thunderguy.com/semicolon/2008/07/10/lotus-notes-is-rubbish/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 11:12:49 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[Risks]]></category>

		<category><![CDATA[Tools]]></category>

		<category><![CDATA[Usability]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/?p=119</guid>
		<description><![CDATA[The Lotus Notes UI is a disaster. Examples are legion, and there are doubtless a million web pages attesting to this. So here&#8217;s the million-and-first. I thought Notes was clunky the first time I encountered it, which was back in 1996. I was dismayed in 2003 when I started a new job to find that [...]]]></description>
			<content:encoded><![CDATA[<p>The Lotus Notes UI is a disaster. Examples are legion, and there are doubtless a million web pages attesting to this. So here&#8217;s the million-and-first. I thought Notes was clunky the first time I encountered it, which was back in 1996. I was dismayed in 2003 when I started a new job to find that I would have to use the Notes client, not just for email, but also for internal documentation and even for entering time sheets. It turned out that that company had started life as a Lotus Notes development shop. I managed to escape, but now my new company and the client that I am working for both use Notes for email. It&#8217;s a nightmare.<span id="more-119"></span></p>
<p>Unless otherwise indicated, all comments apply to Lotus Notes client version 6.5, though a lot of the comments apply to version 7.0 as well. Webmail comments apply to version 7.0.</p>
<p><strong>Preferences</strong>: Looking for Preferences? There is a Preferences submenu under the File menu. It contains <em>six</em> menu items for different kinds of preferences (but none of them are to do with files). But there&#8217;s also another preferences command, in the <em>Actions</em> menu under the <em>Tools</em> submenu (note: not an action). So for example if you want to add a digital signature to all your sent mail, you set this in <em>File/Preferences/User Preferences/Mail</em>; but if you want to add a text signature, you go to <em>Actions/Tools/Preferences/Mail/Signature</em>. This is ridiculous.</p>
<p><strong>New Folder</strong>: I wanted to create a new folder to file some emails. In the panel on the left hand of the window, there&#8217;s the standard Inbox, Drafts, Sent and so on, as well as a Folders icon that can be expanded to show all your folders. You&#8217;d think that the command to create a new folder would be obvious. You&#8217;d be wrong.</p>
<p><em>Right-click the Folders icon.</em> Huh? I get a context menu with options like <em>Italic</em> and <em>Underline</em>, amongst others. (I select <em>Italic</em> just for fun. Nothing happens.) But no <em>New Folder</em> option.</p>
<p><em>Right-click the panel itself.</em> Huh? I get a context menu with options like <em>Document properties</em> and even <em>Forward</em>. (Just for fun I select <em>Forward</em> &#8212; a new email appears containing a <em>picture</em> of the lefthand panel. It&#8217;s hard to overstate the uselessness of this.) But no <em>New Folder</em> option.</p>
<p><em>File/New from the menu bar.</em> This gives me a few options: <em>Memo</em>, <em>Calendar Entry</em>, <em>Contact</em>, <em>To Do</em>, and <em>URL</em>. (<em>URL</em>?! I try selecting that. It turns out that it actually just lets me type in a URL rather than create a new one. And then it opens up the URL in Internet Explorer. Even though Firefox is my default browser.) But no <em>New Folder</em> option.</p>
<p><em>Create from the menu bar.</em> Surely. But no: I can create a new <em>Memo</em>, <em>Reply</em>, <em>To Do</em>, etc. There&#8217;s a <em>Special</em> submenu that lets me send a message to the database manager. There&#8217;s also an <em>Other&#8230;</em> option. Surely here! But no, <em>Other&#8230;</em> actually just gives me a list of the same options in the rest of the <em>Create</em> menu. Brilliant &#8212; the <em>Other</em> option actually does not give me any other options at all! It&#8217;s utterly worthless and confusing and I really can&#8217;t be bothered trying to come up with words to express how monumentally stupid the whole thing is. And I still can&#8217;t create a new folder, even from the <em>Create</em> menu.</p>
<p>And now finally I discover the several ways to create a folder. I select the <em>Inbox</em> icon to show the Inbox view. There is a button bar above the actual Inbox view. It&#8217;s not a toolbar &#8212; there are already two toolbars below the menu bar. This button bar is just a row of buttons, except that some of them are really dropdown menus that look like buttons. Anyway, there is a <em>Folder</em> button (actually a menu) that has a <em>Create Folder&#8230;</em> option. I didn&#8217;t notice it before because it wasn&#8217;t there &#8212; I had the Trash displayed, and the <em>Folder</em> button/menu does not appear when the Trash is displayed.</p>
<p>With the Inbox showing, there&#8217;s also a <em>Folder/Create Folder&#8230;</em> item in the <em>Actions</em> menu. But I also notice that if I click the Inbox icon, the menu bar shows <em>File</em>, <em>Edit</em>, <em>View</em>, <em>Create</em>, <em>Actions </em>and <em>Help</em>. But if I click the word &#8220;Inbox&#8221; beside the icon, the <em>View</em>, <em>Create</em> and <em>Actions</em> menus disappear after a second. If I click it again, those three menus appear for a second and then disappear again. Oh dear.</p>
<p>Also, it turns out that I could right-click the Inbox icon &#8212; a <em>New Folder&#8230;</em> item is in the context menu. Note that the new folder does not appear in the Inbox. In fact it has nothing to do with the Inbox.</p>
<p>So finally I have created a new folder, which appears under the Folders icon. It appears that I can now right-click on my new folder to get a context menu that will let me create another folder. And now that there&#8217;s a folder under the Folders icon, right-clicking the Folders icon gives me a different context menu! It&#8217;s the left-hand panel context menu (with the nonsensical <em>Forward</em> option) rather that the old Folders icon context menu (with the nonsensical <em>Italic</em> option).</p>
<p>All this just for a folder. In the time it took me to do this I could have simply run the Lotus Notes uninstaller and ended up much happier.</p>
<p><strong>Scrolling</strong>: I got a meeting invitation that contained several screenfuls of text, but there was no scrollbar. In fact I didn&#8217;t even discover the text until I printed it out and was surprised that it was 5 printed pages. Scrolling with arrow keys or scroll wheel didn&#8217;t work. I also couldn&#8217;t select any text. I tried going into edit mode, and that seemed to make things better, though the scroll wheel still didn&#8217;t work unless I actually selected some text. (!?)</p>
<p><strong>Daylight Saving</strong>: This is from a Certain Company&#8217;s intranet from 31 March 2008. This is not a joke:</p>
<blockquote><p>The end date of Daylight Saving Time in New Zealand has been extended for 1 week this year. We now revert back on Sunday 6th of April.</p>
<p>When browsing your calendar for the week starting Monday 31st of March, your meetings may appear to be 1 hour late, so please OPEN the calendar entries to view the correct start time.</p></blockquote>
<p><strong>Ugly</strong>: <img src="http://www.thunderguy.com/semicolon/wp/wp-content/uploads/2008/07/stop.gif" align="left" alt="Big fat ugly stop sign" title="Stop" width="32" height="32" class="alignright size-full wp-image-120" />The Notes client has its <a href="http://lotusnotessucks.4t.com/lnEx01.html">mysteriously-changing &#8220;keychain&#8221; picture</a>. The Web client has a big red stop sign on its login page. The stop sign has a fat, fleshy hand on it, just to hammer home the point that you have to stop. It looks like a scary error icon. Why is it even there at all? The only sensible interpretation I can think of is that it&#8217;s saying &#8220;Stop, the application you are about to log in to is hideously bloated and ugly.&#8221;</p>
<p><strong>Meeting invitations</strong>: There&#8217;s no obvious way to accept a meeting invitation without sending a response. Even worse, select <em>Respond with Comments</em>; an email form appears. Change your mind and close the email form. You get an &#8220;are you sure?&#8221; dialog box, and since it&#8217;s Notes, you get several choices: Send as is, send without comments, or continue editing. That&#8217;s right, there is no way to cancel. You are forced to send the response. You can&#8217;t even quit from Notes now &#8212; if you try you will get the dialog box offering the same three choices. The only way to quit Notes without sending the response is to use the Windows Task Manager to kill the process. This is magnificently, fantastically stupid and is just another reason why the people who designed Notes should hang their heads in shame.</p>
<p>It turns out that even killing Notes won&#8217;t kill the meeting response form. When I started up Notes again, the form was there. Apparently that form will now be a permanent fixture of my Notes usage forever, until I finally give in and send it. Even though I don&#8217;t want to send it. Shame! Shame!</p>
<p>I noticed that a couple of days later, the form had finally gone. I can&#8217;t even remember if I restarted my PC or something like that, or if I finally gave up and sent the confounded thing. I have no more comment.</p>
<p><strong>Webmail drafts</strong>: When you&#8217;re writing an email using the webmail interface, the Save button saves the email as a draft and then closes the window. So if you&#8217;re writing a long email and you want to save periodically, you have to keep pressing the button and then going back to the main window, opening the Drafts folder, opening your message and scrolling down to the bottom to continue. What a waste of time.</p>
<p><strong>Spurious error</strong>: In webmail, if I create a new message and start editing it and then return to the main window and select a mail folder, a message pops up saying that they are sorry (which is nice) but &#8220;we were unable to process your request at this time.&#8221; (I don&#8217;t know who &#8220;we&#8221; refers to &#8212; the entire Lotus Notes development team, presumably). So I click OK to dismiss the dialog box and guess what? Everything works fine.</p>
<p>Well, I could go on. In fact I already have. It&#8217;s important to pick holes in Notes like this because otherwise we might get used to its warts. If we relax our standards for the software we use, it might flow on to affect the software we write. For this and for so many other reasons, Lotus Notes is rubbish.</p>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=119&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_119" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/BRKlccXO4nY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2008/07/10/lotus-notes-is-rubbish/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Vespa: A better MVC</title>
		<link>http://www.thunderguy.com/semicolon/2008/06/23/vespa-a-better-mvc/</link>
		<comments>http://www.thunderguy.com/semicolon/2008/06/23/vespa-a-better-mvc/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 11:12:41 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[Architecture]]></category>

		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/?p=113</guid>
		<description><![CDATA[Vespa is a refinement of the old MVC (Model View Controller) architectural pattern that better reflects how Web applications actually work. MVC has been around for a long time, but many implementations have some annoying inconsistencies. I have refined the basic MVC pattern in light of typical usage patterns I&#8217;ve encountered; the result makes it [...]]]></description>
			<content:encoded><![CDATA[<p>Vespa is a refinement of the old MVC (Model View Controller) architectural pattern that better reflects how Web applications actually work. MVC has been around for a long time, but many implementations have some annoying inconsistencies. I have refined the basic MVC pattern in light of typical usage patterns I&#8217;ve encountered; the result makes it easier to design an MVC application in a clear, modular manner.<span id="more-113"></span></p>
<p>The Model-View-Controller application structure came from Smalltalk and has been around for 25 years or so. The Model refers to the application classes, the Controllers are the classes that interpret and respond to user input, and the View displays the results to the user. Web MVC frameworks like <a href="http://struts.apache.org/">Struts</a>, <a href="http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html">Spring Web MVC</a> and <a href="http://www.rubyonrails.org/">Ruby on Rails</a> use a similar kind of structure, refined to fit the way the Web works.</p>
<h2>Problems with MVC</h2>
<p>Here&#8217;s the standard MVC overview. There are three dependencies. First, the Controller updates the Model and extracts Model data. Second, the Controller packages Model data for the View. Third, the View displays data from the Model.</p>
<p><img src="http://www.thunderguy.com/semicolon/wp/wp-content/uploads/2008/06/mvc.png" alt="Model View Controller" title="Model View Controller" width="198" height="161" class="alignleft size-full wp-image-115" /></p>
<p>A few things bothered me about the MVC architecture as implemented in Spring and elsewhere.</p>
<p>1. The Controller classes depend on the View. The Controllers create a Model object which is displayed by the View; because of this, the Controller needs to know which data to include in the Model, because the View is expecting certain named data items. In effect, the Views have an implicit interface that the Controller must honour. This seems wrong if the Controller processes input and the View displays output &#8212; they should be completely decoupled.</p>
<p>2. In many cases, Controllers do too much work. For example, a typical edit controller carries out three separate functions. First, it displays a form for editing a record. Second, it processes user input (e.g. &#8220;user clicks the Delete button&#8221;). Third, it creates a Model and forwards to a View (e.g. show a list of widgets). These activities are not necessarily related, so why bundle them together in one class, or even in one package?</p>
<p>3. The definition of Model seems inconsistent. Originally the model referred simply to the data that was to be displayed in the view. This is sometimes referred to as a Presentation View. However, the <a href="http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html">J2EE &#8220;Model 2&#8243; MVC architecture</a> considers the Model to be all domain-specific code, including data but also including business logic. Rails is similar, but the Model is tied to the database layer, and business logic often seems to live in the Controller. Frameworks like Spring revert to the original meaning, where the Controller creates a Model data structure for use by the View.</p>
<p>These are not really problems; they are just a result of the high-level nature of the MVC structure. It&#8217;s easy to understand MVC, but it&#8217;s impossible to follow it strictly because the structure itself is ill-defined.  Drilling down insde the MVC pattern lets us bring out more detail and understand better the responsibilities of each component. The Controller &#8594; Model dependency is straightforward, but the other two dependencies can be refined so as to clarify the MVC architecture.</p>
<h2>Refactoring the Controller</h2>
<p>In many cases, a Controller carries out several tasks. For example, a typical edit controller carries out three separate functions: form display, input processing, and results display. Code like this is typical.</p>
<blockquote><p><strong>Controller:</strong><br />
if (editing a new record) then<br />
&nbsp;&nbsp;&nbsp;Display the form<br />
else<br />
&nbsp;&nbsp;&nbsp;Save changes<br />
&nbsp;&nbsp;&nbsp;Display the updated record<br />
endif
</p></blockquote>
<p>Often, the code to set up and display a form has only a tiny amount in common with the code to just display a record. The form Controller code need to retrieve values for dropdown menus and checkboxes and other supporting information; the form View needs client-side data validation and other presentation code. And the code for saving changes has little in common with either of the views. This suggests separating the two. Controller code that sets up a view doesn&#8217;t need to update the Model; it only reads it. This type of Controller I call a Presenter. A Controller that updates the Model is called an Actor.</p>
<p><img src="http://www.thunderguy.com/semicolon/wp/wp-content/uploads/2008/06/mvap.png" alt="Model View Actor Presenter" title="Model View Actor Presenter" width="304" height="136" class="alignleft size-full wp-image-114" /></p>
<p>With this structure, the three tasks previously managed by the single Controller become the responsibility of three separate objects: two Presenters and one Actor.</p>
<blockquote><p><strong>Presenter 1:</strong><br />
Display the form</p>
<p><strong>Actor:</strong><br />
Save changes</p>
<p><strong>Presenter 2:</strong><br />
Display the updated record
</p></blockquote>
<p>Also, note that the Presenter depends on the View (as the Controller did before) but there is no dependency between the Actor and the View. This confirms that the Presenter and the Actor really are separate kinds of object.</p>
<h2>Refactoring the Model</h2>
<p>The other dependency is that of the View on the Model. To clarify this, I noticed that &#8220;Model&#8221; sometimes refers to all classes that embody application data, and sometimes it refers only to specific subsets of that data: for example, a specific data structure used by a specific View (as in Spring), or an object-based abstraction to a database (as in Rails). For my purposes, I have decomposed the Model into two separate pieces. One part is the plain data entities that represent business data; these are called Entities. The other part is the code used to store and manipulate the entities; this is called the Store.</p>
<p>Starting from the basic MVC, here&#8217;s what happens when we split the Model into Entity and Store.</p>
<p><img src="http://www.thunderguy.com/semicolon/wp/wp-content/uploads/2008/06/sevc.png" alt="Store Entity View Controller" title="Store Entity View Controller" width="314" height="155" class="alignleft size-full wp-image-117" /></p>
<p>This shows that the View depends only on the Entity, not the Store. This makes sense, since the View only needs to display the basic data objects provided to it by the Controller; it does not access the Store itself.</p>
<h2>The Vespa pattern</h2>
<p>Putting these two ideas together gives us a unified diagram showing how the five areas of the application depend on each other.</p>
<p><img src="http://www.thunderguy.com/semicolon/wp/wp-content/uploads/2008/06/sevap.png" alt="Store Entity View Actor Presenter" title="Store Entity View Actor Presenter" width="333" height="148" class="alignleft size-full wp-image-116" /></p>
<p>This diagram makes it clear that everything depends on the Entities. This is what we would expect. The essential function of an application is to manage data; we would therefore expect all areas of the application to depend on the data to some extent.</p>
<p>Since the Entities are essentially a global dependency, we can simplify the diagram by making that explicit. The result is an architectural pattern that I like to call Vespa. (It&#8217;s an acronym for View, Entity, Store, Presenter, Actor; but it looks ugly if written in capitals. It&#8217;s also a brand of motor scooter, so conjures up images of sleek, fast, shiny things.)</p>
<p><img src="http://www.thunderguy.com/semicolon/wp/wp-content/uploads/2008/06/vespa.png" alt="Vespa architecture" title="Vespa architecture" width="435" height="127" class="alignleft size-full wp-image-118" /></p>
<p>The Vespa diagram is almost as simple as the original MVC diagram, but it is far more instructive and useful. It&#8217;s easy to see where each application function fits and what other functions it depends on. It&#8217;s particularly appropriate for Web applications. If you&#8217;re writing a simple application or a custom framework, you can follow the Vespa architecture directly. Even if you&#8217;re using an existing framework such as Spring, Vespa can help to clarify your thinking as you organise your Controllers.</p>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=113&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_113" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/ycifSqZmR2I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2008/06/23/vespa-a-better-mvc/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Die QUIETLY!</title>
		<link>http://www.thunderguy.com/semicolon/2008/03/19/die-quietly/</link>
		<comments>http://www.thunderguy.com/semicolon/2008/03/19/die-quietly/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 10:25:39 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[Coding]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Risks]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/?p=112</guid>
		<description><![CDATA[Here is a piece of JavaScript code taken from a public commercial website. I have redacted some of the code to protect the guilty, but the rest is verbatim in all its glory. See if you can spot the error.
try {
   doSomething();
} catch(err) {
   // Die quietly
   alert(err);
}
Share This
]]></description>
			<content:encoded><![CDATA[<p>Here is a piece of JavaScript code taken from a public commercial website. I have redacted some of the code to protect the guilty, but the rest is verbatim in all its glory. See if you can spot the error.<span id="more-112"></span></p>
<pre><code>try {
   doSomething();
} catch(err) {
   // Die quietly
   alert(err);
}</code></pre>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=112&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_112" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/0rYWWRaxpVo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2008/03/19/die-quietly/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Search Meter WordPress plugin version 2.3</title>
		<link>http://www.thunderguy.com/semicolon/2007/08/28/search-meter-wordpress-plugin-version-23/</link>
		<comments>http://www.thunderguy.com/semicolon/2007/08/28/search-meter-wordpress-plugin-version-23/#comments</comments>
		<pubDate>Tue, 28 Aug 2007 11:42:08 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/2007/08/28/search-meter-wordpress-plugin-version-23/</guid>
		<description><![CDATA[I have updated Search Meter, my WordPress plugin that lets you find out what your blog visitors have been searching for. The new version includes a couple of widgets to make it easy to display recent and popular searches on your blog, as well as a few other changes. Here are the details.
Widgets
Search Meter 2.3 [...]]]></description>
			<content:encoded><![CDATA[<p>I have updated <a href="http://www.thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/">Search Meter</a>, my WordPress plugin that lets you find out what your blog visitors have been searching for. The new version includes a couple of widgets to make it easy to display recent and popular searches on your blog, as well as a few other changes. Here are the details.<span id="more-110"></span></p>
<h2>Widgets</h2>
<p>Search Meter 2.3 includes two WordPress widgets. <strong>Popular Searches</strong> shows a list of the most popular search terms that have produced hits at your site during the last 30 days, and <strong>Recent Searches</strong> shows a list of the most recent searches that produced hits at your site. Readers can click search terms in the list to search for the term themselves. Both widgets display up to 5 search terms by default, but you can change this to whatever number you like.</p>
<h2>Security</h2>
<p>This version fixes a security hole in previous releases where it was theoretically possible for an unscrupulous website to trick you into deleting your saved search data. This was a pretty minor risk: the search data is transient anyway, and I have not heard of anyone even thinking of doing this, let alone actually managing to carry it out. Anyway, from Search Meter 2.3 the &#8220;attack&#8221; is no longer possible. Previous version are still vulnerable, so truly paranoid users should upgrade. And don&#8217;t forget to wear your tinfoil hat.</p>
<h2>A polite suggestion</h2>
<p>The Options and Statistics screens now include a button allowing convenient access to PayPal to make Search Meter donations. This is meant to be a gentle, unobtrusive suggestion rather than a blatant advertisement. There&#8217;s an option to turn it off if you don&#8217;t like it (or even better, if you have already made a donation).</p>
<p>Remember, donating is optional. You can donate nothing, or you can donate $1000 (yes, really) but Search Meter will work the same regardless.</p>
<h2>Other changes</h2>
<p>I have made a number of changes to the internal workings of the Search Meter code, just to tidy things up a bit and make it easier for me to work with. This version also contains the installation bug fix from Search Meter 2.2, which I released quietly a few months ago. That problem was caused by a core change made in WordPress 2.2.1.</p>
<h2>Download</h2>
<p>As always, go to the main <a href="http://www.thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/">Search Meter plugin</a> page to see full details and to download Search Meter 2.3.</p>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=110&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_110" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/oitIG9fUiWE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2007/08/28/search-meter-wordpress-plugin-version-23/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to look at a GIF (in Windows)</title>
		<link>http://www.thunderguy.com/semicolon/2007/08/24/how-to-look-at-a-gif-in-windows/</link>
		<comments>http://www.thunderguy.com/semicolon/2007/08/24/how-to-look-at-a-gif-in-windows/#comments</comments>
		<pubDate>Fri, 24 Aug 2007 04:08:48 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/2007/08/24/how-to-look-at-a-gif-in-windows/</guid>
		<description><![CDATA[Windows Picture and Fax Viewer (WPFV) is a bare-bones application for viewing images, but it does the job. Except when it doesn&#8217;t; somehow my Windows installation decided that it was going to use Internet Explorer instead of WPFV by default when opening GIF files. The obvious fix for this didn&#8217;t work properly, but I found [...]]]></description>
			<content:encoded><![CDATA[<p>Windows Picture and Fax Viewer (WPFV) is a bare-bones application for viewing images, but it does the job. Except when it doesn&#8217;t; somehow my Windows installation decided that it was going to use Internet Explorer instead of WPFV by default when opening GIF files. The obvious fix for this didn&#8217;t work properly, but I found a simple way of getting back to the behaviour I wanted.<span id="more-111"></span></p>
<p>I could revert to using WPFV as a default GIF viewer by using Windows Explorer Options, but this seemed to affect only Explorer; if I opened an email attachment in Thunderbird it still tried to use IE. The real answer is simply to re-register the DLL the WPFV uses. Here&#8217;s the command to run, courtesy of <a href="http://ask-leo.com/what_if_i_want_windows_picture_and_fax_viewer.html">Leo</a>:</p>
<pre><code>regsvr32 /i shimgvw.dll</code></pre>
<p>Just run this once in your Windows system and all will be right with the world again.</p>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=111&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_111" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/a-CS4l0dwNY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2007/08/24/how-to-look-at-a-gif-in-windows/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Code Markup WordPress plugin version 1.2</title>
		<link>http://www.thunderguy.com/semicolon/2007/08/16/code-markup-wordpress-plugin-version-12/</link>
		<comments>http://www.thunderguy.com/semicolon/2007/08/16/code-markup-wordpress-plugin-version-12/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 10:32:12 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/2007/08/16/code-markup-wordpress-plugin-version-12/</guid>
		<description><![CDATA[Code Markup, my WordPress plugin that makes it easy to include program code samples in your posts, has been updated to version 1.2.  Code Markup lets you include HTML markup in the code sample; it magically knows which characters should be displayed as code and which should be rendered as HTML. Version 1.2 has [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.thunderguy.com/semicolon/wordpress/code-markup-wordpress-plugin/">Code Markup</a>, my <a href="http://wordpress.org/">WordPress</a> plugin that makes it easy to include program code samples in your posts, has been updated to version 1.2.  Code Markup lets you include HTML markup in the code sample; it magically knows which characters should be displayed as code and which should be rendered as HTML. Version 1.2 has a few tweaks that will make it more useful and usable.<span id="more-109"></span></p>
<p>The best new feature is that Code Markup now fixes a WordPress bug where backslashes inside a <code>&lt;pre&gt;</code> block would not be displayed. With Code Markup, such backslashes are now displayed even if not in a code block.</p>
<p>The &#8220;allow&#8221; attribute is now called &#8220;markup&#8221;. I could never remember whether <code>allow="span"</code> meant that <code>&lt;span&gt;</code> was allowed as a literal or allowed as markup. Now I hope it&#8217;s clearer: <code>markup="span"</code> means that <code>&lt;span&gt;</code> will be treated as markup, not displayed literally.</p>
<p>For backwards-compatibility, you can still use &#8220;allow&#8221; as the attribute name. So any existing code blocks will still work with the new Code Markup.</p>
<p>Finally, Code Markup works better now with tags and comments that span multiple lines.</p>
<h2>Download</h2>
<p>For details and download, see the main <a href="http://www.thunderguy.com/semicolon/wordpress/code-markup-wordpress-plugin/">Code Markup</a> page.</p>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=109&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_109" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/vIY43Bw7gWA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2007/08/16/code-markup-wordpress-plugin-version-12/feed/</wfw:commentRss>
		</item>
		<item>
		<title>elementReady: a jQuery plugin</title>
		<link>http://www.thunderguy.com/semicolon/2007/08/14/elementready-jquery-plugin/</link>
		<comments>http://www.thunderguy.com/semicolon/2007/08/14/elementready-jquery-plugin/#comments</comments>
		<pubDate>Tue, 14 Aug 2007 03:59:21 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Web publishing]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/2007/08/14/elementready-jquery-plugin/</guid>
		<description><![CDATA[I have written a simple but useful jQuery plugin. elementReady calls a function during page load as soon as a specific element is available &#8212; even before the full DOM is loaded. It&#8217;s useful if you have unobtrusive JavaScript that you want to apply to particular page elements immediately, without having to wait for the [...]]]></description>
			<content:encoded><![CDATA[<p>I have written a simple but useful jQuery plugin. <code>elementReady</code> calls a function during page load as soon as a specific element is available &#8212; even before the full DOM is loaded. It&#8217;s useful if you have unobtrusive JavaScript that you want to apply to particular page elements immediately, without having to wait for the whole DOM to load in a large page.<span id="more-108"></span></p>
<p>Using the elementReady plugin is very similar to the existing <code>jQuery.ready()</code> function, except that you must also pass in a string representing the ID of the element that should have the function call attached.</p>
<p>This version of the plugin works well for me in testing (and on this page), but it has not been exhaustively tested. I would appreciate your comments on the idea and the implementation.</p>
<h2>Download</h2>
<p>Get <a href="http://www.thunderguy.com/semicolon/plaintext/jquery.elementReady.js">jquery.elementReady.js</a> here. Then read on for instructions and details.</p>
<h2>Documentation for jQuery.elementReady()</h2>
<p><!-- Generated by http://jquery.bassistance.de/docTool/docTool.html<br />
--><span class="fn">$.elementReady( <span class="arg-type tooltip" title="A string of characters.">String</span> <span class="arg-name tooltip" title="string ID of the element to wait for">id</span>, <span class="arg-type tooltip" title="A reference to a Javascript function.">Function</span> <span class="arg-name tooltip" title="function to call when the element is ready">fn</span> ) returns <span class="type"><span class="tooltip" title="A jQuery object.">jQuery</span></span></span>
<div class="more">
<div class="desc">While a page is loading, call a given callback function as soon as a specific<br />
element is loaded into the DOM, even before the full DOM has been loaded.<br />
Executes the function within the context of the element. This means that when<br />
the passed-in function is executed, the &#8216;this&#8217; keyword points to the specific<br />
DOM element.<br/><br/>The function returns &#8216;this&#8217;, so you can chain multiple calls to<br />
elementReady(). (Not that there&#8217;s much benefit in doing that.)<br/><br/>One argument is passed to the callback: a reference to the jQuery function.<br />
You can name this argument $ and therefore use the $ alias even in<br />
noConflict mode.<br/><br/>If the element has not been found by the time the DOM is fully loaded, then<br />
the function will not be called.</div>
<div class="example">
<h3>Example:</h3>
<p>Change the source of a specific image as soon as it is loaded into the<br />
DOM (before the whole DOM is loaded).</p>
<pre>$.elementReady('powerpic', function(){
    this.src = 'powered-by-jquery.png';
});</pre>
</div>
<div class="example">
<h3>Example:</h3>
<p>If you want to have the jQuery object instead of the regular DOM<br />
element, use the $(this) function.</p>
<pre>$.elementReady('header', function(){
    $(this).addClass('fancy');
});</pre>
</div>
<div class="example">
<h3>Example:</h3>
<p>Chain multiple calls to $.elementReady().</p>
<pre>$.elementReady('first',  function(){ $(this).fancify(); })
 .elementReady('second', function(){ $(this).fancify(); });</pre>
</div>
<div class="example">
<h3>Example:</h3>
<p>Use the &#8216;$&#8217; alias within your callback, even in noConflict mode.</p>
<pre>jQuery.noConflict();
jQuery.elementReady('header', function($){
    $(this).addClass('fancy');
});</pre>
</div>
<div class="example">
<h3>Example:</h3>
<p>Change the polling interval to 100ms. This only works if <code>$.elementReady()</code> has not yet been called.</p>
<pre>$.elementReady.interval_ms = 100;</pre>
</div>
</div>
<h2>Why?</h2>
<p>I <a href="http://groups.google.com/group/jquery-dev/browse_thread/thread/263b3ee5c6e4e63e/896356fc3f78c65d">asked about this feature on the jQuery development list</a>, and to my surprise nobody had made much of an effort to do this. jQuery author John Resig has some <a href="http://groups.google.com/group/jquery-dev/browse_thread/thread/6843a97e9e12df5c/8629dd8bb941fec1">magic code that makes this happen automatically</a>(!), but including this &#8220;auto&#8221; feature in the core would complicate things unnecessarily. He suggested that a plugin should be written. So here it is.</p>
<h2>Implementation notes</h2>
<p>The jQuery.elementReady() function is interesting because it&#8217;s quite unlike most other jQuery methods. It&#8217;s not a method applied to a normal jQuery object; it can&#8217;t be, because a jQuery object is normally constructed from a DOM element (or list of DOM elements). But in this case, all we have is the ID of an element: the element itself does not yet exist.</p>
<p>When the function is called, the matched element is passed to the callback function as a raw DOM element rather than a jQuery object. This is consistent with other event handlers, and is also the most efficient approach.</p>
<p>The function returns the global jQuery object, so you can chain calls to <code>elementReady()</code>.</p>
<p>My implementation is inspired by the Yahoo UI library&#8217;s <a href="http://developer.yahoo.com/yui/docs/YAHOO.util.Event.html#onAvailable">onAvailable()</a>, but is simpler (and, to be sure, less well-tested). YUI also includes a related method called <a href="http://developer.yahoo.com/yui/docs/YAHOO.util.Event.html#onContentReady">onContentReady()</a>, which is supposed to fire when the element <em>and</em> all its content is available. I have not implemented that feature yet, but I think I may end up doing so in elementReady(). This will make it more robust yet still dead simple to use.</p>
<p>I thought about somehow overloading the original <code>ready()</code> function but thought that would just add complexity for no real benefit.</p>
<h2>Things to do</h2>
<p>This is an early version of the plugin. I&#8217;m afraid I have tested it only on Windows browsers, but I have had no problems with it yet. The main thing to do is to test it more thoroughly. As I mentioned above, I also might decide to modify the function to check that the entire contents of the target element are loaded, not just the element itself.</p>
<h2>Have your say</h2>
<p>Apparently blog posts get x% more comments if the last heading says &#8220;Have your say&#8221;. So have <em>your</em> say! Is this useful? How can it be improved?</p>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=108&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_108" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/vbH1RMWHLgs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2007/08/14/elementready-jquery-plugin/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Share This (jQuery): a WordPress plugin</title>
		<link>http://www.thunderguy.com/semicolon/2007/07/30/share-this-jquery-a-wordpress-plugin/</link>
		<comments>http://www.thunderguy.com/semicolon/2007/07/30/share-this-jquery-a-wordpress-plugin/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 02:56:04 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Web publishing]]></category>

		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/2007/07/30/share-this-jquery-a-wordpress-plugin/</guid>
		<description><![CDATA[I have made a useful modification to Alex King&#8217;s excellent Share This WordPress plugin. Share This adds a nice popup to your posts allowing readers to easily submit the post to any number of social networking and news sites. The original version relies on the large Prototype JavaScript library, which adds to the download size [...]]]></description>
			<content:encoded><![CDATA[<p>I have made a useful modification to Alex King&#8217;s excellent <a href="http://alexking.org/projects/wordpress">Share This WordPress plugin</a>. Share This adds a nice popup to your posts allowing readers to easily submit the post to any number of social networking and news sites. The original version relies on the large <a href="http://www.prototypejs.org/">Prototype</a> JavaScript library, which adds to the download size for the page. Share This only uses a sprinkling of its features, so I wanted to replace it with something smaller.<span id="more-103"></span></p>
<p>I have recently redesigned this website; it now uses the marvellous <a href="http://jquery.com/">jQuery</a> library for some of the effects. So to avoid having to include Prototype, I modified Share This to use jQuery instead of Prototype. If you want to reduce the time taken for your pages to load, you could replace the original Share This with my version; this is is especially good if you are already using jQuery.</p>
<p>To use this version of Share This, first install the original <a href="http://alexking.org/projects/wordpress">Share This</a> (version 1.4), and then <a href="http://www.thunderguy.com/semicolon/plaintext/share-this.php"><strong>download Share This (jQuery)</strong></a> and replace the original share-this.php file with the new one. Read on for more details.</p>
<p>Here are the changes I made, compared to version 1.4 of Share This.</p>
<p><strong>Use jQuery instead of Prototype.</strong><br />
jQuery is included in WordPress 2.2 and later versions. For earlier versions, you should <a href="http://jquery.com/">download jQuery</a> and copy it into the <code>wp-includes/js/</code> directory in your WordPress installation. Make sure the file name is <code>jquery.js</code>.</p>
<p><strong>Behave nicely if somebody tries to load the plugin file directly.</strong><br />
If someone tries to load the share-this.php file directly, just show a blank screen. The original version shows an error message.</p>
<p><strong>Use a sliding &#8220;roller blind&#8221; effect when showing the Share This popup window.</strong><br />
Just because it looks nice.</p>
<p><strong>Remove some unused code.</strong><br />
Because it was unused.</p>
<p>My modifications are in the public domain.</p>
<p>Now, there is one other change. The position of the popup window is not the same in all browsers; in some browsers (such as Firefox) the popup appears below the &#8220;Share This&#8221; link; in others (such as Internet Explorer) the popup appears just to the right of the link. I don&#8217;t mind the inconsistency: both variations look nice in themselves.</p>
<p>I thought about using the <a href="http://jquery.com/plugins/project/dimensions">Dimensions plugin</a> to emulate the original Share This more exactly. But in the end I decided to keep things simple and avoid introducing yet another file.</p>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=103&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_103" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/gwOXIwRe4Zs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2007/07/30/share-this-jquery-a-wordpress-plugin/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Be careful with JavaScript variable declarations</title>
		<link>http://www.thunderguy.com/semicolon/2007/07/30/be-careful-with-javascript-variable-declarations/</link>
		<comments>http://www.thunderguy.com/semicolon/2007/07/30/be-careful-with-javascript-variable-declarations/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 01:32:05 +0000</pubDate>
		<dc:creator>Bennett</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Risks]]></category>

		<guid isPermaLink="false">http://www.thunderguy.com/semicolon/2007/07/30/be-careful-with-javascript-variable-declarations/</guid>
		<description><![CDATA[Make sure you declare all your JavaScript variables exactly once. Otherwise it&#8217;s easy to introduce bugs that are hard to diagnose, especially if you&#8217;re used to programming in a C-like language such as Java, C++ or C#.
Local variables in JavaScript are scoped to the function they are in, not to their containing block. This is [...]]]></description>
			<content:encoded><![CDATA[<p>Make sure you declare all your JavaScript variables <em>exactly</em> once. Otherwise it&#8217;s easy to introduce bugs that are hard to diagnose, especially if you&#8217;re used to programming in a C-like language such as Java, C++ or C#.<span id="more-107"></span></p>
<p>Local variables in JavaScript are scoped to the function they are in, not to their containing block. This is different to C++, Java and similar languages. One of the consequences of this is that a <code>var</code> statement by itself generally does nothing at all, unlike in C++, Java, etc. Here&#8217;s a contrived example.</p>
<pre class="code"><code>for (var i = 0; i &lt; 5; ++i) {
	<span class="bad-code">var t;</span>
	// If i is even then set t to "even"
	if (i % 2 == 0) {
		t = "even ";
	}
	// If t is not set then i must be odd
	if (!t) {
		t = "odd ";
	}
	document.write(t);
}</code></pre>
<p>After one iteration, <code>t</code> contains the value &#8220;even&nbsp;&#8221;. In the second iteration, a Java or C++ programmer might expect the <code>var t</code> to initialise the variable <code>t</code> again, so <code>t</code> will subsequently end up with the value &#8220;odd&nbsp;&#8221;. But actually the <code>var t</code> has no effect and <code>t</code> always has the value &#8220;even&nbsp;&#8221;. The program above writes &#8220;even even even even even&nbsp;&#8221; to the document.</p>
<p>The fix is simple. Instead of an ineffectual <code>var t</code>, use <code>var t = null</code> to explicitly set the value of <code>t</code>. In fact, you could follow Douglas Crockford&#8217;s <a href="http://javascript.crockford.com/code.html">JavaScript coding conventions</a>, which recommend:</p>
<blockquote><p>All variables should be declared before used. JavaScript does not require this, but doing so makes the program easier to read and makes it easier to detect undeclared variables that may become implied globals.</p>
<p>The var statements should be the first statements in the function body.</p>
<p>&#8230;</p>
<p>JavaScript does not have block scope, so defining variables in blocks can confuse programmers who are experienced with other C family languages. Define all variables at the top of the function.</p></blockquote>
<p>Following this recommendation, you&#8217;d write the following code, which produces &#8220;even odd even odd even &#8221; in proper accordance with the laws of parity.</p>
<pre class="code"><code><span class="hilite-code">var t;</span>
<span class="hilite-code">var i;</span>
for (i = 0; i &lt; 5; ++i) {
	<span class="hilite-code">t = null;</span>
	// If i is even then set t to "even"
	if (i % 2 == 0) {
		t = "even ";
	}
	// If t is not set then i must be odd
	if (!t) {
		t = "odd ";
	}
	document.write(t);
}</code></pre>
<p class="akst_link"><a href="http://www.thunderguy.com/semicolon/?p=107&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_107" class="akst_share_link" rel="nofollow">Share This</a>
</p><img src="http://feedproxy.google.com/~r/semicolon/~4/QV8cEWz6mz0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.thunderguy.com/semicolon/2007/07/30/be-careful-with-javascript-variable-declarations/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
