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, |
(written 2013-11-13, updated 2013-11-16)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q101 - Object Orientation and General technical topics - Programming Principles [2001] I have not programmed before, and need to learn - (2009-01-19)
[2022] Pre and post increment - the ++ operator - (2009-02-03)
[2228] Where do I start when writing a program? - (2009-06-11)
[2310] Learning to write high quality code in Lua - (2009-07-30)
[2327] Planning! - (2009-08-08)
[2415] Variable names like i and j - why? - (2009-09-22)
[2510] The music of the stock market - (2009-11-22)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2586] And and Or illustrated by locks - (2010-01-17)
[2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
[2769] Easy - but for whom? - (2010-05-18)
[2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
[3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
[3456] Stepping stones - early coding, and writing re-usable code quickly - (2011-09-24)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
[3878] From Structured to Object Oriented Programming. - (2012-10-02)
[3928] Storing your intermediate data - what format should you you choose? - (2012-11-20)
[3954] Lesson 1 in programing - write clean, reuseable and maintainable tidy code - (2012-12-16)
[4003] Web and console - same principle, same code - Ruby example - (2013-02-14)
[4061] Seamless, integrated IT - we have a long way to go! - (2013-04-11)
[4090] Test Driven Development in Python - Customer Comes First - (2013-05-16)
[4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
[4153] Rooms available tonight - how to code an algorithm from first principles - (2013-08-19)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4611] Hungarian, Camel, Snake and Kebab - variable naming conventions - (2016-01-03)
[4632] Remember to ask the question before you listen for the answer - (2016-01-26)
[4645] What are callbacks? Why use them? An example in Python - (2016-02-11)
T050 - Tcl/Tk - Tcl, Tcl/Tk and Expect - General [2429] Tcl scripts / processes on a web server via CGI - (2009-09-27)
[2474] Using Tcl and Expect to automate repetitive jobs - (2009-10-24)
[2504] Learning to program in ... - (2009-11-15)
[2681] Tcl - a great engineering language - (2010-03-17)
[3192] Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04)
[3286] Should we cover expect and/or Tk on our public Tcl courses? - (2011-05-11)
[4434] Public training courses - upcoming dates - (2015-02-21)
[4460] Using Object Oriented Tcl and the Tk toolkit together - real life example - (2015-03-12)
[4616] Still teaching Tcl in 2016? - (2016-01-06)
Some other Articles
If elif elif elif - multiway selection in PythonLists in Tcl - fundamentals in a commented source code exampleTcl + Tk (Wish) - an introduction and revision exampleException handling in TclWriting the perfect program in Tcl?Regular Expression Substitution - TclWith the Shpigs and DonkeysA weekend away - Four in a Bed returns to Happy Donkey HillExcellent Railfuture conference, but some setup lessonsLooking to the future at Melksham Station