Wednesday, June 24, 2009

Where did my global variable go?

Came accross this problem the other day - thanks to David! Wanted to test if the javascript variable had already been defined (globally) and supply a default value if it wasn't defined.

  var name = "Rob";

  function testScope() {
    document.write("<p>Before IF. My name is " + name + "</p>");
    if (!name) {
      var name = "Rob2";
    document.write("<p>After IF. My name is " + name + "</p>");

  document.write("<p>Before testScope(). My name is " + name + "</p>");
  document.write("<p>After testScope(). My name is " + name + "</p>");

Here is the result.

   Before testScope(). My name is Rob

   Before IF. My name is undefined

   After IF. My name is Rob2
   After testScope(). My name is Rob

Unfortunately, simply declaring the var again (var name = "Rob2";) created a locally scoped version of the var for the entire function - even though I only declared it within the IF. The fix was easy: just remove the var.

  var name = "Rob";

  function testScope() {
    document.write("<p>Before IF. My name is " + name + "</p>");
    if (!name) {
      name = "Rob2";  // No var!
    document.write("<p>After IF. My name is " + name + "</p>");

  document.write("<p>Before testScope(). My name is " + name + "</p>");
  document.write("<p>After testScope(). My name is " + name + "</p>");

Here is the result.

   Before testScope(). My name is Rob

   Before IF. My name is Rob

   After IF. My name is Rob

   After testScope(). My name is Rob

It had a value all along!

Remember that global variables belong to the window object, thus the following would also have worked.

  if (! { = "Rob3";

My Favourite way to Google is my new favourite way to Google. It mashes results from Google (including blog and image search), Wikipedia, and YouTube. It shows results as you type and lets you keyboard through the results using the cursor keys.

Use the nightly build site to sample the most recent functionality.

Update: 4/08/2009 11:34:43 AM
I still like keyboardr, but Google has their own version, which I like more: Google Search with Keyboard Shortcuts on their experimental page. Here is a sample query.

Sunday, June 07, 2009

Tools for Java and Web Development

A programmer needs good tools, and a good memory in order to remember what things to re-install each time they get a new machine. I lack the second, so this page is my crutch. :)

15/06/2009 12:15:36 PM. Added new row to Firefox v Chrome comparison: Bookmarks/Search. This article is copied from my Google App Engine site.


Eclipse is my IDE of choice, though I prefer Dreamweaver for HTML editing. Eclipse is fantastic for code editing, but sucks for HTML editing when compared to Dreamweaver. Dreamweaver lets you customize existing keyboard shortcuts.

Here is a list o the Eclipse plugins I use.

  • Andrei Loskutov has a set of Any Edit Eclipse plugins. I always install AnyEdit Tools so that I can do things like various on-save actions like remove trailing spaces. Update site:
  • StartExplorer is a very handy plugin for opening Windows Explorer from any path in Navigator view (or the like) or copying the qualified name of a resource to the clipboard: a couple of tasks I have wanted to perform from within Eclipse enough times to make this plugin well worthwhile. Update site:
  • Multi clipboard and Tiny HTML. Tiny HTML gives a few quick ways to insert common HTML tags, but it is Multi-Clipboard that I really like! Multi-Clipboard gives you a clipboard view and lets you copy to/paste from multiple clipboard slots. Many is the time I wished to copy two or more items simultaneously to the clipboard, and this plugin is perfect for it. It is something UltraEdit has had since I started using it and which I have ever since wished to see in Eclipse! I tend to keep a couple of often used snippets in the first two slots because it is so easy once you assign the right short-cuts to it. Update site:
  • FindBugs is a very important tool for warning you of many common errors and helps you write better code. Update site:
  • CheckStyle is another very important tool: it helps you write consistent code according to whatever common practices you or your team accept. Update site:
  • Aptana Studio is a stand-alone product that also offers a plugin for Eclipse that has a very nice Javascript Editor, plus a ton of other features. Not a necessary plugin, and I find it a bit heavy and occasionally flaky. Update site:
  • Dead. Column Mode for Eclipse was my all time favourite for about two months: it gave UltraEdit style column mode editing, but now (as of Eclipse Version: 3.4.2) it doesn't work for some reason. Eclipse 3.5 will have it's own column edit mode of some sort though, so here's hoping!
  • Dead. Eclipse Bookmarks was a favourite of mine for making and navigating around quick bookmarks with simple keyboard shortcuts. At last something that makes the Bookmarks view actually useful. I say "was", because the site is dead.


Mozilla's Firefox is in a very close fight with Google's Chrome for being my browser of choice.

Feature Firefox Chrome
Plugins Huge array of very useful plugins a.k.a. add-ons. Favourite plugins: Xmarks and Firebug. None, yet. Xmarks is promised but not here yet. The in-built developer tools don't seem to be as useful as Firebug.
Memory Despite various tips on how to reduce memory usage, Firefox (2 and 3) still becomes a sluggish memory hog after a few hours of usage. This may be the fault of an errant plugin, but there is no easy way to tell when you are using 20+ add-ons. One process per tab and a process manager that shows per tab and per plugin memory use with the ability to end either.
Tabbed UI So faster and more flexible than IE. Can split tabs with Split Browser plugin. Ditto IE comment, but better than Firefox: with Chrome you can rip tabs off into their own window - much better than Firefox's Split Browser add-on.
Bookmarks/Search Has a very functional bookmarks sidebar, enhanced by a few good plugins. Lets you access bookmarks and quick searches from the address bar.

Brilliant search technique from the address bar: Google search by default; type "" and press tab to search

But only shows three bookmarks results at a time when you type in the address bar. What's up with that??

What about Javascript engines? To be honest, I haven't noticed a difference - both seem to be great.

I usually make sure to have Safari, Opera and IE (or the sometimes buggy Standalones) handy for testing all platforms.

Here is a list of the Firefox add-ons I use.

  • Adblock Plus to get rid of pesky ads.
  • All-in-One Sidebar groups things like bookmarks and add-ons into one side bar instead of pop-ups etc.
  • Better Gmail 2 allows you to customize Gmail.
  • Boox allows me to locate a bookmark when I search for it. I.e. in the bookmark view I can type "java", see all matching bookmarks, right click on it and select "locate" and see where in my folder hierarchy the bookmark actually is.
  • CacheViewer view my cache.
  • CustomizeGoogle allows you to customize lots of different Google tools like search, maps, mail etc.
  • Download Statusbar shows status of downloads in the status bar - much better than Firefox's downloads view.
  • FEBE backs up your add-ons or entire profile.
  • Fetch Text URL opens plain text URLs.
  • Firebug is the tool for debugging CSS and Javascript.
  • Firecookie is a cookies add-on for Firebug.
  • FireGestures brings mouse gestures to Firefox. Now I use StrokeIt on Windows because it offers mouse gestures for all programs.
  • Xmarks Bookmark Synchronizer (formerly FoxMarks) synchronizes your bookmarks and passwords across Firefox/IE on any machine. Soon support will be added for Chrome too, hopefully.
  • FoxyTunes controls your music player from within Firefox.
  • Gmail Manager is a Gmail notifier for multiple accounts.
  • Greasemonkey allows you to customize the way a web page displays using small bits of JavaScript.
  • gui:config adds an advanced settings panel with easy access to options normally hidden behind cryptic about:config options.
  • IE Tab can view any tab in IE.
  • iMacros for Firefox can record/play macros which you can bookmark and add your own javascript to. Inserting Javascript is a bit awkward though. You can't insert Javascript into a macro: you have to write Javascript that launches (and manipulates data from) macros. Has one macro language.
  • It's All Text! lets you edit text area content in your favourite text edit.
  • LastTab allows tab navigation (with tab preview) in a most recently used manner.
  • NoScript allow active content to run only from sites you trust, and protect yourself against XSS and Clickjacking attacks. This one is beginning to annoy me though, because I forget to allow a site and wonder why it isn't working!
  • PDF Download gives better control of PDF files within Firefox.
  • Selenium IDE is another macro add-on that allows you to bookmark the macros and use Javascript. Unlike iMacros, you can insert Javascript into the macros. Selenium allows you to write the macros in a wide variety of client languages.
  • Show Keywords creates short-cuts to site specific searches. Not really needed any more though - use Smart Keywords.
  • Speed Dial puts 9 (by default) shortcuts to your favourite URLs on a newly opened tab. Lets you open any of them with keyboard shortcuts (control+1-9 to open the URL in the current tab; control+shift+1-9 to open the URL in a new tab).
  • Split Browser brings split screens to Firefox. Not as nice Chrome's ability to rip tabs off into their window.
  • StatusbarEx adds useful stats to the status bar, like current memory and bandwidth usage for Firefox.
  • Tab Clicking Options gives you a bunch of extra tab actions when you click on a ... tab.
  • Taboo lets you save a page for later (taking a screen shot, and using the Session Saver code to remember scroll location and form fields).
  • TagSifter lets you search through bookmarks by tags. Has a tool to add tags to existing bookmarks based on the folder hierarchy.
  • TwitKit is a Twitter sidebar, TwitterBar posts to Twitter from the address bar, TwitterFox is a FireFox Twitter client with a nice looking unobtrusive UI.
    • I have given all of these up in favour of Twhirl, an Adobe Air desktop client. I was using TweetDeck for a while (another Adobe Air desktop client) but it doesn't show new messages in the pop-up - which Twhirl does (and TwitterFox, but TwitterFox shows the pop-up only within FireFox).
  • Undo Closed Tabs Button re-opens tabs you just closed.
  • Video DownloadHelper lets you download streaming videos.


As far as database clients go, DBVisualizer is my client of choice. It works against every DB I know of and has an impressive feature list. The personal version is free, but you can continually renew an evaluation license of the full version. One of my favourite aspects is the references view that gives you a great ERD. Doesn't run PL/SQL.

Eclipse comes in-built with some good database tooling too - a very easy way to execute selected pieces of SQL. Doesn't run PL/SQL.

Command line/Scripting environment

I think it is important as a programmer to get cosy with a Linux command line environment and a scripting language and build up a library of useful custom tools and utilities. I use Cygwin, a Linux-like environment for Windows, with PuttyCyg as the terminal. I use bash and have written over 50 bash scripts over the past few years to do various jobs like searching and file/project management tasks. Some of my favourite ones are shared on my blog.

Text editor

UltraEdit has always been my "go-to" editor. It has great basic to advanced functionality like column edit mode and a diff function. It supports roll-your-own macros, templates, Javascript scripts, tags, syntax highlighting and function lists. About the only thing I wish it had was Textpad's ability to automatically highlight all instances of the currently selected token.

For the times when I am on a command line, I made it my business to get comfortable with vim. You get Gvim from the same place. Vim has a steep learning curve if you are a Windows user, and there is a full on religious war between lovers of vim and Emacs, but I chose vim because at some level it seemed easier. I have since found that vim is quite extensible: you can configure so many aspects of it, and its macro system is great too. I put up some vim tricks I find on my blog.

I am also just beginning to teach myself Jedit, which I want to use so that I can write macros in Java!