Monday, November 26, 2007

Down with IF!

Campagna Anti-IFTHEN what?
A friend of mine sent me a link to an Italian website sponsoring a campaign to rid software of the IF statement, especially 'type determinant' IF statements. Here is a link to the original page, and here is my rough translation of it:

The anti IF campaign. "if you want to stop, you can"

Objective:

The objective of this campaign is to make developers more aware of how to use object oriented design principles effectively. The first step towards reaching this objective is becoming aware of the consequences of using IF statements and other imperative programming techniques in the context of an Object Oriented language. The major objective of the campaign is to help developers understand how principles of OO design make it possible to achieve software that is more effective in terms of flexibility, changeability, descriptiveness, and testability.

Why:

The campaign was born from one of Francesco Cirillo's ideas: "Many teams want to be agile but don't know how to reduce the complixity of their code. We can start to achieve this by using OO techniques to remove type-determining IF statements from our code."

The fundamental problem is that type-determining IF statements create dipendencies, and coupling across modules (methods, objects, components, etc.) and increase the possible paths through the code, reducing the readability. The IF statement seems to be a quick and easy way to achieve functionality but with complex IF statements you quickly get to apoint where the software is no longer able to be modified and is full of duplication. Here is a simple example

// classe Bond
double calculateValue() {
if(_type == BTP) {
return calculateBTPValue();
} else if(_type == BOT) {
return calculateBOTValue();
} else {
return calculateEUBValue();
}
}

Every additional type added to the system obliges us to modify this piece of code. Objects give us an effective instrument for making sure that changes in requirements don't translate into changes in our code.

// classe Bond
double calculateValue() {
_bondProfile.calculate();
}

// classe AbstractBondProfile
abstract double calculate();

// classe BTPBondProfile
double calculate() {
...
}
// classe BOTBondProfile
double calculate() {
...
}
// classe EUBondProfile
double calculate() {
...
}

What is the advantage of removing the IF? In the fact that tomorrow the request for a new type of Loan requires simply creating a new class with the only additional logic being how to calculate the value of the loan.

The solution isn't always to create a new abstract class of interface, but the solution always increases the flexibility, descriptiveness, and testability of the software.

I agree with them, but would like to point out that you don't need that stinky old abstract class! If you apply a dynamic language such as Ruby to the problem, Duck typing removes the need for such an empty construct. And meta-programming can go a long way in removing unnecessary IF statements from your code. These guys should definately have some meta-programming examples of reducing cyclomatic complexity on their page.

Thursday, November 22, 2007

Zip A Directory With Ruby


I just found a new gem called rubyzip. Which is nice for creating regular old zip files. Ruby comes with zlib, but that is for gzip files which are not too friendly for windows users. Not that I condone the practice of using windows... Anyway. To install just type 'gem install rubyzip'. Here is some code I am using to zip up an entire directory called 'src', excluding hidden files. Just remove the Find.prune line if you don't want to exclude anything.


require 'rubygems'
require 'zip/zip'
require 'find'
require 'fileutils'
include FileUtils

root = pwd+'/src'
new_zip_file = pwd+'/newzipfile.zip'

Zip::ZipFile.open(new_zip_file, Zip::ZipFile::CREATE)do |zipfile|
Find.find(root) do |path|
Find.prune if File.basename(path)[0] == ?.
dest = /src\/(\w.*)/.match(path)
zipfile.add(dest[1],path) if dest
end
end


By the way sorry for no code formatting or highlighting. If only blogger had support for such useful stuff. I bet devastatin dave has code highlighting on his website. Hmm, I wonder what devastatin dave has been doing since 1986?

Monday, November 19, 2007

Sound Opinions Dissed on Guitar Hero


There is a wonderful program on public radio these days. Sound Opinions is the only radio show I still tune in for. Jim DeRogatis and Greg Kot are the Siskel and Ebert of rock and roll in pedigree, only better. So I was listening to their last show. They were on one of their favorite topics, talking about musicians licensing their music for commercial purposes, and suddenly, they were talking about the rolling stones licensing "Paint it Black" for use in Guitar Hero. They gave off their usual negative vibes about licensing art for commercial purposes and then they proceeded to mock the game and call it a waste of time. I have to call them out on this one. These guys clearly don't know the first thing about video games, particularly this one. Who hasn't listened to their favorite music and imagined themselves in the band? Guitar Hero is simply an electronic extension to that fantasy. And guys, when you add buttons to that air guitar it means you can compete against your friends. I wonder who does a better Kieth Richards, Jim or Greg. I guess we will never know. I'm just saying, but if anyone should be all for rocking out in all of its forms, the defenders of Pete Townsend's Windmilling right arm should be for it. But alas no, their advice is to stop wasting your time and learn guitar for real. Thanks gramps, I won't have time for that because I'm too buys saving up to buy GH III for my Wii.

Sunday, November 18, 2007

Move over VMWare


I just found out that I will need Visual Studio for my next project. That means I need Windows. In the past when I needed windows I installed it as a virtual machine and keep Ubuntu as my host OS. VMWare worked great, but setup and maintenance were a hassle because it wasn't installed with the package manager, and any time I got a kernel upgrade I would have to recompile the VMWare kernel module. Fortunately InnoTek, a competitor of VMWare, has recently GPL'd their virtuilization software called VirtualBox. Installation is as simple as
apt-get install virtualbox-ose
I had XP up and running atop Ubuntu in 20 minutes. After a couple weeks of use I can say it is fast, stable, and network configuration is much easier than VMWare. Bring on the kernel updates! Also you will note from my screenshot that individual windows apps run in their own windows, which is much nicer than having to interact with all your windows apps in one window. Now I'm not claiming virtualbox is the greatest, but in my experience, it is waaaay better than virtual PC, and for ubuntu users and many other GNU/Linux users it is superior to VMWare. Dig it.

Monday, November 5, 2007

You aren't computer literate unless you can understand a computer language


I was watching a lecture by Alan Kay entitled "Doing with Images Makes Symbols: Communicating Wit h Computers". At one point Kay is talking about teaching children to program and the importance of computer literacy. For Kay, literacy isn't understanding computer jargon or being able to check your email. He defines three aspects of literacy: Access Literacy is reading. You must be able to understand the intent of others. Creative Literacy is writing. You should be able to express your intent. Literature gives us genres and different ways of understanding what you are reading or writing. Kay's ideal is that people should be able to make the computer do anything they can think of. If it doesn't already do something, you should simply be able to tell it how to do that new thing. Such a high level relationship requires communicating with your computer at a high level. The three basic mouse gestures (point, left-click, right-click) aren't practical for conveying such complex ideas*. That is where programming languages come in (Smalltalk was his answer to this). Although I think all languages fall short of this ideal, you have to hand it to him, it is a ambitious goal, and in trying to achieve it he has done some amazing things.

More to come on various languages that attempt to bring computer literacy to the masses. In the meantime check out the video. I'm talking about a small little part of what Alan talks about. His work on creating GUIs is fascinating.

Here is the video 1hr 36 mins:


*(Although gestures can be a medium for a powerful language, even for computers, the Wii proves that)