Thursday, 14 May 2009

Writing: A case for code reuse

The May issue of ACCU's CVu magazine is hitting doormats right now, and contains my latest Professionalism in Programming column, called "A case for code reuse". I present a number of "re-use" cases (pun intended), and show a real use for your old code - as a tool to help you write better code in the future.

This is another excellent issue of CVu. Edited by Steve Love (don't ask how many beers contributed to his picture on the editorial page), there are some great articles on subjects including: distributed version control, job hunting, and the D programming language (this latter one contributed by Andrei Alexandrescu).

Friday, 8 May 2009

Const-correctness for documents

Const correctness is the art of using the type system of a static programming language (conventionally C or C++) to ensure that immutable objects are never modified, and mutable ones... can be.

It's a real boon to ensure program correctness. If you don't want your data fiddled with then you declare it (or any references you give to it) const, so that users of said data cannot change it. Note that you can give away a const reference to mutable data - in this case the user cannot change it, but the you may do so behind their back.

And now, in the Real World...

I'm getting fed up with the sloppy business practice of sending around copies of Microsoft Word documents as email attachments. Everyone does it. So it must be OK. Right?

No. It's cobblers. For a start, I don't have a copy of Word. I know that makes me rare, but there are a few freaks like me out there. Although Open Office is good, it's never perfect at rendering complex word documents.

However, I'm more worried about the distribution of a mutable document (the Word document itself). Sending a .doc to people who can modify it in ways that are not easily traceable, and then send it onwards is not ideal. If the document proudly proclaims that you are the author, then this should scare you rigid. Some muppet in marketing could change your text beyond all recognition - make it factually incorrect, but not change the author line - and then publish it to the world, making you look a total plonker in the process.

Ensure the const-correctness of your documents.

When you distribute any document that should not be modified by others, send around a PDF copy. OK, PDFs can be annotated, but those annotations are clear and cannot easily be mistaken for part of the original document. Of course, some devious idiot can make a convincing variant, but then a devious maintenance programmer could always const_cast your data's immutability away.

PDFs are the mainstay of const-correct document control.

Bonus points: yes, this is not entirely the same as my programming language analogue. In C or C++ a user could make their own copy of your const data and modify it themselves. Fair game. You can also give away a const reference to some data, but still change that data yourself. How would you do these things in the document? Perhaps consider a URL as your document reference?

Legacy Code: Learning to live with it

The slides from my ACCU 2009 conference presentation, Legacy Code: Learning to live with it, are now available from the conference website, here.

Click on {Slides} link to download a PDF copy. In the comfort of your own home, you too can enjoy theology, underwear, frogs, a shopping list or two, and a whole pile of dodgy code.