codeRambler Ramblings on code, the web, and technology

How much is too much caffiene?
Wednesday August 15th 2007, 5:13 pm
Filed under: Ramblings

A news report recently surfaced on the BBC news website about a 17 year old girl who reportedly overdosed on caffiene.

After downing 7 double espressos in her family-run sandwich shop, she started to suffer side effects including laughing and crying before being sent to hospital for observation (she was later discharged home after a few hours).

Regardless of whether she thought they were single or double shots of espresso (the article indicates she thought they were singles)… why was a 17 year old drinking 7 cups of espresso in the first place?!

I love espresso but I never managed to get such an extreme reaction from over indulging (although I did have a fun morning after drinking 4 doubles once).

So I listed my top 5 beverages and found out just how much caffiene is in each of them:

Name Amount
Coca-Cola Zero 355 34.5 10
Diet Coke 355 45.0 13
Coffee (Instant) 237 57.0 24
Starbucks Grande Latte 473 116.0 25
Coffee (Espresso) 44 77.0 175

So 7 cups of single shot espresso would be about 540 mg of caffiene. Double that (she was drinking double shots of espresso) and you’re talking about 1000 mg of caffiene. The same as scoffing 5 double-strength No-Doz pills!

I’m off for a coffee.

Comments Off

Tips on converting strings into numbers
Monday August 06th 2007, 10:29 am
Filed under: Javascript, Web development

When you are reading a value from a text input, the value is returned as a string. This is not really a problem for Javascript in that it’s loosely typed and converting the string into a number is really straightforward.

As with most things, there are several ways to do this conversion. I’ve used parseInt() a lot in the past to convert a string into a number (although strictly speaking I am converting to an Integer rather than just a Number when using parseInt()), but recently ran into some strange behaviour that I wanted to understand.

var l_sValue = "07";
alert(parseInt(l_sValue)); // alerts 7

This seems to work fine, the string represents the number 7, and the parseInt() correctly parses it into the integer 7. Now we change the string value and something strange occurs:

var l_sValue = "08";
alert(parseInt(l_sValue)); // alerts 0

This is obviously not the expected result! The solution is to use parseInt() correctly, and pass through both parameters (the second parameter is the number base to use).

If you do not pass in the second parameter (the base, or radix) then Javascript will default to base 10, unless:

  • the string starts with ‘0′, then it will default to base 8 (octal)
  • the string starts with ‘0x’, then it will default to base 16 (hexadecimal)

Because the string we used above started with ‘0′, Javascript defaulted to use base 8. The reason we see 0 being returned from parseInt(’08′) is because 8 is not a valid number in octal (we would count in octal 0, 1, 2, … 6, 7, 10, 11, 12, … 17, 20, 21, …).

Now look what happens when we pass in the second parameter and use (the more familiar) base 10:

var l_sValue = "08";
alert(parseInt(l_sValue, 10)); // alerts 8

Lesson for the day: always pass in the base when using parseInt() to parse a string into an integer.

Memory leaks and closures in Javascript
Thursday August 02nd 2007, 11:57 am
Filed under: Javascript, Web development

The topic of memory leaks reared it’s ugly head again recently and I tracked down several good articles and tutorials that discuss and explain some of their causes (and how to prevent them) quite well.

One of the main symptoms of memory leaking is performance slowdown in the browser (this affects all browser, some to a greater degree than others). Investigations of this type of problem invariably lead to Javascript closures – a very powerful feature of Javascript that is very easy to abuse.

For those looking for an explaination of what closures (aka. continuation by some) are and how they are so important to Javascript, take a look at this informative article at

There is a working account of one person’s battle with closure related memory leaks titled IE: where’s my memory?. This article links to many good resources, and has a very well thought out explaination as to why closures are so useful in the first place.

Of course there is a formal description at the MSDN Library as well (complete with diagrams, code examples and patterns).

There is a good tutorial on how to avoid Javascript memory leaks at Jack Slocum’s Blog. This specifically mentions problems experienced with the Dojo Toolkit, (along with Prototype) and even WordPress (used to power this blog).

After reading all those articles, no doubt you will be itching to refactor your existing code!

Books: Mortal Engines by Philip Reeve
Wednesday August 01st 2007, 12:00 pm
Filed under: Books

My commute leaves me about an hour or so every day to indulge in a good read… and whilst I enjoy many different kinds of book, I have found “youth” children’s books to be ideal (given the interruptions you inevitably suffer on a train). A colleague recommended some books by Philip Reeve – a new author (for me), and I’ve been reading his “award-winning Mortal Engines quartet”.

I’ve always had difficulty deciding what to call a series of 4 books, and would normally attempt to refer to them as a quadrilogy – but in this case I was saved by the book cover referring to them as a quartet.

These books are set in a distant future on an Earth that has been reduced to ruin by war (the “Sixty Minute War” was the one that caused the most damage). The ground is a wasteland and resources are scarce. Populations roam around on moving cities (named after cities of today – including London, Brighton and Anchorage). These cities follow the principle of Municipal Darwinism where it’s a “city-eat-city” world full of predator cities chasing down smaller cities and suburbs (also on wheels) and “eating” them – dismantling them for their metal and enslaving their captured inhabitants.

The technology used in the books is a little like that of China Melvielle’s “Steam Punk” genre books and Jules Verne styled contraptions. There are air ships (reminiscient of Zepplins) and submarines to go along with flying “Stalker birds” (dead birds that have been reanimated into some kind of cyborg equivalent) and sand boats that sail the wind across the deserts.

This quartet was a great read. Whilst each book nicely leads into the next, each can be read independently (although it would be a little difficult to follow the later books if you hadn’t completed the first book). Unlike many books, the author doesn’t spend ages updating you on “what has already happened” – this is a welcome relief.

I have now bought some more books by Philip Reeve and look forward to reading more of his work. I hope you do too.

Comments Off