Main Content

Writing the perfect program in Tcl?

Archive - Originally posted on "The Horse's Mouth" - 2013-11-13 12:21:54 - Graham Ellis

I'm going to disappoint you... there's no such thing as the perfect program. But when I'm writing an answer to a class exercise and showing a sample, I have to make sure it's to a high standard - and that means at least:
• Well commented
• Using good variable names
• Avoiding repetition of constants
Ideally, it would also be
• Robust to any incorrect inputs
• Avoids repeating code (no cut and paste!)
• Efficient in operation
But those latter three elements may require elements of coding (such as procedures and regular expressions) that I may not have taught early on.

So why isn't there a single perfect way to write a program? Consider this...
• Who is going to maintain the program? Does it need to be written for a novice maintainer using simpler structures and lots of comments, or for a an experienced Tcl coder who can easily read some of the more sophisticated regular expression syntax?
• There may be a compromise between speed of operation and ease of reading. Which should be selected?
• Should the code be strongly structured to allow code re-use, or should it be tuned to reduce dependencies on other modules and file - for safer distribution
• May you assume that it will only run on recent versions of Tcl and code using newer features, or is there a realistic chance that the code will need to be run on older systems too. Perhaps your version of Postbuilder isn't yet using Tcl version 8.5, for example
• Should I write my code so that it can all be see on a single page / projector screen by my delegates or should I spread it out so that it can be better understood later when it's posted at places like this for the benefit of people who've not been on the course?
I can't give you a [wrong/right] directive, in general term, on any of the above. But what is certainly right is to make our delegates aware of the issues so that they write their own code with a knowledge and maturity that takes such things into consideration - with an objective of writing code efficiently, and code that's easy to use and easy to maintain thereafter.

Want so see what I mean? There's an example of a quite early answer written during class [here], and a later one [here] which has common logic for user input separated out into a more robust common function loaded from [here]. I'm not saying either is "right", but I prefer the second example because it's more forward useful, and I prefer the first example because my delegates can get to see something fairly effective early on.

Examples from the recent public Tcl Course I ran at our training centre in Melksham. The course is a niche one, and we get delegates from far and wide (you are required to be fluent in English to attend - otherwise it's not fair on other delegates. Clearly the course was a hit... may I share a follow-up letter with you:

Dear Graham,

I am already back in Turkey, comfortably and without any problems.

I send this mail to you to thak once againg for your informative training, help on other subjects (like public transport J), friendship and hospitality. I was very lucky to work with you and I thank you very much for everything.

I wish you, your family, Wellho Ltd. and Melksham the best for the following times.

Best Regards,