« Archives in August, 2010

Playgrounds

I can still remember the day when two colleagues had an argument about a piece of code similar to this one:

While one of them argued that ‘k’ was initialized upon every loop iteration, the other claimed that the variable was only initialized (to zero) once. After almost an quarter of an hour, they wanted to hear my opinion on this matter.

I didn’t tell them the answer, but instead asked: “Why don’t you just try it out? It shouldn’t take more than a minute…”. They looked baffled, so I did it on my machine while they were watching. Granted, it actually took me two minutes, but, hey, I was not used to work in front of such a big audience.

legobricks.jpgDuring a typical development day, lots of questions arise. Some are simple, some are not so simple and some of them are really tough. In many cases it is necessary to look up the missing information in a manual, in other cases it is best to consult a friend. But very often, one should just try things out in a safe place. That’s learning by doing, or — ideally — learning by playing, which is probably the best way of acquiring knowledge.

Playing should be fun, but at the very least it has to be painless — otherwise it won’t be done. If you have to close down/restart your IDE and/or have to go through an eight-step wizard to create a toy project before you can start, you wont do it. Experimenting shouldn’t be disruptive — it should have as little impact on your main work as possible. So what we need is safe place that is easily accessible and fun to work in — a playground.

Let me tell you about my solution, my playground. A virtual desktop manager (VDM) is at its center and I wouldn’t want to live without it anymore.

Normally, you only have one desktop, but by using a VDM you get many, as many as you desire. Most Linux desktops like KDE and Gnome already support virtual desktops out-of-the box and when I’m doing development on a Windows machine, I use VirtuaWin (virtuawin.sourceforge.net).

The main benefit of VDMs is that they allow you to separate different activities: you can go from one activity to another without closing/reopening applications; restoring context is effortless: just press the corresponding shortcut to switch from one desktop to another.

For me, four virtual desktops are sufficient: desktop 1 is for my main development work, desktop 2 for any urgent, higher-priority interrupts (like fixing the build breaker that I checked-in last night — damn!), desktop 4 for mail, instant messaging and Internet browsing. Desktop 3, if you haven’t guessed it by now, is my playground.

On desktop 3 there is always a terminal waiting for me and its current directory points to ~/pg, short for ‘playground’. Within this directory there are several subdirectories:

Each of these directories contains various code snippets, results from experiments that I did to find out certain aspects of a programming language, tool or environment.

In most directories I keep a write-protected file named ‘pg’, which serves as a default template to save me from having to type the same boilerplate code over and over again. As an example, here is the ‘pg’ file of the ‘cpp’ subdirectory:

With a playground like this in place, tackling the loop initialization problem becomes easy:

Actually, it’s even easier, as I have defined some shortcuts that let me compile and run code from within my editor; that is, without having to alt-tab to my bash console.

But this is just my playground, a place that gives me lots of joy. Maybe you work with Microsoft’s Visual Studio or Eclipse. In that case you could place links to template projects on your desktop; or, you place batch files in your playground directories that launch a new instance of your IDE and load a template project. Set-up your playground such that it is a fun place for you, but make sure you have one.

Like I said earlier, I keep my experiments for future reference and I suggest you do so, too. Over time, you not just improve your programming kungfu, you also build-up a valuable knowledge repository.