CmSc 150 Fundamentals of Computing I


Coding Style

Good coding style is very important for easy understanding what your program is about. Here are some recommendations.

  1. Comments
    1. Each Java program must start with a comment block describing what the program is about, stating the author and the version of the program.
    2. Methods must be preceded by a comment block in the form:
      /*--------------- name of the method ----------------------------*/
      // Purpose::
      // Input :
      // Output:
      // Methods used:
      /*---------------------------------------------------------------*/
    3. All declarations within a method/class should be grouped together. Each should be on a separate line, accompanied by a comment describing the purpose of the declared variable.
    4. The code for each basic step in the implemented algorithm should be preceded by a comment describing the purpose of that code.
    5. Example

      The algorithm for computing the average of list of numbers has two steps:

      1. Finding the sum of all numbers
      2. Dividing the sum by the number of the elements.

      The code should look like this:

      
      // Finding the sum of the elements in the array XXXX
       ……. Here comes the code that finds the sum
      
      // computing the average
      …….. Here comes the code that computes the average
  2. Identifiers
    1. Class names start with a capital letter
    2. Names of methods and variables start with a lower case letter
    3. Names of constants should be all capital letters
    4. Names should be meaningful. For example, if your program has a method that processes withdrawal from a bank account, the name of the method should be “withdrawal”.
    5. Composite names are recommended to provide more meaning. Such names can be form by either writing the second word in the name with a capital letter, e.g. “checkingAccount”, or using an underscore as a delimiter , e.g. “checking_account”. Whatever option you choose, follow it.
  3. Curly braces
  4. There are several ways to write curly braces:

    1. while(......){
       …..
      }
    2. while(......)
       {
              …..
       }
    3. while(......)
           {
                     ……
           }

    Make a choice and stick to it. My personal preference is b).

  5. Indentation
  6. The code within the curly braces should be indented. Use 2 to 8 spaces for indentation.

  7. Statements
    1. There should be only one statement per line
    2. Statements should be less than 80 characters long

  8. Operators
  9. Operators should be separated from their operands by a space.

    Example:

    a = b + c;
    do not write a=b+c;

  10. Constants
  11. Except for 0 and 1, all numerical constants in the program should be named.

    Example:

    
    int MAX_SIZE = 1000;
                 ……..
    for( i = 0; i < MAX_SIZE; i++)
                 …….
    
  12. Spelling
  13. Your program should have correct spelling.


Lydia Sinapova