Wednesday, June 29, 2011

strange programmer habits : literal comes first in a comparison

Consider this JavaScript code:
function foo( aString ) { if( ‘:error’ == aString ) { handleError(); } }
Why put the variable reference after the string literal? So you don't accidentally turn the comparison into an assignment. Look at this code:
function foo( aString ) { if( aString = ‘:error’ ) { handleError(); } }
Do you see the error? The programmer has accidentally dropped one of the equal signs in the comparison operator. If this code was being edited at 4AM the day before a big demo, believe me, it would be hard to spot.
But if put the literal was first and then forgot the extra equals sign, we would get an error. Go ahead and try it. Open up the javascript debugger in your browser and copy this code fragment into it:
( function ( aString ) { if( ‘:error’ = aString ) { console.log( ‘looks like we were passed an error token.’ ); } } ) ( ‘blarg’ );
You should get a syntax error, which should clue you off that you forgot an equals sign. This is a programmer habit seen in most "curley brace" languages (C, C++, Java, Python, etc.) JavaScript programmers may also want to consider using the "strict equal" comparison operator (i.e. the triple equals.) Refer to Mozilla's excellent Javascript Docs for more info.

Tuesday, June 21, 2011

chromium os on a dell mini 9? i like it

so for the last week i've been playing with chromium os. it's the linux-based web-focused operating system from google everyone's been yammering about for the past week. samsung and acer just released a couple "chromebooks," and it seems like the tech press is falling over itself to pan the devices. but i recently took the plunge and installed chromium on my dell vostro a90 (aka mini 9) and here are my impressions.

i like it. (with some caveats)

let me start by saying i'm a bit of a google fangrrl; not a complete fangrrl, but a fangrrl nonetheless. i started taking google services seriously several years ago when i subscribed to gmail. i love it: all my mail is available from any PC i happen to be sitting in front of. i was also an early adopter of writely (later google docs.) i love it for exactly the same reason: my documents automatically follow me around from machine to machine.

so i'm not a google fangrrl in the same way that people can be apple fans. design is important to me, but i'm more interested in the program's interface and capabilities. sure, cool looking hardware is nice, but it's the UX that counts. google's UI for gmail, gdocs, &c aren't "beautiful" in the way that apple interfaces are beautiful; but they're more than good enough. i am also not so much of a google fan that i use google buzz or wave or orkut.

i've been burned by syncing too many times in the past to trust anyone to get it right. i like the google services because they give me document and service mobility without having to think about it. that is my primary consideration.

yes, i occasionally happen to have my netbook powered on when there's no network coverage. and that sucks. for me, the benefits of service mobility outweigh the drawbacks of not being able to get to my docs when the network evaporates.

so i'm sure you've heard of gmail and google docs and picasa. i use them. they're great. however, there are a couple other services you might not have heard about. (and it's okay, these are all pretty much services for software developers.)

one service i kind of had to start using is Cloud9. it's a javascript editor and development environment right in your web browser. it's not perfect, but with the recent git and mercurial integration, Cloud9 is the node.js developer's equivalent of an unstoppable force whose kung fu will defeat the forces of evil. okay. maybe it only seems that way 'cause i'm a fan of storing your stuff in the cloud. but check it out. when my free trial expires, i'm totally giving them the small amount of money they're asking for. your mileage may vary.

in the future, i hope the Cloud9 team convert my money into a group editing / etherpad-esque feature.

so that's basically been my week. i installed chromium on my dell (thanks to the work by Doug Anson of Dell's CTO's Office.) i continued using google services i have been using, and started using (and was impressed by) Cloud9.

and i think the future is going to be even brighter. google docs currently have the problem that they don't make use of HTML5's local storage and web application APIs to provide a compelling "offline experience." (gmail currently has an "offline mode.") but CNET (and others) are reporting that we'll start seeing offline editing features this summer. if/when this happens, one of the largest complaints people have about chromebooks will disappear.

so... chromium on the dell mini-9 is a great alternative to paying $500 for a new laptop; especially if, like me, you happen to have a mini-9 hanging around the house. installing the OS was pretty straight-forward; use the link to Doug Anson's build above; hexxeh flow had some problems loading on my mini-9. but even Doug's work has a few warts:
  1. there's no audio - this is a known problem. Doug says it'll likely get fixed in a future release.
  2. there's no (easy) java - the most recent dell build of chromium doesn't ship with java, and the stock java installer for linux barfs (i'm guessing because of unfulfilled dynamic libraries.) i didn't spend a lot of time on getting it to work, and have seen java work on other chromium builds, so i know it can be done. it's just not easy.
  3. wifi's turned off out of the box - like many linux devices that use broadcom wifi chipsets, you have to download the proprietary drivers using a wired ethernet connection before you can experience wireless bliss. but it's pretty straight-forward to add the drivers. the release notes intimate the dell guys are working with the broadcom lawyers to figure out a way to include the drivers in the stock build, so maybe this problem will go away.
  4. WebGL? if it's enabled in the may 13th build, i've yet to see it work.
so, chromium os on a dell mini 9: i like it. it's not without it's warts, but it's a better alternative than plunking down $500 on a new laptop.

Monday, June 6, 2011

a few new node modules

people who know me know that i love node.js, the javascript network application framework. most of the projects i've prototyped in the last couple of years have been done with javascript in a browser and node.js on the server. i don't know why it took me so long to think about this, but i'm finally releasing some of the tools i developed. (okay, technically, i re-implemented them to avoid some project specific kruft.)

but anyway, in the last couple of weeks i released two node packages: node-props and node-mug.

node-props.js

node-props is a package that lets developers read properties from one or more URIs specified on the command line. so, basically, you can do this:
node application.js --config file:///etc/host_props.json --config https://example.org/app_props.js
i found the ability to grab properties from multiple locations is good for "cloud-like" applications. i use it to separate "host config" parameters (e.g. - addresses & ports to listen on) from "application config" parameters (like db addresses, etc.)

separating the two classes of config info provides a bit of flexibility if you're deploying an array of servers. by placing application config information on a central server, you only need to change a single file to change your app's behavior.

the package has been published to the npm registry and the source is available at https://github.com/OhMeadhbh/node-props

node-mug.js

the node-mug package exports an interface developers can use to generate RFC 4122 compliant Version 4 (random) UUIDs. unlike some other UUID generators for node, node-mug collects entropy from the /dev/urandom file present on most modern *nix systems. paranoid app developers can configure the system to read entropy from /dev/random.

interested users can install node-mug via npm or retrieving the source from the git repository at https://github.com/OhMeadhbh/node-mug

happy coding!