Docsity

Coding Standards and Best Practices for Computer Science Students, Study notes of Computer Science

Toggle sidebar
bg1
Department of Computer Science University of San Francisco
Computer Science 112
Coding Standards
Spring 2008
1 Why have coding standards?
Code readability and maintainability are incredibly important. Coding standards are a
relatively easy way to increase the readability (and hence maintainability) of your code.
2 Identifier Naming
Method and variable names should start with a lowercase letter, and class names should
start with an uppercase letter Idendifiers are often multi-word descriptions of what they are
identifying, and uppercase letters are used to specify word breaks. For example, a variable
representing a first name might be called firstName. A class representing a bank account
might be called BankAccount. Identifier names should be descriptive enough for someone
reading the code to know what the variable is, and how it is being used, just by looking at
the name of the variable. Thus, variables like x, y, a, and bare usually poor choices, while
variable names like studentID, and playerName are better. Longer is not necessarily better,
however. Names like iand jare perfectly fine for loop control variables. Some examples:
•Bad: too sparce
int f (int a[], int b, int c)
{
int d = Integer.MAX_VALUE;
for (int i = b; i <= c; i++)
{
if (a[i] < d)
{
d = a[i];
}
}
return d;
}
•Bad: too verbose
int findTheSmallestValueFromLowTOHightInArray (int arrayToSeachForLowValue[],
int lowIndexOfArray,
int highIndexOfArray)
1
pf3
pf4

Partial preview of the text

Download Coding Standards and Best Practices for Computer Science Students and more Study notes Computer Science in PDF only on Docsity!

Department of Computer Science University of San Francisco

Computer Science 112

Coding Standards

Spring 2008

1 Why have coding standards?

Code readability and maintainability are incredibly important. Coding standards are a relatively easy way to increase the readability (and hence maintainability) of your code.

2 Identifier Naming

Method and variable names should start with a lowercase letter, and class names should start with an uppercase letter Idendifiers are often multi-word descriptions of what they are identifying, and uppercase letters are used to specify word breaks. For example, a variable representing a first name might be called firstName. A class representing a bank account might be called BankAccount. Identifier names should be descriptive enough for someone reading the code to know what the variable is, and how it is being used, just by looking at the name of the variable. Thus, variables like x, y, a, and b are usually poor choices, while variable names like studentID, and playerName are better. Longer is not necessarily better, however. Names like i and j are perfectly fine for loop control variables. Some examples:

  • Bad: too sparce

int f (int a[], int b, int c) { int d = Integer.MAX_VALUE; for (int i = b; i <= c; i++) { if (a[i] < d) { d = a[i]; } } return d; }

  • Bad: too verbose

int findTheSmallestValueFromLowTOHightInArray (int arrayToSeachForLowValue[], int lowIndexOfArray, int highIndexOfArray)

int smallestValueInArray = Integer.MAX_VALUE; for (int indexForArrray = lowIndexOfArray; indexForArray <= highIndexOfArray; indexForArray++) { if (arrayToSeachForLowValue[indexForArray] < smallestValueInArray) { smallestValueInArray = arrayToSeachForLowValue[indexForArray] < smallestValueInArray; } } return smallestValueInArray =; }

  • Good

int smallestValueInRange(int array[], int lowIndex, int highIndex) { int smallest = Integer.MAX_VALUE; for (int i = lowIndex; i <= highIndex; i++) { if (array[i] < smallest) { smallest = array[i]; } } return smallest; }

3 Indendation

Code between an open brace ({) and the corresponding close brace (}) should be indented a consistent number of spaces. This applies to if statements, loops, method definitions, and class definitions. The close brace should always be lined up with the header of the statement. The open brace be on a new line, like the code examples above.

4 Spacing

  • Put one space after each comma in a parameter list.
  • Put one space on either side of a binary operator.
  • Do not put spaces immediately after a left parenthesis or before a right parenthesis.
  1. Robustness - It is important to ensure that your programs are robust to failures and errors, and handle all situations gracefully. Among other things, this means that your design should limit the number of errors that might be triggered by misbehaving code. Encapsulation, a fundamental object-oriented principle, refers to limiting how data is modified by making data members private and allowing access to and modification of the data only through limited methods. It is one way to help ensure that your programs are robust.
  2. Extensibility - Anyone with a computer knows that software updates happen contin- uously. Software is continually being extended to support new functionality. Clearly, in order to avoid having to rewrite a program from scratch each time it is updated, a design should easily support integration of new classes.
  3. Modularity - Designing a program as a set of interoperating yet independent modules makes development and maintenance much simpler. It can be very intimidating to think about writing a program containing hundreds or thousands of lines of code. However, if a programmer takes the time to design the program as several modules that can be implemented and tested independently, the task of implementation becomes much more manageable.
  4. Efficiency - The easy way to do something is not always the most efficient. It is important to understand the resources required to accomplish a task and to ensure that your algorithms make efficient use of those resources. We’ll touch on this topic in CS 112, but you will learn more about this in CS 245.

6.2 Design Techniques and Tools

Design really is a creative process and each person may approach the process differently. For the purposes of this class, the outcome of the design process will be a set of classes you need to implement, the data and methods each class will contain, and and overview of how the classes will interact. Larger programs may have more complex design issues and the first outcome of the design process will be a system architecture with multiple components for which a more refined design will then be developed. Keep in mind that an initial design is unlikely to be flawless. You will almost certainly need to go back and refine or change your design as you implement your program.