University of Arizona, Department of Computer Science

CSC 120: Programming Style

Programs are read much more often than they are written, so it is important to pay attention to its style. This document lists (some of) the style requirements for programming assignments in this class. This list is non-exhaustive: you will be penalized if you violate these requirements; however, you may also be penalized if your code uses poor style (in the latter case, you will get a warning first, and penalized if the style does not improve in subsequent assignments).

For CSc 120, we will generally follow the style guidelines given in PEP 8 -- Style Guide for Python Code. In particular, you should adhere to the rules given below.

NOTE: These rules will be expanded as the semester progresses and we cover new topics. When this happens, you will be informed of the change.

Docstrings

The style guidelines below refer to docstrings. A docstring for a function is a special kind of string that begins with, and ends with, three double-quote (") characters, and which provides documentation about the program entity it is attached to. Examples of docstrings are available here.

I. What you write

  1. Layout:

  2. Comments: Your code should be appropriately commented.
  3. Naming conventions:

  4. List comprehensions:

II. How you write

  1. Avoid redundancy. If you see the very similar code sequences—i.e., code that either looks similar, or behaves similarly—appear in more than one place, consider pulling them into a function.

    Exception: Small fragments of trivial code, e.g., two or three statements to initialize or increment a few variables.

  2. Avoid unnecessary complexity. Everything else being the same, simple code and data structures are better than complicated ones. Therefore, If a data structure or algorithm discussed in class (lectures or discussion sections) does what you need to do, then—unless explicitly required by an assignment spec—you should not resort to something significantly more complex.