Thursday, December 22, 2011

Introducing HTML5

The other day I was at the bookstore (my wife had organized a book fair for my daughter's school) and decided to check out the books about HTML5. I had read a few things in the past but wanted a deeper understanding of what changes it brings to writing web applications. I grabbed the five books I could find on the subject and started browsing through them. Introducing HTML5 (2nd Edition) hooked me from the introduction and I ended up buying it.

The introduction provides a history of the development of HTML5. I had thought of HTML5 as the evolutionary successor to HTML4 but was interested to find that not for a small group at Opera lead by Ian Hickson it almost wan't a reality. W3C was pushing XHTML 2.0 as the successor to HTML 4 which required XML syntax and broke backward compatibility. Whereas the Opera group took an approach more focused on current browser behavior and looked to add items to simplify web development.

The first chapters deal with the changes to overall structure of an HTML document and text on the page. There are chapters on the new form elements, video, the new canvas tag and offline storage. There are a couple of chapters on geolocation, messaging, and sockets that I haven't gotten to yet. Finally there is a chapter on backward compatibility and using polyfills to make sure your pages work with earlier browsers.

Overall it's a great overview of the new stuff in HTML5, why the new tags were added and when to use them.

Saturday, October 22, 2011

If Statements

Its amazing how something simple like an if statement can have so many different permutations and different options. Here are some of my general rules to follow:
  • Always put the true condition first. Some people prefer the shorter block first (or last) but I think if you consistently put the true condition first it leads to less confusion when looking at the code later.
if (trueCondition) {
    DoSomething();
} else {
    DoSomethingElse();
}
  • Always take advantage of short-circuit evaluation and put the most cached or simplest conditions first.
if (localVar && string.IsNullOrEmpty(s) && expensiveTest()) {
  • Don't explicitly return true or false
// Argh, don't do this.
if (condition) {
  return true;
} else {
  return false;
}
// Just return the result of the condition
return condition;

// Or even worse I've seen this:
bool value = condition ? true : false;
// Just set the value:
bool value = condition;
  • Unless it is a one line if statement always bracket the blocks. This protects against the case where you write the one line case and later another developer has to add another statement to the if condition and doesn't notice the missing brackets and then introduces a bug. For example this is ok:
if (arg == null) return;
but not this:
if (condition) 
  x = 4;
Because too often I've seen another developer later add a line of code thinking it's part of the branch:
if (condition)
    x = 4;
    y = 6;

Tuesday, June 21, 2011

Browser Session Management

First off, I admit I have a problem. I typically have 5-6 browser windows open with 10-15 tabs per window and that's just with one brand of browser. I sometimes have that many tabs/windows with two of the big three (IE, Firefox or Chrome) browsers going at the same time.

There are many reasons why I end up with so many open tabs:
  • I open a new window to search on something and open the links in tabs
  • I get easily distracted. I start to reading a page then switch to something else and don't want to close the tab knowing I will come back to it.
  • There are some tabs that are semi-permanently open (like gmail, Facebook, Twitter, Amazon Cloud Player).
  • I use open pages as a todo list.
Usually this isn't that big of a problem but it can become one. The biggest problem is memory usage. Over time all three browsers seem to leak memory (some worse than others - Firefox I'm looking at you) forcing restarts (and occasionally Windows wants to install updates and reboot). Having the browser re-open without losing all of my open tabs is a big selling point. 

Here is my take on the big three and session management.

Internet Explorer

IE is the worst of the three when it comes to session management. IE8 and 9 added a link to 'Reopen last session' (accessible on new tabs) but as far as I can tell it assumes all of your previously open tabs were in one window. If you do have multiple windows open and restart your computer only the last window closed is restored (which always seems to be the one with the fewest tabs). If your computer hard crashes and you restart IE it does ask if you would like to restore the previous session and this does reopen all windows and tabs. So full session information does seem to be stored somewhere just not surfaced to the end user.

Firefox

Early on Firefox itself did not have great session management but it had good add-ons like "Session Manager". If I noticed things running slow or saw Firefox memory usage spiking in Task Manager I could kill the browser and with Session Manager reload all of the tabs when it restarts. Firefox has now built this in as option to restore all open tabs.

Chrome

With Chrome I ended up having on of those 'doh' moments. I knew Chrome tracked all of the opened windows and tabs because if I restarted from a hard shutdown it would offer to Restore all of them. The problem was that if I noticed Chrome eating up a lot of memory there didn't seem to be a clean way to close all of the windows and completely restart the browser session. Since each tab spawns its own process there was not a single process I could kill like Firefox in the Task Manager. And if you clicked on the X in each browser window like IE the last window became the only one restored. Then I don't remember where I read it but I found out if you use the 'Exit' menu option from the Wrench icon it will close and remember all of your open windows and tabs. It was one of those 'aha' moments that made live easier. 

Friday, June 17, 2011

Scratch and Squeak

Some of my first programming experiences were typing BASIC programs into my TI-99/4A home computer copied from computer magazines. The whole process was madly frustrating. There was no editor, you had to exactly copy the lines into the console and then you could save the program to an analog cassette. If you were lucky you could reload your program from the cassette at a later date. I remember trying to create a Star Trek game and gave up after taking 2 weeks just to create the logo screen. Despite all of this there was something about programming and I was hooked.

Now many years later I am a father of three and hoping to pass along some of that love of programming to my kids. Looking around its amazing the options out there to get them started . When I was starting out the options were either BASIC or Logo. Logo had some neat ideas and I played with it a bit, but moving the turtle around never got me hooked. Now this Wikipedia page lists 71 educational programming languages.

As a past Smalltalker, the most interesting to me of these appear to be Squeak and Scratch. Squeak is a modern, open source implementation of the Smalltalk programming language and environment. Scratch is built using Squeak under the covers but is a visual programming language where programs are built by snapping together blocks. I think my daughters will like the ability to quickly create an animated story and control the action. I also have a first generation Lego Mindstorms kit but I think Scratch will be a good starting point.

Thursday, June 16, 2011

Smalltalk Extensions

In a previous lifetime I worked for a Smalltalk vendor and my last two posts (here and here) got me thinking about all of the ways that you could hack the Smalltalk environment. In Smalltalk, except for a few primitives, all of the base libraries source code is present and editable in your environment. You can edit the default implementations or easily add additional functionality to base classes. This can lead to all sorts of interesting side effects.

I remember working with one customer that had occasional lock-ups in their development environment which they were blaming on us. These lock-ups were intermittent and hard to reproduce. Well one day while in their environment I was able debug right after a lock-up. I started stepping through some code in the base collection class that I didn't recognize. Turns out they had overridden the inherited Sort function and implemented their own (might have been a quicksort I can't remember) along with a very subtle bug. Given the right list size and starting conditions the code would enter an infinite loop. Fixing their sort function fixed their lock-ups and contributed to my reluctance to ever touch base libraries.