Lab 5: General Importance Style Conventions/Advice
Based on observing student's code in recent lectures and today's lab, Prof. Lyn would like to emphasize the following about generalk coding style guidelines for the course, as well as some guidelines specific to loops.
Familiarize yourself with the Code Style Guide
Especially if you're not familiar with it already, you should frequently consult the Code Style Guide for general guidelines about coding style for the course
General Naming Guidelines for Parameters and Local Variables
In general, you should avoid very short (especially single-letter) or otherwise unhelpful parameter and local variable names. There are a few exceptions: it's sometimes OK to use single-letter variable names (or single letters followed by a digit) in cases where the letter implies a type (but longer names are often more helpful). For example:
-
i,j,kfor integers, especially indices (butindorindexis better for indices) -
f,gfor floats -
n,mfor other numbers (butnumis typically better) -
sandtfor strings (butst,str, orstringis typically better) -
cfor a single-character string (thoughchancharare typically better) -
bfor booleans (but variants ofboolare better) -
Lfor lists. (Use an uppercaseLrather than a lowercasel, which can be hard to distinguish from the digit1. But variants oflstorlistare typically better. -
For dictionaries, use variants of
dctordict. - For tuples, use variants of
tpl.
Note it's often OK to use type names like int, str, bool, list,
dict (and variants thereof) also as parameter or local variable names.
But you do need to be careful not to shadow another use of these
name in your context. E.g., you can't use int, str, list, etc.
as a funtion parameter if you also want to call them as built-in
functions in the function body!
Specific Naming Guidlines for Sequences and Loops
-
When naming a list, either use a plural noun (e.g.,
nums,strings,words, etc.) or a single noun followed byList(e.g.numList,stringList,wordList). -
In a
forloop, using a singular noun (or variant thereof) that's related to the sequence name. E.g.:for num in nums: for val in valList: for word1 in wordList:This makes it very easy to connect the element to the sequence, but at the same time keep them distinct!
-
Some common special cases:
for index in range(...): for char in string: for letter in word:
-
-
When naming an accumulation variable, either use a generic name like
result,answer,ans, or something that's more specific to the type or purpose of the variable (e.g.count,sum,sumSoFar,newString,numVowels, etc. Prof. Lyn particularly likes the suffixSoFarbecause it emphasizes that the value is changing over time. -
Similarly, tracking variables and other local variables should be given helpful names, like
prev(orprevNum, etc.),next, ...- for variables with a boolean value, give them a name that makes this obvious,
e.g.,
isPositive,isPrevCharASpace,didBreak, etc.
- for variables with a boolean value, give them a name that makes this obvious,
e.g.,
Table of Contents
- Lab 5 Home
- Part 1: General Important Style Guidelines
- Part 1: Early Returns from Loops in a Function
- Part 2: Other Early Exits from Loops
- Part 3: Tracking Variables
- Part 4: for-based Index Loops
- Part 5: Advanced
forLoop Exercises - Part 6: Debugging Loops
- Part 7: Loops and Graphics(optional)