20130118
The
computer does not care if we use structured program or spaghetti code.
The reason we choose to follow programming principles is so that
our programs can be more easily understood by human beings.
Sometimes the people who are trying to understand how and why our
programs work are ourselves, looking back at programs we wrote years
earlier.
Over the years people have developed a set of
guidelines for writing programs that are easy to understand.
Collectively these make up what is usually called good programming style. It includes using structured programming (at least in procedural languages like Python), as well as ...
Meaningful Identifiers
Variables
and functions should have names that indicate their purpose, such as
class_average or total_expenses for variables, and such as
find_second_largest() for functions
Visual Organization of Program
Indentation
should be consistent. Statements that are part of a larger
statement (such as the statements inside a loop or an if) should be
indented further than the line that initiates the control structure.
Vertical
spacing should be generous and consistent. For example you may
wish to put an empy line before and after each loop, and before and
after each function defmition
Internal Documentation
Python
allows us to place a comment anywhere in a program by using the #
character. Everything following this character on the same line
is treated as a comment rather than as an instruction.
Every
program should have comments at the beginning naming the author, giving
the date the program was written, and briefly describing its purpose.
If the program has been modified at a later date, the date and
nature of the modifications should be described at the top of the
program also.
Every function should have a comment just after
its header, explaining its purpose. If the function's purpose is
completely expressed by its name (for example
compute_final_course_grade()) then this comment may not be necessary.
Even in such a simple case as this you might want to include a
comment describing the parameters the function needs, and the value or
values it will return.
Every major block of code should have a comment describing its role in the algorithm.
Long
loops (ie. loops that spread over more lines than will fit on a typical
screen) should have a comment after their final line saying something
like "# end of 'while x <= 10 loop' "
In Python there is no
key word or symbol that shows the main program is beginning. It
is a very good idea to add a comment such as " # main program"
before the first line of the main program.
Program Organization
One of the most useful and popular techniques for creating programs that are easy to write and easy to read is called top-down programming or stepwise refinement. The basic idea is first to write down our algorithm in general, natural terms such as
change all the red pixels in this image to blue
then refine that by thinking of the basic control structures that we want to use, such as
for each pixel in this image
if the pixel is red
change it to blue
then refine that a bit further
for each row of the image
for each pixel in that row
if the pixel is red
change it to blue
and continue
the refinement process until we are actually writing lines of code.
Often during this process we discover repeated sequences of
operations - these can be "pulled out" and written as functions.
You will probably find that some lines in your stepwise
refinement become comments in your program. Indeed, some people
do their stepwise refinement using a tool like SciTE, turning some of
their descriptive lines into comments as they write the final Python
version of the program.
EasyGUI
We
looked at a Graphical User Interface package called EasyGUI - a free
download for Windows and Mac (and probably Linux too). I will be
using it for a lot of examples during class and I think you will find
it very useful. However if you don't want to use it, that is
fine. I expect you to know it but I won't test you on it.