Archive for Computer

Code Spelunking

Alex Miller writes about code spelunking techniques for when you’re digesting someone else’s code. He offers some good advice, particularly for tracking down call hierarchies and implementors (and some useful Eclipse shortcuts). What was missing, I thought, was the easiest one of all:

Draw a picture.

He did show a project dependency graph, but I find it’s really best to draw a simple picture, not even in UML, just in whatever comes naturally to you. On this picture, keep the real names of the classes, but then give them useful names, or small summaries, and describe their general responsibilities.

Use this diagram as your map, and don’t complete it for perfectionism, but only follow interesting paths. Invariably you’ll end up pruning paths as it’s clear that it leads to property holders, or utility functions (which is part of classifying the objects, as Alex writes) in which you’re not interested. But in particular, when you draw your own diagram, you have a bread crumb trail to look back on, so when the names don’t make sense, or you can’t remember the big picture, you’ve got your notes of what you’ve already seen.

Besides, if you’re drawing a picture, then the next developer who comes along will need to draw a picture too. So you might be able to do a formal diagram and put it into the project, helping others pick up on the architecture that much faster.

Comments

Database Programming Simplified

Ah hah! The solution to my database woes was the PreparedStatement.

The only serious database-intensive application I’ve written used PHP’s Pear DB package. And PHP was just a lot nicer about typing and variable substitution, so I found myself doing less of the quote-escaping and type checking necessary in Java. So it is with great relief that I discovered prepared statements.

It works just like a regular statement, in that you create it off the connection:
PreparedStatement prepstmt = conn.prepareStatement("SELECT * FROM ITEMS WHERE NAME=?");
But, as you can see, the SQL string substitutes values with question marks. The fantastic thing about it is that this statement is not typed. By this I mean that you can put an integer or a string into the statement without changing the SQL. This cleans up a lot of the messiness of the SQL requirement of having single quotes surround strings. And because Java String concatenation is ugliest for short strings, it cleans up your SQL statements several fold, in my opinion.

To set it, all you have to do it:

prepstmt.setString(1, "myname");
ResultSet rs = prepstmt.executeQuery();

So here’s a before:

int rows = stmt.executeUpdate(”INSERT INTO “+
“ConfigData (configkey, configname, “+
“configdate) VALUES (”+configKey+
“, ‘”+configname+”‘, “+configdate+”)”);

And the after:

String sql = "INSERT INTO ConfigData(configkey, configname, configdate) VALUES(?, ?, ?)";
... //Create the prepared statement object
prepStmt.setInt(1, configkey);
prepStmt.setName(2, configname);
prepStmt.setDate(3, configdate);

Even without the performance gains of re-using these PreparedStatements, the cleaner String notation is a great solution for queries with several variables.

Comments

Ambient Orb

I bought my partner an ambient device a year or two ago. It’s basically a frosted glass orb with some LEDs on the inside and a wireless receiver. The LEDs change color and can pulse at different rates, based on the command received by wireless (same coverage as cell phones). As we were living apart at the time, I paid for the subscription (about $7/month) to be able to change the color of the orb over the internet (it’s called the developer’s channel). I wrote some PHP to map the colors to my mood or my work level, things like that.

The purpose of Ambient Devices’ products are to convey information in neither a pull method (i.e. polling your email for new mail) or a push method (i.e. your phone ringing). In reality, it does both, just at a very low priority. Some people set it to stocks or the weather, or traffic, and whenever they want that information, they can glance at it to see the current state of their chosen subject.

So, with my partner moving in with me next month, I got the hardware developer kit, which is essentially a circuit board that connects to the serial port on your computer, and the other end into a connector on the orb. This way, I can send my own signals, and not pay to use their server to send the wireless commands. They include a small Java GUI to show you how you can control it, and the Java API they’ve created allows you to specify the color and pulse, and then through native code, push that out to the orb.

It’s unclear whether the orb must stay connected to the PC to keep its current state, as it is set to receive wireless signals through Ambient’s servers. These would overwrite the most recent computer command if it were not connected to the computer still.

I’m looking forward to creating something to control it, probably a servlet, although I could use some practice with RMI.

Comments

Bizarre Windows Glitch

The strangest thing that has ever happened to me on a PC happened today. I was going about my work, with Eclipse open, debugging a few apps, an email program was running, and there were some background applications (antivirus, firewall, etc.). Just for completeness sake, I’m running Windows XP, up to date on updates, and it’s on an IBM/Lenovo Thinkpad.

So, something glitchy happened when I went to click on something near the system tray, and in a matter of seconds, 30 to 40 program windows opened up, in all states of display – with or without titles, with or without minimize, maximize or close buttons, with or without screen real estate inside the windows. The computer was all but halted as I tried to close each window separately, since I imagine it was paging everything back and forth, trying to juggle so many windows.

But what was curious was that the windows were apparently visual manifestations of each (or a large portion of) my processes – there was svchost.exe, ctfmon.exe, ccApp.exe, winlogon.exe, and many others. Some of them were grouped according to which application started them, apparently – Firefox had at least 3, only one of which was named, and it was something like HkUpdate.

It took about 5 minutes of closing windows nicely then forcibly, and I still had to reboot, but I’ve never seen anything like that. A quick google doesn’t help, either, since “processes as windows” isn’t really precise. I’d be fascinated to find out what really happened, though.

Comments