CS313E Python Programming Hints
Dr. Bill Young
Below are some programming hints. I'll add others as I think of them.
- Do your design top-down in an object-oriented style.
- Write and test your program in pieces. If you write your whole
program and just expect it to work, you're probably out of luck, and
finding the problems may be much harder. Don't think you're saving
time by writing more before you test; you're not.
- Break your program into conceptually manageable chunks. If a
function (def) is more than 20 lines of code (not comments), it's
probably too long.
- Generate reasonable test data. If your program is going to run
on 1000's of inputs, test it first on 20 inputs. If it won't work on
20, it won't work on 1000's.
- Use procedural abstraction. If you have a block of code that does
some well-defined function, make it a function / procedure.
- Each class should have an abstract interface; hide the details of
the implementation as much as possible.
- Use print statements liberally. When trying to debug a function,
check to make sure that you are getting the data in that you are
expecting and producing the intermediate results that you expect.
- Make sure that every class has a __str__ function. This allows you
to print class objects to see if they are as expected.
- If your function is returning None (and it's not supposed to), you
probably left off the "return" statement.
- Comment your code liberally. Each function should have an initial
comment that explains the purpose, calling conventions, and expected
return type of the function. Each class should have an initial
comment that explains what the class is for and what's in the
- There should be minimal code outside of classes. If you have a
bunch of helper functions, put them into a Utility class.
- Make your program as general as possible. For example, if your
program manipulates strings of length 5, make it operate on strings of
length n and make 5 a global constant.