Simplicity first, everything else somewhere after that

I’m currently hard at work putting together a GUI for my research project using Python, Qt and the PyQt interface. Actually that’s a lie, I’m forcing coding in between classes, homework, meals and gym time. Anyways, the point is I’m building a GUI using PyQt. Qt is really quite an advanced toolkit which does much more than just let you build a pretty interface. While the full powers of Qt certainly merit a full series of blog posts on their own (and I’m certain someone has already done just that), one part  of the toolkit that I find really neat is their framework for displaying complex data models.

Here’s the basic idea: you have a somewhat complicated data structure sitting around and you want it to be displayed as some sort of list or tree-like structure. You also want the user to interact with the data structure and any changes made to the structure should be reflected in the display without you having to explicitly control the updating. Qt gives you a way to do this using models and views. First you have to build a model of your data as a subclass of the QAbstractModel class. Your data needs to be able onto a grid-like structure of rows and columns and your subclass controls how that mapping takes place. You then hand off an instance of that subclass (with the data you want to display) to some display widget, generally a list of tree view. This widget then becomes (as the name suggests) a “view” on to the underlying data model. Changes made to the data automatically make their way to display via the model you specified and vice versa.

As you can understand, I’m hiding the details, and believe me, there are a lot of details. While it’s really nice to have this automatic mapping if you have a complex data structure, it’s implemented via this rather complex structure of interacting classes and objects layered on top of each other. The result is that when you don’t have a complex data structure, jumping through the hoops and navigating the network of QSomethingOrTheOther objects is simply a pain in the ass.

My system has a routinely updated list that needs to be presented to the user. My first ideas was to implement a Model-View structure so that I didn’t have to handle updating the display maunally. Bad idea.  While I certainly got the system working to a certain extent, my resulting code a was a mess of interlocking method calls and object juggling which really seemed unjustified considering that I was just displaying a simple list. I eventually decided to take the plunge and in twenty minutes rewrote most of the relevant parts. The result was a dead simple item-based list that would get updated in response to particular method calls from the rest of the GUI elements.

Lesson learned: simplicity first. It is very tempting to just go after the most complex, most theoretically solution. After all, the tendency to tinker and try out new things is deeply ingrained in the hacker psyche. But sometimes our search for carefully ordered, hyper-complex order descends into a chaotic mess of obfuscated code that really does a lot more than is necessary. The simplest solution is often not the best but a lot of the time, it’s just good enough. And in the case of software, good enough is often better than perfect. Remember that you’re not just coding for machines, you’re coding for people too. If your first reaction to your own code is WTF??? then something needs to change. Have fun coding simply.

One thought on “Simplicity first, everything else somewhere after that

  1. Good post. In software Occam’s Razor definitely applies a lot of the time, however, it isn’t a silver bullet either.

    Several times during my CS schooling, I witnessed friends fall victim to the “But wouldn’t it be cool if …” fault where they would unnecessarily complicate programs beyond anything they could reasonably maintain given the short hack->release cycles we’re subject to in school.

    The mantra I’ve tried to adopt now that I’m done with school is to ask myself, “Is this code ‘perfect enough’?”, meaning not perfect, and not necessarily 100% perfect according to proper design, but rather, is it practical and maintainable enough to do what it needs to? It’s worked out quite well so far.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s