Rules For Living: Out Before In

This rule is so simple, I’m amazed how often so many people get it wrong. Out before in.

Getting on a bus? Let the people out before you get in. Getting on an elevator? Same. Heading into a Starbucks? Let the nice lady with her embarrassingly complicated coffee-like drink out before you barge in.

Do you hear me? Out before in.

Share and Enjoy:
  • Twitter
  • del.icio.us
  • Facebook
  • Print

JavaScript: Numbers to Strings

Javascript is pretty casual about strings and numbers. Because javascript is an untyped language, it’s easy to get used doing things like:

var aString = "kb" + 63; // results in string "kb63"
Instead of choking, javascript will treat the number 63 as a string and concatenate them together for you.

Sometimes, you really need a string or a number. For example, you might have a database key that you want to use as an array index:

var dbKey = 546735289; // some surrogate key out of the database.
var dbObject = {
   name: "Kenneth",
   age: "old",
   likes: "beer"
};
var theArray = new Array();
theArray[dbKey] = dbObject;

But now, theArray.length is 546735290 – one more than the index. Javascript arrays are sparse, so this might not cause any memory problems since memory should only be allocated for the single array element at 546735289. However, if you loop through the array using a for loop theArray.length times, performing some expensive operation inside the loop – such as a complex DOM manipulation – you might get into trouble.

In this case, you’d be better off using a named property instead of a numeric array index. To convert a number to a string:

    // use the toString method
    dbKey.toString

    // use the String() constructor
    String(dbKey)

    // concatenate a number with empty string. Crude and ugly
    dbKey + ""

What if you want to go the other way, from a string to a number? The cheap and easy way is to do some math on the string:

    var myString = "1000";
    var myNumber = myString * 1

Don’t try and use the + operator:

    // won't work. javascript thinks concatenate
    var myString = "1000";
    var myNumber = myString + 0; // results in the string 10000
Share and Enjoy:
  • Twitter
  • del.icio.us
  • Facebook
  • Print

GTK Key Bindings

When I upgraded my Linux boxes to Firefox 1.0 (I’m now at 1.5), I finally made the jump to using the GTK 2.0 libraries. The only drag is that the default key bindings are more Windoze like. Ctrl-a selects all, rather than moving to the beginning of the line, and other annoyances to the enlightened.

To fix this, create a ~/.gtkrc-2.0. Add the line:

gtk-key-theme-name = "Emacs"

Restart any applications using GTK. You do not have to restart X.

The GTK Tutorial has lots of info on all the sundry rc files.

Share and Enjoy:
  • Twitter
  • del.icio.us
  • Facebook
  • Print

Beginning Running

I ran a lot as a young person. Hardly at all as a young adult. Sometime around my mid-thirties I started running again; Running is an efficient and enjoyable way to stay fit. I enjoyed it enough to compete in a few road races, but I overdid it and cultivated a series of injuries: plantar fasciitis and shin splints. Injuries like these are almost totally avoidable, if you take it easy, listen to your body, and remember that you aren’t twenty any more.

I took the last year off to recover, and now I have to start from scratch. Here’s a plan to get you started. read more…

Share and Enjoy:
  • Twitter
  • del.icio.us
  • Facebook
  • Print

Drygleaning

Word Fugatives book cover

Word mashups are all the rage: craptastic. ginormous. wiktionary.

In spite of the richness of the English language, people are plugging its holes in fun and inventive ways. My new favorite? Drygleaning: picking through the dirty laundry for something clean enough to wear (Every one does this, right?).

Word Fugatives is a whole book full of these discoveries.

Share and Enjoy:
  • Twitter
  • del.icio.us
  • Facebook
  • Print