[Tutor] Sorting a dictionary in one line?
alan.gauld@bt.com
alan.gauld@bt.com
Wed Jan 22 06:17:01 2003
> code others have written. One thing I observe is that comments
> often do not match the code because code (even with good
> design practices) tends to evolve,
One reason for this is that the commenting is often bad.
It often describes *how the code works* rather than *what*
the code does, or *why* the code is there!
The excellent book "Code Complete" has much to say on the subject
of good commenting and I commend it to every programmer.
Example: Here is a bad comment....
# open a file, read the content then extract the third field
f = open('pay1232003.txt','r')
txt = f.read().split()
val = txt[2]
f.close()
A better, and more robust, comment might say:
# extract the salary value from the payments file
Of course if I had used better names it would help too:
f = open(payment_file,'r')
content = f.read().split()
salary = content[2]
f.close()
Good commenting and coding generally is an art as well
as a science.
Unfortunately a list like this tends not to teach
good commenting since we use comments to explain
our code samples - which is exactly what you should
NOT do in real world code...(unless you are using
some dangerous or obscure coding idiom).
Alan g.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld/