CMSC131 / CMSC132 Style Guidelines

Notice we have left some code style examples at the end.

  1. Good names for variables, constants and methods. Names should be descriptive and should use camelCase.
  2. 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;
                        m = 200;
  3. Use braces ({ }) and avoid loops and conditionals without them.
  4. 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.
  5. In your code you should leave one blank space between binary operators (e.g., x = 5 + 7).
  6. About return statements
    • Return at the end of a method is OK.
    • Return at the beginning of method for erroneous input is OK.
  7. Define variables close to where you use them.
  8. 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; 
  9. You should avoid long lines of code (keeping them around 80 characters is recommended).
  10. 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;
  11. 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.
  12. Using break is OK, but do not abuse it.
  13. Using continue can be useful, but it should be rare.
  14. Do not call static methods by using a class instance.
  15. Remove from your code variables that are declared but never used.
  16. Remove any unnecessary import statements.
  17. 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.
  18. Make sure you recognize when a field should be defined as private, protected, public or package.
  19. 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.
  20. Simplify your code when possible. After implementing a method verify if it can be implemented in a simpler way.
  21. Always strive for code that is neat, well organized and consistent.
  22. 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.

Web Accessibility