January 10, 2008

Else is evil

What is the purpose of the else statement?
When you write if statement, you are expressing explicit condition, which the code following if will be executed under. You ensure that the code will run, only when the condition holds. What about else? By using the else, you are stating, that you don't mind the condition, potentially grouping multiple conditions under one statement. To illustrate the point, i will use the snippet of code

int deliveryType = order.getDeliveryType();

if(deliveryType == Delivery.NORMAL) {
orderQueue.placeOrder(order);
} else {
orderQueue.placePriorityOrder(order);
}

What's wrong with this code? Everything will work fine, unless there are only two types of order. The code explicitly handles normal order and then groups every other order and treats it the same. As soon as there is new order type added this code would break.

I believe that as the if expresses exactly one condition, the else too should express exactly one condition, or if this is not the case, then it should serve as a guardian. A guardian before the inevitable changes.

int deliveryType = order.getDeliveryType();

if(deliveryType == Delivery.NORMAL) {
orderQueue.placeOrder(order);
} else if(deliveryType == Delivery.EXPRESS) {
orderQueue.placePriorityOrder(order);
} else {
throw new RuntimeException("Unknown delivery type.");
}

The code above illustrates much better approach. The else serves here as a guardian. The application will work, and when there is new delivery type requested it will immediately break unit tests and signal the cause of problem.

if(order.isNew() == true) {
getOrderDao().save(order);
} else {
getOrderDao().update(order);
}

Using else here is appropriate too, as it represents exactly one condition. A boolean can never be anything else than true, or false. True or false, two conditions, two branches and there is no way for third.


January 6, 2008

I used to have a player


I used to have an mp3 player. It was an iPod Nano and it sucked. Everytime I wanted to push there some music, I had to go through extra slow and user-unfriendly iTunes. I don't have that player anymore, I threw it away.


I have a new player now. I can manage my music using a file browser. I can watch videos, play games (half-life, doom, gameboy color games). I can choose how it will look like and how it will behave. It is still the same old iPod Nano..
That little shiny magic, that converted my iPod into a box full of fun is RockBox


Rockbox is an open source firmware replacement for a growing number of digital audio players. It has been in development since 2001 and receives new features, tweaks and fixes every day to provide you with the best possible music listening experience. Rockbox aims to be considerably more functional and efficient than your device's stock firmware while remaining easy to use and customizable. We believe that you should never need to go through a series of menus for an action you perform frequently. We also believe that you should be able to configure almost anything about Rockbox you could want, pertaining to functionality. It is written by users, for users.


Features



So, what can you expect from it? Lot of things.

Although this post talks about iPod, RockBox runs on other players too, just check the official list

No more database


Rockbox is a file system based jukebox. Unlike iPod original firmware, it does not depend on any proprietary binary database. This means, that you no more need iTunes to access and manage your music. All you need is a file browser. With rockbox, iPod works just like any other USB device. You can organize your files whatever you like, they will be recognized by rockbox and ready to play. On figure 1, you can see a file tree view.


Fig. 1 File tree view


It behaves exactly like you'd expect, like a normal file browser. You can find your files there, and they will be handled with the correct plugin (eg. opening an jpeg image will activate the image viewer plugin, opening .mpg file will play the video).

Database


Well, I lied in the previous section. You can have your database if you want. Rockbox can be set, such that it scans your files when it starts. All audio files are processed and the database is created from the id3 tags. You can then view your music in a similar way like with the original firmware (figure 2).


Fig. 2 Database view


Wide range of music formats and audio settings


You can have your music encoded in one of the 15 audio codecs, including (mp3, ogg, wma, aac, wav and flac. see the full list). RockBox provides a graphical equalizer, with a presets and gaples playback.

Customizable screen themes


You can choose from a range of themes, or create your own. Figure 3 shows some themes I liked.


Fig. 3 Themes


Comprehensive settings


You can set almost everything. This might not mean much to you, but if you are like me, and you like to have control over your player, you will be pleased.

Movies


Yes, rockbox can play movies. But to make any use from it, you will need to reencode them in the mpeg2 format, and change the resolution. There are 3 examples on the plugin page, but only the one with mencode worked for me. Fortunately, all movies from pocketmovies.com worked well without encoding.
The plugin page further states:

mpegplayer does all video and audio decoding using your device's main CPU. It does not use any special video decoding hardware such as the Broadcom Video Processor found in the ipod Video. mpegplayer therefore performs very badly on such devices in comparison with the manufacturer's original firmware.


Games


This is the best feature of rockbox for me. It comes bundled with a dozen of simple games, like chess, snake, winmines, solitaire.. and doom. Rockbox actually provides a doom engine, to play the game, you need to get the game wads. The wads provide the game graphics and level definitions. You can find some wads on doomworld. I've even seen half-life and counterstrike wads.
Another possibility for a game-thirsty rockboxer is the gameboy emulator, with which you can play gameboy color games, like Super Mario.


Fig. 4 Freedoom


Applications and Demos


Quite useless for me, I don't need text editors, paint tools, nor do I like to watch fire demos, or rotating cubes. They are a simple demonstration of power of rockbox.

Image and text viewing


The same as with original firmware, except that you can place your images anywhere in the player, and you don't need to use iTunes.

Voice menus and multilingual support


Great for people with limited abilities, and for those, who don't speak English.

Last.Fm support


Even if your player does not support Last.Fm directly, with RockBox, you can get a log of your music activity. This log can be then sent to Last.Fm using one of the tools listed in RockBox wiki.

Downsides


The main disadvantage of all this is the battery life. iPod Nano is simply not prepared for a display backlight turned on for a long time, as is the case when playing videos, or games. According to my experience, batteries cannot take longer than one hour of playing, or watching videos. But this is not a blame of rockbox, but of the iPod's poor battery life. 8 hours of music playback with original firmware is not much I think. Rockbox can match the original firmware in this aspect, 8 hours of playback with a promise:


Right now Rockbox is in development. The software currently requires more power to run than the original firmware, and so the battery life is shorter. With time this will improve. On other platforms Rockbox has actually surpassed the retail firmware in battery life, though of course we can't promise that will happen with Rockbox until we see what the limits are, but it will definitely be better than it is now.


Another thing I noticed is no podcast support. Podcasts are treated as normal audio files. With the original firmware, I could resume the podcast where I stopped it, there is no such option in rockbox.

Installation



Installation was a breeze. Actually, and there's a bit of irony in this, rockbox installation was far more simpler than loading my iPod with music using iTunes.
All what is needed is to unpack the rockbox archive to player and run the boot loader patcher. Patching was very convenient, I just ran the patcher and it did its job well, without asking me for help. I didn't even had to specify the mount point, everything was autodetected.

Rockbox team provides a tool for automatic download, installation and patching called RockBoxUtility. Unfortunately it didn't work for me, but it might work for you.

Notice: Installing rockbox will not destroy your original firmware, nor will it delete your music collection. Original firmware will be still available.

After a week of using, I can say for myself, that I never want to see the original firmware again.

All images, except the fig. 3, were captured by myself, using the rockbox simulator - it is a nice utility which can run RockBox on a PC.