Programming style
Below are some basic and important guidelines. Note that early in the semester we are not too concerned with efficiency.
- Documentation (comments)
- Begin each script with a concise comment describing the program.
- In a function, the function comment follows the function header and gives the specifications concisely, including the descriptions of the parameters.
- Code should be sufficiently, but not excessively, commented. For example, define constants and important variables with comments and write a concise comment for each individual block of code. Do not comment every line of code.
- General style
- Use meaningful variable names; they do not need to be long.
- Name important parameters as variables.
- Use proper indentation.
- Line lengths should not be excessively long--a length of 80 columns is reasonable.
- Use semi-colons as needed and never at ``mid-statement'' such as the end of these lines: if, elseif, else, for, and while. Note the distinction between the end of a line and the end of a statement.
Here is an example with one ill-placed semi-colon and one correctly placed semi-colon:
if x < y; % Semi-colon is ill-placed at the end of a % line--the if statement isn't complete here x= x*2; % Semi-colon is correctly placed at the end of % a statement end
- Conciseness and efficiency
- There should be no superfluous code (e.g., an empty if or else branch or a useless loop)
- Code should be reasonably efficient. (Note that optimal efficiency is not required; reasonably efficient code is expected.)