CS313E Python Programming Hints

Dr. Bill Young

Below are some programming hints. I'll add others as I think of them.
  1. Do your design top-down in an object-oriented style.

  2. 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.

  3. 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.

  4. 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.

  5. Use procedural abstraction. If you have a block of code that does some well-defined function, make it a function / procedure.

  6. Each class should have an abstract interface; hide the details of the implementation as much as possible.

  7. 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.

  8. Make sure that every class has a __str__ function. This allows you to print class objects to see if they are as expected.

  9. If your function is returning None (and it's not supposed to), you probably left off the "return" statement.

  10. 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 interface.

  11. There should be minimal code outside of classes. If you have a bunch of helper functions, put them into a Utility class.

  12. 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.