ian sharpe :: personal web site

My lesson in elegance – less is more

This is a line of thought prompted by construction of the Excel page planner spreadsheet available in the Downloads section.

I admire elegance – minimal and efficient mechanisms for achieving a result.

Often, when tackling a complex problem, our initial solution may work but be woolly and overstuffed. Simply finding a way to accomplish the desired result is a struggle, and efficiency may not be a high priority.

Confessions of a blockhead

Years ago, I was editor of a magazine for the Amstrad CPC, a series of home computers popular in Europe in the late eighties. I was in the office when review copies of the very first release of Tetris arrived. We got them well before the game hit the shops (on cassettes in those days).

Tetris was instantly addictive. It was Friday and we played all afternoon. As keen programmers, we also remarked that the level of coding complexity was below what we were accustomed to in commercial games, even in those primitive days of 8-bit home micros. That alone was cause for admiration. Truly simple but great original ideas are few and far between.

I was sure I could write my own version, perhaps for publication as a type-in program. Home computer magazines were big on type-ins, since the Internet and cover-mounted software barely existed.

It wasn't a big problem, and by Monday morning I had the bare bones of a functional Tetris clone to flaunt in the office. It needed some user interface tweaks and didn't run smoothly, but it worked. I think I can lay claim to probably having written the world's first Tetris rip-off.

The reason mine didn't run smoothly is that processor power in those days was incredibly low. The program had a lot of screen updating to do, and I favoured coding simplicity in order to make it suitable for a type-in listing. So, even though I was writing in Z80 Assembler, gameplay in my version wasn't quite as slick as Mirrorsoft's.

It wouldn't have mattered too much for a type-in, but I wasn't satisfied and decided to work on the speed.

Going hardcore

The core program loop took user input, updated variables and updated the screen. It occupied something like three printed pages of Assembler, with one instruction per line.

For nearly a week, I obsessively refined the code, finding shorter and faster ways to do things, carving it down to a bare and elegant minimum. I looked up the timings of each machine code instruction and tried hard to minimise clock cycles in critical places.

The core listing finally occupied less than a page and ran every bit as smoothly as the original.

My quest for elegance taught me an immense amount and took my programming forward to new levels. That one exercise brought about the shift in thinking necessary for further progress.

Aside from such personal considerations, an initial inefficient design may be quick to produce but often there is a price to be paid for sticking with it. It may be slow, harder to use, consume more resources, or be more prone to error due to complexity breeding bugs, or through bad interactions with the user.

The cost of these penalties may add up to far more than was saved by economising on development. Spending time reviewing and refining a design may require more investment up front but can reap returns further down the line.

It's common sense and I have seen it demonstrated many times in real production environments. Yet it can still be hard to convince management without an engineering background and insulated from the production process that additional development time could profitably be spent.

On the other hand, sometimes the inefficient answer is good enough with no real drawbacks. There is no point chasing elegance for its own sake, unless you can afford the luxury for personal aesthetic reasons. An artist with no financial imperatives can do that, an engineer with budgets and deadlines cannot.

Back to the future

How does this relate to a fairly trivial diary worksheet?

I wrote two versions. The first took some head scratching to get working properly. I had a deadline to meet, and no choice but to put it on a magazine cover CD without further consideration. I used it myself for a few years and got occasional messages from readers telling me that they did too.

Then, for a reason I forget, an opportunity came to republish it. In light of what I had since learnt about Excel, I suspected that the same result could be achieved much more cleanly.

When you write for magazines, you hold up your work for examination by a vast number of people, ranging from novices to experts. A professional developer may decide to use some quick and dirty code that no-one will ever see but in magazine work you set yourself up for critical inspection.

Although I couldn't always achieve perfection, I always felt the need to do the best I could in the time available. This was partly from a sense of responsibility to people who hoped to learn from my work, and partly from my own pride in personal and professional standards.

Rather than simply re-run the old version, I decided to recode it. I'm glad I did. The update looks and behaves almost identically to the original, but behind the façade it is a model of simplicity in comparison.

The old sheet has formulas (or formulae) galore and intermediate calculations in a hidden column. The new one has no hidden columns and a bare minimum of calculation. Conditional formatting highlights weekends and suppresses dates beyond the end of the month at the bottom of each column.

In this case, the net gain from a user's point of view is minimal. For me, there is the satisfaction of a solution approaching the optimum possible, and a mark of my progress with Excel over the years.

You may like to investigate both versions for your own interest.

Forgotten but not dead

My version of Tetris was christened Fourtris (a name since used by others) and ended up on a game compilation cassette pedalled by my employer.

Image showing Fourtris described in a magazine
Clip from an advert in CPC Computing, October 1988

The lawyers would feast on such blatant theft of intellectual property these days. In 1988 Britain, hardly anyone had heard of intellectual property. I didn't think about the commercial issues; it was a fun challenge plain and simple.

I was pleased to find that although my Fourtris was written in the late eighties for a now obscure computer, Google finds it alive on retro computing sites such as this one. You would need to run it under a CPC emulator.

It isn't often that one gets a chance to notch up a world first, but "First Ever Tetris Cloner" may be carved on my gravestone.

All iansharpe.com site content and software © Ian Sharpe September 2010. Nothing may be reproduced without permission. See the full copyright statement. Use of iansharpe.com programs, information and advertisers signifies acceptance of the disclaimer. A privacy statement may be read here.