CMSC131 / CMSC132 Style Guidelines
Notice we have left some code style examples at the end.
- Good names for variables, constants and methods. Names should be descriptive and
should use camelCase.
- Consistent style for curly brackets (pick one style and stick with it). For example:
/* valid style */
if (x > y) {
m = 10;
} else {
m = 200;
}
/* valid style */
if (x > y)
{
m = 10;
}
else
{
m = 200;
}
- Use braces ({ }) and avoid loops and conditionals without them.
- Consistent indentation (2, 3, 4, etc. spaces). Remember to always use the same
number of spaces. Be careful with the tab key as your code indentation may
appear different when using different editors.
- In your code you should leave one blank space between binary operators (e.g., x = 5 + 7).
- About return statements
- Return at the end of a method is OK.
- Return at the beginning of method for erroneous input is OK.
- Define variables close to where you use them.
-
Make sure you define values as "symbolic constants" when needed.
Do not use numbers (literals) in your expressions if they have special
meanings. For example: Instead of using the literal "3.1415927"
in an expression, define and use a symbolic constant as follows:
public static final double PI = 3.1415927;
- You should avoid long lines of code (keeping them around 80 characters is recommended).
- The number of lines of your code can be reduced by defining variables
of the same type in the same line. For example:
int x;
int y;
int w;
float a;
float b;
float c;
/* alternative */
int x, y, w;
float a, b, c;
- Using i, j, k as the name of the iteration variable of a for
loop is fine although in some cases a descriptive name can
help (e.g., a nested loop to process a 2-dim array can benefit
from using row and col). Besides i, j, and k
avoid all non-descriptive variable names.
- Using break is OK, but do not abuse it.
- Using continue can be useful, but it should be rare.
- Do not call static methods by using a class instance.
- Remove from your code variables that are declared but never used.
- Remove any unnecessary import statements.
- You must avoid code duplication by calling appropriate methods
(rather than cutting and pasting code). You may define your own
private utility methods to perform often repeated tasks.
- Make sure you recognize when a field should be defined as private, protected,
public or package.
- The clarity of code can benefit from using empty lines between
segments of code. Make sure you take this into account while writing your
code. Similarly, comments in your methods are important in order to clarify
what your code is doing. Make sure you provide such comments when needed.
Methods (at the top) should include a description of the task performed by
the method.
- Simplify your code when possible. After implementing a method verify
if it can be implemented in a simpler way.
- Always strive for code that is neat, well organized and consistent.
-
Do not mix tabs and spaces for indentation. In the Eclipse IDE you can use the available indentation options.
Code Style Examples
See style examples.