Every programming department has a set of standards or conventions that programmers are expected to follow. The purpose of these standards is make programs readable and maintainable. After all, you may be the programmer who maintains your own code more than 6 months after having written the original. Programming standards vary by department; therefore, it is important that all members of the department follow the same standards.
At UMBC, the following standards have been created and are followed in CMSC 201.
Part of every project and homework grade is how well these standards are followed.
It is your responsibility to understand these standards. If you have questions, ask any of the TAs or the instructors.
The prudent use of whitespace goes a long way to making your program readable.
Horizontal whitespace (spaces between characters) will make it easier to read your code. Vertical whitespace (blank lines between lines of code) will help you to organize it.
o For example, write x = y + 5, NOT x=y+5.
To improve readability, you should use constants whenever you are dealing with hardcoded values. Your code shouldn't have any "magic numbers," numbers whose meaning is unknown.
total = subtotal + subtotal * .06
In the code above, .06 is a magic number. What is it? The number itself tells us nothing; at the very least, this code would require a comment. However, if we use a constant, the number's meaning becomes obvious, the code becomes more readable, and no comment is required.
Constants are typically placed near the top of the program so that if their value ever changes they are easy to locate to modify. Constants may be placed outside of the main() function – this makes them global constants, which means everything in the file has access to them. (Global variables are only allowed for constants!)
Here’s the updated code:
TAX_RATE = .06 # TAX_RATE is a global constant
# lots of code goes here
total = subtotal + subtotal * TAX_RATE
# other code goes here
print("Maryland has a sales tax rate of", TAX_RATE, "percent")
Programmers rely on comments to help document the project and parts of the project. Generally, we categorize comments as one of three types:
Every file should contain a comment at the top describing the contents of the file and other pertinent information. This "file header comment" MUST include the following information. The file name
# File: proj1.py
# Author: Katherine Gibson
# Date: 10/14/2015
# Section: 1
# E-mail: email@example.com
# This file contains python code that will simulate a run of
# Conway's "Game of Life". It allows the user to choose # which cells to turn on, and for how many iterations it is run.
Every single function must have a header comment that includes the following:
# circleArea() calculates the area of a circle from the radius
# Input: the circle's radius # Output: the circle's area
In-line comments are comments within the code itself. They are normally comments for the line of code directly below them.
Well-structured code will be broken into logical sections that perform a simple task. Each of these sections of code (typically starting with an 'if' statement, or a loop) should be documented.
An in-line comment appears above the code to which it applies and is indented to the same level as the code.
# check every member of a list for num in numList :
# if it's odd, print it, if even, do nothing if num % 2 == 1 :
Python has many useful built-in functions that easily let a programmer perform a variety of tasks. Unless specified by the assignment, you are not to use any of the built-in functions we have not covered in class.
Using a built-in function to solve a homework problem for you does not show that you have mastered the concepts behind it, and hence does not fulfill the assignment.
Using break and continue is not allowed in any of your code for this class. Using these statements damages the readability of your code. Readability is a quality necessary for easy code maintenance.
Assignment Writing Help
Engineering Assignment Services
Do My Assignment Help
Write My Essay Services