Sunday, 7 February 2010

Generating Random Numbers using LFSRs

On a recent project I needed a simple random number generator. It didn't need to be anything fancy, nothing mathematically pure or statistically valid, just a quick'n'dirty little generator that would spit out numbers that looked random to the casual eye.

In a previous incarnation I worked for a mobile radio consultancy. One of the things I came across in that particular life was the concept of Pseudo Random Binary Sequences (PRBSs).

These are sequences of 0s and 1s that are generated deterministically, yet are random enough for many practical purposes. They are used extensively in communications to spread the bandwidth occupied by a signal. This makes the signal more immune to jamming, as the signal is smeared across a bigger chunk of spectrum than would otherwise be the case. It is also more difficult to intercept, since any receiver must know and be synchronised to the spreading sequence at the transmitter. A more subtle usage is that multiple signals can be spread across the same spectrum by multiple PRBSs, yet be teased apart at multiple receivers by correlation with the correct individual spreading sequences.

PRBSs are typically generated using Linear Feedback Shift Registers (LFSRs). These are cheap and easy to build in hardware, and easy to code in software.

At any time, a function of the LFSR contents is generated as the output bit value. This is fed back into the shift register as the next input. The output bit sequence is essentially random, as is the sequence of states formed by the LFSR contents.

The 'function of the LFSR contents' mentioned above is called a generator polynomial. It determines which LFSR cells are combined and fed back to form the LFSR input for the next state.

For instance, the Wikipedia article on LFSRs lists the following generator polynomial for an 8-bit PRBS:

x^8 + x^6 + x^5 + x^4 + 1

We now need to know how to convert this into code.

As we are generating an 8-bit polynomial, we start off with the 8-bit unsigned value 0x00u as our feedback function. If a power of x, say m, is used in the polynomial, assign a 1 to polynomial bit (m-1). The +1 in the polynomial corresponds to the output bit, and does not appear in the feedback settings. Here we have an 8-bit generator polynomial with bits 7, 5, 4, and 3 set, giving a value of 0xB8u.

Given this, and also using the example code in the Wikipedia article, we can now write a function that returns the next value in a pseudo-random 8-bit sequence.

#define LFSR_8_INITIAL_VALUE 0x01u
#define LFSR_8_POLYNOMIAL 0xB8u

uint8_t next_lfsr_8( void )
{
/* seed LFSR */
static uint8_t lfsr = LFSR_8_INITIAL_VALUE;

/* get LFSR LSB */
uint8_t lsb = (uint8_t)( lfsr & 0x01u );

/* shift LFSR contents */
lfsr = (uint8_t)( lfsr >> 1u );

/* toggle feedback taps if we output a 1 */
if( 1 == lsb )
{
lfsr ^= LFSR_8_POLYNOMIAL;
}

return lfsr;
}

An n-bit PRBS is said to be maximal length if the number of distinct output values it generates is 2^n - 1. That is, a maximal length 8-bit PRBS consists of 255 values. Our generator polynomial is indeed maximal length, and so the values returned will start to repeat after 255 calls.

In the above code I have set the initial value of the LFSR to 0x01u. The LFSR will generate a fixed sequence of output values, and the initial value merely determines where in the sequence the reported values will start.

Note that because the feedback taps uses the XOR operator, the LFSR will get stuck in the all-zeros state if it ever gets there. Care must be taken that an LFSR is not initialised with this value.

The corresponding 16-bit function is as follows.

#define LFSR_16_INITIAL_VALUE 0x0001u
#define LFSR_16_POLYNOMIAL 0xB400u
uint16_t next_lfsr_16( void )
{
/* seed LFSR */
static uint16_t lfsr = LFSR_16_INITIAL_VALUE;

/* get LFSR LSB */
uint8_t lsb = (uint8_t)( lfsr & 0x0001u );

/* shift LFSR contents */
lfsr = (uint16_t)( lfsr >> 1u );

/* toggle feedback taps if we output a 1 */
if( 1 == lsb )
{
lfsr ^= LFSR_16_POLYNOMIAL;
}

return lfsr;
}
Using the above approach, you can generate PRBSs of arbitrary length, given tables of generator polynomials (your search engine of choice will unearth plenty).

Sunday, 20 December 2009

What's In Your Toolbox?

This article started out as a simple list of the tools and applications on my work PC.  This was to be both an audit of the current state of the union on my PC, and a checklist for myself for when my PC gets upgraded or dies (note that there is no 'if' in the preceding statement).  I was interested in not just the full-blown applications that appear in the Start menu, but also the little scripts, batch files, and utilities that tend to accrete on our PCs, the small-but-useful stuff that doesn't get much mind share.

The main reason stuff is on my PC is to help me develop, maintain, or review code, but some other bits and pieces have snuck on too, for instance applications that help me write documentation, or prepare a presentation.  I try to keep my PC fluff-free, and any application that doesn't cut the mustard is ruthlessly excised.  If it's listed here, I use it, and find it useful.

Programmers are notoriously obsessive about their choice of tools, and love agonising over them in minute detail.  Just type 'best text editor' into Stack Overflow and watch your screen melt.  I suspect engineers are comparatively pragmatic, but there's no harm in taking a lead from our non-soldering brethren.

This list does not by any means constitute the best of breed.  It's just bits and pieces I've accumulated in my travels.  Sometimes I stumble upon gems through books, blogs, or magazines, sometimes I go looking for a particular functionality I'm sure somebody must have built, and sometimes people recommend programs.  Whatever.  If you see something here that looks interesting, but would like to see some alternatives, Scott Hanselman maintains an awesomely huge tools über-list, and I highly recommend that you at least skim it.

One tip: learn to use your tools.  RTFM, and learn the keyboard shortcuts.  I've found over the years that many of the tools I've been using are more powerful than I realised, but you need to delve to find this out.  And using keyboard shortcuts is a no-brainer - even if they only save a couple of seconds over the equivalent operation with a mouse, the compound interest on this saving over the course of a year is enormous.  [Related pet peeve - I find it almost physically painful to watch somebody doing something like copying and pasting text between two documents using the mouse.  They carefully highlight the text of interest, then go to Edit|Copy, then press the toolbar icon for the destination document, carefully place the cursor at the appropriate point, and then select Edit|Paste.  And in my head I'm shouting "Double click!  Ctrl-C!  Alt-tab!  Ctrl-V!".  But maybe that's just me.]

Don't be afraid to splash your own cash.  Even if the department budget won't stretch to buying everybody a decent text editor, spending a few quid of your own hard-earned on decent tools is well worth the investment.  I've a friend who's a car mechanic, and has spent thousands of pounds on stocking a decent toolbox over the years - I don't see why supposedly professional engineers should shy away from investing in their own careers in like manner.  That said, many of the tools listed below are free.

Rules for inclusion: I'm interested in applications that may be of interest to the engineer on the Clapham omnibus, and so haven't listed project-specific tools like compilers and vendor IDEs.  If you need such tools, you'll have them.  I've also skipped over staples like Microsoft Office - such applications are likely to be on the default engineer PC install.  Again, if you need them, you'll have them.  Tools are listed in alphabetical order.  And lastly, these are all Windows applications, because that's where I live.  Some may have ports to other platforms, and there are almost certainly functional equivalents for Linux, OS X, etc.

7-zip: Everybody needs a compression tool.  This one is free, and handles all of the formats I've thrown at it over the years (including things like *nix tarballs).  It can also generate password-encrypted zip files, which is neat for sending confidential documents or source code to offsite contacts.

Absolute Uninstaller: The clue's in the name - this uninstalls programs.  But this free program lists installed programs much more quickly than control panel, removes programs more completely, and can export a list of all installed programs.  In truth this is my main use for this program - generating a weekly list of all installed programs as a snapshot for future reference.

Agent Ransack: This is just insanely useful.  It's a free search program allowing you to recursively search folders for files containing specified text.  Both the filenames and the search text can be regular expressions.  When installed, "Agent Ransack..." appears on the right-click context menu in Windows Explorer, allowing you to specify the root folder for your search.  I have this up and running pretty much all the time I'm developing or examining code.

Beyond Compare: Again, everybody needs a file differencing program.  This one lets you compare files or folders in text or binary mode.  Text comparisons can ignore whitespace-only differences, or 'trivial' changes such as source code comments.  Changes between two files or folders can be copied across in either direction.  Folders can be synchronised, FTP is supported, and there's a whole bunch of options I haven't even looked at.  Heeding my own advice above, I really need to revisit this and RTFM.  Again, the tool integrates with Windows Explorer, appearing on the right-click menu to let you choose the 'from' and 'to' files/folders for a comparison.

Bug Shooting: Before I installed this free screenshot utility, I wouldn't have believed how much I would use it.  It can be run by clicking on its icon in the system tray, or by pressing one of the hot key combinations.  It can capture either the whole screen, or just a specified section, and can do so immediately or after a timed interval.  Once captured, you can crop the image, highlight an area, write text on the image, and so on.  It was originally developed for posting screenshots to bug tracking systems, but I use it for writing documentation, FAQs on our in-house wiki, and a bunch of other stuff that wouldn't have occurred to me before I'd installed it.

CCCC: This one is fairly hard-core geeky.  It's an open source application based, I believe, on a doctoral thesis, that analyses C/C++ source code and spits out various metrics including line of code, line of comments, and McCabe complexity metric.  I use these as a general basis for comparison of bodies of source code - is this application inherently more complicated than this other one?  It also highlights complexity hotspots worth investigating in a bit more detail.

CCleaner: This free program removes unused files to free up space by clearing your recycle bin, Windows temporary and log files, browsing history, and so on.  It also contains a registry cleaner for removing unused and old entries.

Clean Sources Plus: This is a neat little freebie utility from Jeff Atwood.  It gives you a right-click menu item in Windows Explorer (yes, I'm a sucker for these) that lets you delete a user-specified set of files and folders in and under the clicked-on folder.  So, for instance, you can remove all object files in your development folder before checking your code into version control.  It also gives you the option to zip the folder - very handy when you want to upload or email some code.

Clipname: Another freebie that rapidly became indispensable.  Right-click on one or more files or folders, and you get the option to save their names to the clipboard for copying into other applications.  You can save just their names, or their full paths including all folder information, and can do so in the URL-encoded format needed for hyperlinks.  Very, very helpful.

CmdHere Powertoy: This time the freebie is from Microsoft itself.  Right-click on a folder (are you spotting a common theme here?) to get the option to open a command window pointing at the selected folder.  Very useful when you want to jump in and check the output from a batch file, or quickly run a program with particular command line options.

DebugView: The first but by no means the last appearance for the Sysinternals tools on this list.  This program captures debug output locally, or across a TCP/IP network.  In my case I've only ever used it to capture OutputDebugString() calls in my PC code, but apparently it also supports kernel mode debugging.

Editplus: a decent text editor is absolutely essential.  When developing you almost certainly spend a huge chunk of your working day in a text editor, and so should be using a tool that helps you as much as possible.  For me, the minimum requirements of a text editor include language-specific syntax highlighting, the ability to record and play back keystroke macros, and regular expression searching.  Failure to provide any of these results in me immediately losing interest.  For historical reasons my weapon of choice here is Editplus, which is small, cheap, and powerful.  BTW, one of my standard interview questions is to ask the candidate what their favourite editor is.  I'm not really interested in their answer, so much as in if they have one.  If not, they're not thinking about what they're doing.

EverNote: I realise that I'm only using a tiny percentage of the functionality of this application.  I basically use it as a journal/logbook, writing up notes as I work, and pasting in code snippets, scope shots, and photos as appropriate.  These are all then available for searching when, in six months, I want to quickly find all my notes on a given project.

Fences: This one is still in the stage I like to call "trying-it-out-to-see-if-I-like-it-and-or-find-it-useful".  It lets you create areas (that it calls 'fences') on your desktop, in which you can drop files or shortcuts.  I personally try to keep my desktop empty, and use a hotkey application launcher to start up most of my programs, but this has potential.  I've been using it to create project fences containing links to the datasheet for the chip I'm working with, the project demo software, and so on.  Fences feels stable and would certainly be worth looking at if you tend to use your desktop as a holding area.

Foxit Reader: Smaller, faster, and leaner than Adobe Reader.

Google Desktop Search: This brings the power of the mighty Google to your desktop.  Once installed it starts indexing all of the files on your PC.  I've got it running in the system tray, and by typing in, for instance, the name of a project I've worked on, it will find all files containing that name.  And that includes not just text files, but also Word, Excel, PDFs, emails, etc.

Janitor Script: I have a folder on my PC where I put all downloads, email attachments, and things I only want to deal with on a temporary basis.  This little script runs every lunch time and deletes everything older than a week from the folder.  Neat.

Keepass: Keep all your passwords in a single database with a single password to remember.  I use this for not only all of the web sites demanding I have a password to identify myself, but also for the passwords I've assigned to users on some of our internal tools.

Launchy: This is just mind-bogglingly, consciousness-expandingly useful.  It's a hotkey application launcher that has totally removed my need to use the Start menu, shortcuts, or just about any other method of running a program or opening a commonly accessed file.  I press ALT-space, type in the first few characters of the application I'm interested in, it auto-suggests the most likely candidate based on my previous invocations, and I hit enter to run the program or open the file.  Or, in many cases, I can supply further information.  For instance, to search for a book on Amazon, I type in 'am' to get Launchy to suggest 'Amazon', then hit tab, followed by the name of the book I'm interested in.  It then launches my browser and displays the Amazon search results.  I can also create emails, add items to my web-based to-do list, and append text to files on my PC.  If you haven't tried a hotkey launcher, give them a go, they'll change your life.

PDFCreator: Install this freebie to have a PDF output option appear in your print menu.  This lets you create PDFs straight from any Windows application that lets you print.  Neat.

Paint.NET: Free and increasingly-powerful image and photo editing software.  I tend to use it for really simple things like annotating photos, but there are a wealth of features in there.

PC-Lint: If you're writing C or C++, and don't have this, or a functional equivalent, go buy it.  Now.  Seriously.  This static analysis tool remorselessly examines all of your code, and pedantically flags every potential violation of the rules of the language.  You can also configure it to perform MISRA compliance checking, so consider it as two-for-the-price-of-one.  It's remarkably flexible (for which read 'difficult to configure to get it going'), but well worth the effort.  Hook it in to your development system so that it runs every time you build your application, and you'll never look back. 

Portmon: YASG (Yet Another Sysinternals Gem).  I stumbled across this when debugging a serial port application I'd written.  It lets you capture all reads and writes to/from specified serial and parallel ports.  Absolutely essential if you're playing with such items.  Also useful if you want to figure out the interface protocol for a bit of third-party kit.  There used to be similar applications for file activity (filemon) and registry activity (regmon), but these have now been superseded by a process monitor (procmon).

Process Explorer: YASG (see above).  Task Manager on steroids.  See unbelievably detailed information on what's going on in your PC.  I've no idea what a lot of the displayed data is, but what I have been able to do with this in the past is use it to check for memory leaks in my PC code.  Select a program, right click to select 'Properties...', and switch to the 'Performance Graph' tab to see the application memory usage - if it continually ramps up, you've got a problem.

RegexBuddy: OK, this is pretty geeky too, but in a good way.  I bought this following Jeff Atwood's enthusiastic endorsement, and find that it excels in its (admittedly rather narrow) sphere of application.  As previously mentioned, one of my main uses for script languages is searching for data in log files.  Well, with RegexBuddy, I can copy some of a log file into a 'test' window and try out regular expressions that are evaluated on the fly, highlighting matches as I type.  When I'm happy with my expression I can copy and paste it into a fully-fledged script and I'm good to go.  Similarly, I can copy sections of code into the test window, and develop regular expression searches to be pasted into the aforementioned Agent Ransack.

Source Code Spell Checker: As I’ve mentioned before, I have a fairly dogmatic approach to spelling and grammar in source code.  I run this tool on all of my own code, and on code I'm reviewing, and fix or flag all transgressions.  Cheap, and does what it says on the tin.

Source Navigator: This is an open source code analysis tool.  It can generate function call trees, variable cross reference tables, class hierarchies, etc.  Extremely handy in getting you up and running on a glob of code you haven't seen before.

SyncToy: Another freebie from Redmond.  This one lets you synchronise folders, which is very useful when, for instance, keeping a snapshot of your source code on a USB stick or network drive.

TreeSize: Right-click a folder to see a graphical representation of the size of it and all folders under it.  Great when trying to track down what's eating all of the space on your hard drive/USB stick.

ZoomIt: YASG (see above).  Lets you zoom in and scroll around on your PC screen, highlight areas, and scribble notes on it.  Invaluable during presentations or training sessions.

Thursday, 12 November 2009

Synchronicity or Serendipity?

The week after I posted my blog on integer division I was asked for help on a problem involving linearising a sensor output. Part of the answer turned out to involve multiplying a function of the output value by some weird factor like 211/255. Well, given that I'd just written exactly the code needed to handle this sort of strangeness, this turned out to take approximately ten minutes of my day. Result.

Of course this approach only occurred to me because I'd so recently been playing with integer division code, and so made the obvious connection. Maybe given time, if I hadn't been handed the answer by the universe immediately beforehand, I would have come up with an equally valid alternative approach. But for the life of me, I can't think of a neater solution to this particular problem.

A second example of the power of autodidacticism (not as dirty as it sounds) came about the following week when I was looking at how to add new events to a client-server messaging system. Having a global enumeration of all possible events works fine, but means that you have to rebuild every darned application every time you add a new enumeration. Well, I'm something of a compulsive purchaser of programming books, and one evening I was idly flicking through the excellent Game Coding Complete when I came upon an extremely neat solution. You calculate a hash of a descriptive string, and use that as the event enumeration. For instance, if you have an event to set the gain of an amplifier, you calculate the enumeration as hash( "set_amplifier_gain" ). Store the string and enumeration in an object that gets passed around the system. Point your debugger at an event, and you see a clear text description of what it is. There's a bit of housekeeping to ensure that two strings don't accidentally generate the same hash value, but the approach is lightweight, and lets you arbitrarily add new events while only rebuilding applications that actually need to know about them.

So: two closely-spaced incidents. In one, I have a neat solution, and almost immediately stumble upon a matching problem. In the other, I have a trivial-but-irritating problem, and almost immediately stumble upon a neat answer. Both cases involved looking outside the scope of my everyday activities to add some tools to my grab-bag of solutions.

It's what Stephen Covey refers to as sharpening the saw, which is a great analogy, and resonates conveniently with the tools metaphor - we all have a bunch of tools at our disposal, and it behooves us to care for them. It also pays dividends to get new tools, upgrade old ones, or simply window-shop to check out what's available.

As the always-readable Rands wrote, "We see the world how we want. A carpenter sees all problems as a nail. I see problems as finite state machines." Well, amen to that, my brother. (Hint: a state machine may not always be the best answer, but it's almost always an answer.)

So: not exactly a profound revelation, just a gentle slap upside the head from fate, reminding me that it's always worth spending time learning new techniques, following interesting blogs and articles, and keeping a weather eye on seemingly unrelated spheres of interest. Even if the number of times you extract a true nugget is small, it's not vanishingly so, and the return on investment can be enormous.

Sunday, 21 June 2009

Division of Integers by Constants

Nigel Jones recently wrote a blog entry that very neatly summarises an article called 'Reciprocal Multiplication, a tutorial', by Douglas Jones.

The (Douglas) Jones article explains how to use reciprocal multiplication to perform division - for instance, if you want to divide by 10, this is the same as multiplying by 1/10. However, by doing everything in fixed point arithmetic, you can avoid the computational overhead of invoking your compiler's division routine. The result is smaller, faster code that has exactly the same accuracy as the compiler-supplied result. Anyone writing PC code may not appreciate this, but in the embedded world this sort of approach is vitally important in terms of both code size and run time.

The original article is quite long and detailed, and despite having stumbled upon it a couple of times in the past, I'd never got round to actually reading it. And so (Nigel) Jones's article came as a very welcome abstract, boiling it down as he does to a couple of simple algorithms - if you want to divide by 'x', here's what to do.

At the end of his post, (Nigel) Jones said 'If someone has too much time on their hands and would care to write a program to compute the values for all integer divisors, then I'd be happy to post the results for everyone to use.' Well, I don't know about having too much time, but I do enjoy a lunchtime programming exercise, so I sent him a text file containing the coefficients to perform integer division for all 16-bit unsigned numbers from 3 to 32768. I also asked if he would mind if I posted the source code here, in case it was of use to anyone.

He responded with an excellent suggestion - how about generating a header file containing macros with the appropriate coefficients for all divisors? Anyone wanting to use these algorithms could then simply include the header file, and call the relevant macro to perform the required division.

I've followed this suggestion, so please follow the links to check out:
  • a header file containing the coefficients for unsigned 16-bit division
  • a header file containing the coefficients for unsigned 8-bit division
  • a command-line program generating the coefficients for unsigned 16-bit division [source][exe]
  • a command-line program generating the coefficients for unsigned 8-bit division [source][exe]
The programs were written in Borland Builder, but are in ANSI C, and so should run on any platform with appropriate tweaks to the uintX_t typedefs. They accept floating-point arguments, and so can generate the coefficients for division by π, sqrt(2), etc. They will also report the number of errors found during an exhaustive search of all divisors, and the maximum error found.

Monday, 1 June 2009

Profound Advice: Learn a Scripting Language

This is another entry in an intermittent series in which I arrogantly pontificate on what constitutes profound advice in the world of embedded software. Actually, I'm unnecessarily narrowing my horizons here, I feel quite sure I could be arrogant in a much larger sphere.



So, here we go again. This time I would urge neophytes to learn a scripting language.

These may not be directly useful in terms of running on a little 8-bitter, but they are unbelievably useful in a range of other applications.

The advantage of scripting languages is that they're... well, scripting languages. They act like super batch files letting you leverage the power of other programs to get a job done. They tend to be interpreted rather than compiled, and so are (a) really fast to write, since they sidestep the edit-compile-link-run paradigm, and (b) relatively slow to run, since they've sidestepped the edit-compile-link-run paradigm. But with the power of modern desktops, performance really isn't a huge concern with the applications I'm interested in.

At a more detailed level, they also tend to support dynamic typing, which lets you blithely ignore whether the variable you've just created is a string, an integer, or whatever, until you start using it in a context that defines what it has to be. This is both a good and a bad thing, as what the interpreter thinks it is doesn't always tally with what you think it is.

Yes, you could create an executable that would run much faster, as it's not interpreted. But for sheer speed of get-in-there-and-hack-out-something-that-works, you can't beat a scripting language.

But before I go blindly hacking away into the undergrowth of what a language is or isn't, let's try to steer the conversation back on track.

The scripting applications I find myself revisiting time and again are (1) analyzing log files, and (2) generating code.

Dumping data to disk on a PC hooked up to an embedded system is part and parcel of the daily life of a firmware engineer. But when the system's been running for several days in an environmental chamber, or for months in a remote data acquisition application, these log files can be enormous.

The chances are you're only interested in a tiny fraction of the logged data, or you're looking for a particular event, or you want to change its format slightly, or... something else. Maybe you need to break it up into chunks for analysis. Maybe you only want to see every line in which the fourth CSV field is greater than 10. Maybe you want to reformat the data for input to another program. For whatever reason, you've got a bunch of data on a disk, and you want to pull the data needle out of the multi-megabyte haystack.

The script to do this will typically boil down to a few lines containing regular expressions. Pipe the data into the script, and out to a text file, and you're done.

Scripting languages are also a natural for code generation. Write a script to generate big chunks of your firmware, which are then compiled as normal. Any type of code containing lots of variations on a theme is a natural for this approach - good candidates are communications handlers and state machines. Give the script a list of the UART commands to be handled, or the FSM states, and let it build all of the scaffolding code in a loop over the list contents. You follow along afterwards and fill in the blanks detailing, for instance, how to handle the parameters of a specific command.

You could of course make your script sophisticated enough to accept details like the command parameters, and then generate the handling code too. But there's always the danger of spending too much time developing the script instead of doing actual useful work - as always, your mileage will vary, you need to be pragmatic, and you need to make realistic trade-offs.




My weapon of choice as a scripting language is Perl. The reason for this is mainly historical, as I've been using it for years now. I can't even remember why I first picked it up, but would guess that I wanted to adapt something that almost-but-not-quite did what I wanted, and it happened to be in Perl.

My relationship with Perl is somewhat ambivalent. I find it an enormous sprawling mess of a language, both immensely powerful and willfully obtuse. In fairness I don't spend enough time with it to make it really sing, and tend to rely on the Camel, the Cookbook, and my collection of previous scripts to bash together what I need at any given time.

The Perl apostles trumpet its approach of There's More Than One Way To Do It (TMTOWTDI) as a strength, but I find it muddies the waters; ask three Perl coders to write some code, and they'll do it in at least six different ways.

However, there's no denying the power of the language. Most scripts boil down to relatively few lines of actual code, and are amazingly compact compared to the functionally equivalent C/C++ code.

There are of course scads of scripting languages; Ruby and Python seem to be very much à la mode. Just pick one, preferably one that somebody near your desk can help out with when you get stuck, and dive in.

Tuesday, 12 May 2009

Profound Advice: Do It Right the Third Time

This is one of series of posts on the subject of profound advice, in which I metaphorically travel back in time to meet my younger self, and give that sad git some hard-earned pearls of wisdom. Which, knowing me, I will completely ignore. Or did ignore. Or something.



This week marks the 40th anniversary of the release of 'The Italian Job'. This most quintessentially British of all crime capers stars Michael Caine as Charlie Croker, modish wide boy with an eye for the main chance.

Noel Coward is magisterial as Mr. Bridger, the uber-royalist crime lord running the underworld from his prison cell.

Even Benny Hill is watchable as the lascivious computer expert, with a penchant for the larger lady, charged with bringing Turin to a standstill by overriding the traffic control system.

And of course it gave the world the essential Michael Caine quote, the one guaranteed to be invoked by generations of impressionists.

The 2003 remake with Mark Wahlberg was a perfectly acceptable crime movie, but in the UK at least was damned by association with the original - how dare anybody remake such a classic movie? My wife wouldn't even let me bring it into the house until last year.



It's something of a truism in engineering that the best time to design your solution to a given problem is just after the project's finished. At this point you've met the pitfalls and pratfalls awaiting you, you've worked around them, and you're fully aware of the implications of your early decisions. Knowing all this, and with the benefit of 20-20 hindsight, it would be relatively trivial to re-do the project.

A depressingly common approach to design is the big bang approach, AKA BUFD (big up-front design). Given a problem, you attempt to construct an all-singing, all-dancing solution that will stand forever as a testament to your genius. You try to build a framework that will solve not only your immediate problem, but a whole class of similar problems that may or may not arise in the future. Which is a fine and worthy goal, except that it's almost certainly a colossal waste of time and effort. It's certainly not shipping code. You don't know the future - if you did you would be betting on horses, not cranking code. So admit your temporal limitations and code for what you need right now, not what you may need in some conjectural future.

My first attempt at solving a problem tends to involve a lot of hardwired assumptions - e.g., the demo code only works if the microcontroller is at a specific address on the I2C bus, has a fixed memory map, and is running a specific version of the firmware.

Which works fine a surprising amount of the time. If I have to revisit the code - for instance, the firmware gets updated - I'll add a bit more intelligence to the code to handle this new situation.

Finally, if I have to revisit the code yet again, I'll make more of an effort to do things 'right'. At this point I've already solved the basic problem twice, so I've got a pretty good handle on how to do this. I know what works and what doesn't. I can devote some effort to developing a more generic solution with the full knowledge of what functionality is likely to actually be needed - because it was actually needed. I can develop a more modular code layout that lends itself to future maintenance work. If I'd tried to do this from the get-go, I would needlessly have locked myself into what I thought was the correct architecture on day one. And it would have taken a lot longer to build the generic-but-wrong solution than it took to build my hardwired-but-quick-to-get-going solution.

As usual I thought I had independently invented here a whole new concept that would revolutionise the world, a concept I termed 'do it right the third time'. I also toyed with the phrase 'speculative complexity', which pleases me, as it captures something of the unnecessary intricacy many people seem to find so attractive.

However, it turns out that lots of other people have had exactly the same idea; the extreme-ists call it 'you ain't gonna need it', which is admittedly much cooler.

Don't go for a big bang when you're only supposed to blow the bloody doors off.

Thursday, 7 May 2009

The Tom (not Peters) Guide to Man Management

Last weekend was a bank holiday here in the UK, and so, as is our wont, on Friday evening we hopped in the car and headed off to the New Forest to go camping. We're ideally positioned for this, close enough to get out into the heart of the forest in 30 or 40 minutes (as long as we can dodge the bank holiday traffic). It means we can do a full day at work, but be pitched up with the dinner on and the first beer (always the best) in hand by 8 or 9 o'clock.

One of our favourite haunts is a site called Tom's Field. This is a field, owned by a bloke called Tom - the clue's in the name. It's low key, the facilities are basic but clean and well-maintained, and Tom and the loose confederacy who seem to run the place are friendly and helpful. Plus, of course, it's in the New Forest, with easy access to loads of walking, cycling, and pub lunches.

We had an excellent weekend, and by Monday lunchtime were packing up ready to go home and start preparing for the week ahead. Along with everybody else on-site.

Now, Tom owns a thing called a Gator. It's a little quad bike/tractor thingy that he uses for moving stuff around the site. The sort of thing you see farmers and groundsmen using.


On the Monday morning Tom went round the site telling all of the kids that if they put all the rubbish in the bins provided by the gate, they could have a ride on the back of the Gator.

This is genius. Sheer genius.

With this simple act Tom unleashed a veritable Tsunami of wombles who swept across the site like locusts, picking it clean of every last scrap of litter. In return for ten minutes of taking kids for joyrides, Tom saved himself several hours of tedious work.

I'm not entirely sure how to link this little story back into coding, the nominal topic of this blog. Somebody with more perspicacity and insight than me could doubtless create a compelling analogy to empowering the inherent synergy in a cross-functional team to achieve a win-win scenario. To using the carrot rather than the stick. To envisaging the desired outcome of a project and setting in motion the train of events necessary to manifest that version of reality.

Me, I'm just impressed that Tom gets his field cleaned for him every weekend. Tom, I salute you.