Skip to content

Code Conventions for VIP Projects: Java.

Sara Ortiz Drada edited this page Oct 8, 2018 · 14 revisions

Content

  1. Introduction
    1. Importance of having Code Conventions
  2. File Basics
    1. File Names and Suffixes
  3. File Structure
    1. License or Copyright Information (if present)
    2. Java Source Files
      1. Documentation Comments
      2. Package and Import Statements
      3. Class and Interface Statements
  4. Formatting
    1. Braces
    2. Block Identation
    3. Lines
    4. Whitespace
    5. Specific Constructs
  5. Naming Conventions
    1. Common Rules
    2. Rules by Identifier
  6. Comments
    1. Block Comments
    2. Single Line Comments
    3. Trailing Comments
    4. End of Line Comments
    5. Documentation Comments
  7. Statements
    1. Simple Statements
    2. Variable Declaration Statements
    3. return Statements
    4. Conditional Branching Statements
      1. if Statements
      2. if-else Statements
      3. if else-if else Statements
    5. Iteration Statements
      1. for Statements
      2. while Statements
    6. switch Statements
    7. try-catch Statements
  8. Programming Practices
    1. Access to Instance and Class Variables
    2. @Override tag
    3. Caught Exceptions
    4. Expressions before `?' in the Conditional Operator
  9. Bibliography

Introduction

This document has the purpose to define VIP's (ICESI) coding standards for source code in the Java™ Programming Language for all of it's projects.

Importance of Having Code Conventions

Code Conventions are key in the software structural quality.
Programmers are recommended to use this guidelines because:

  • They improve readability, allowing programmers and engineers to understand new code easily.
  • Most of the lifetime cost of a software goes to maintenance so it's important to have guidelines to help understand it and make the process easier.
  • There is almost a zero chance that the original author is going to maintain the software for its whole life.z

File Basics

Source File Names and Suffixes

For Suffixes, Java uses:

File Type Suffix
Java source .java
Java bytecode .class

The source file's name consists of the case-sensitive name.

File Structure

License or Copyright Information

If there exist license or copyright information, it belongs in this file.

Java Source Files

A source file consists of:

  1. License or copyright information, if present
  2. Package statement
  3. Import statements
  4. Exactly one top-level class
  5. Exactly one blank line separates each section that is present.

Documentation Comments

Every source files begin with a c-style comment that has the class name, version information, date, and copyright information:

Package and Import Statements

After the documentation comments, the first line of a Java source file is a package statement followed by import statements.

Class and Interface Statements

Parts of a class or interface declaration, in the correct order:

  1. Class/interface documentation comment ( /**...*/)
  2. Class/interface statement
  3. Class/interface implementation comment ( /*...*/), if necessary
  4. Class (static) variables
    1. Public class variables
    2. Protected class variables
    3. No access modifier varibles
    4. Private variables

Formatting

Braces

Where to Use Braces

Braces are used with conditional (if, if-else, if else-if else), iteration (for, do and while) and try/catch/finally statements, even when the body is empty or contains a single statement.

Empty Blocks

An empty block should follow the Kernighan and Ritchie style:

  • No line break before the opening brace.
  • Line break after the opening brace.
  • Line break before the closing brace.
  • Line break after the closing brace, only if that brace terminates a statement or terminates the body of a method, constructor, or named class. There is no line break after the brace if it is followed by else or a comma.

For empty blocks braces may be closed immediately after they are opened, with no characters or line break in between, only if they aren't part of a multi-block statement.

Nonempty Blocks

Nomempty blocks also follow the Kernighan and Ritchie style.

Block Identation

The ident increases by two for every new block or block-like construct. Once the block ends the indent goes back to the previous indent level.

Lines

One Statement per Line

Each statement is followed by a line break.

Line Length

Each line should have a shorter length than 80 characters.

Line Wrapping

General rules to break an expression that doesn't fit on a single line:

  • Prefer higher syntactic level breaks.
  • Break after a comma.
  • Break before an operator.
  • Align the new line with the beginning of the expression at the same level on the previous line.

Whitespace

Blank Lines

A blank line always has be used in the following circumstances:

  1. Between sections of a source file to improve
  2. Between class and interface definitions
  3. Between methods
  4. Between logical sections inside a method to improve readability
  5. Before a block or a single line comment
  6. Between statements

Multiple consecutive blank lines are permitted, but not required.

Blank Spaces

A blank space always has be used in the following circumstances:

  1. Separating any reserved word,from an open parenthesis that follows it on that line
  2. Note that a blank space should not be used between a method name and its opening parenthesis.
  3. After commas in argument lists.
  4. Separating any reserved word, such as else or catch, from a closing curly brace (}) that precedes it on that line.
  5. On both sides of any binary or ternary operator. Except (.)
  6. Blank spaces should never separate unary operators such as unary minus, increment ("++"), and decrement ("--") from their operands.
  7. Writing the expressions in a for statement
  8. On both sides of the double slash (//) that begins an end-of-line comment.
  9. Casts should be followed by a blank space.

Specific Constructs

Variable Declarations

One variable per declaration.

Every variable declaration (field or local) declares only one variable: declarations such as int a, b; are not used.

Iteration Statements

For iteration statements multiple variable declarations are acceptable in the header, and are required to be written in capital letters.

Where to write the declaration

Local variables are habitually declared at the start of their containing block or block-like construct.

Arrays

Initializers

Any array initializer may optionally be formatted as if it were a "block-like construct."

Declarations

The square brackets form a part of the type, not the variable: String[] args, not String args[].

Naming Conventions

Common Rules

Identifiers use only ASCII letters and digits, and, in a small number of cases noted below, underscores.

Rules by Identifier

Identifier Type Rules for Naming
Packages Package names are all lowercase ASCII letters, with consecutive words simply concatenated together (no underscores).
Classes Classes names should be nouns, in mixed case with the first letter of each internal word capitalized.
Interfaces Interface names may be nouns or noun phrases, but may sometimes be adjectives or adjective phrases instead. They should be capitalized like class names.
Methods Method names are typically verbs or verb phrases, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
Constants Constant names should be all uppercase with words separated by underscores ("_").
Variables Variables names should be all lowercase, with the exception of variables in the header of iteration methods. They should not start with underscore _ or dollar sign $ characters, and even though both are allowed we do not encourage them.
Parameters Parameter names should be all lowercase. One-character parameter names in public methods should be avoided.

Comments

Block Comments

Block comments are used to provide descriptions of files, methods, data structures and algorithms. Block comments may be used at the beginning of each file and before each method.

A block comment should be preceded by a blank line to set it apart from the rest of the code.

Single Line Comments

Short comments can appear on a single line indented to the level of the code that follows. If a comment can't be written in a single line, it should follow the block comment format (see previous definition).

A single-line comment should be preceded by a blank line.

Trailing Comments

Very short comments can appear on the same line as the code they describe, but should be shifted far enough to separate them from the statements.

End of Line Comments

The // comment delimiter can comment out a complete line or only a partial line. It shouldn't be used on consecutive multiple lines for text comments; however, it can be used in consecutive multiple lines for commenting out sections of code.

Documentation Comments

See section: 3.ii.a Documentation Comments

Statements

Simple Statements

Each line should contain at most one statement.

Variable Declaration Statements

See section: 4.v. Specific Constructs

return Statements

A return statement with a value should not use parentheses unless they make the return value more obvious in some way.

Conditional Branching Statements

if Statements

The if statements should have the following form:

if-else Statements

The if-else statements should have the following form:

if else-if else Statements

The if else-if else statements should have the following form:

Iteration Statements

for Statements

A for statement should have the following form:

An empty for statement should have the following form:

while Statements

A while statement should have the following form:

An empty while statement should have the following form:

switch Statements

A switch statement should have the following form:

Every switch statement should include a default case. The break in the default case is redundant, but it prevents a fall-through error if later another case is added.

try-catch Statements

A try-catch statement should have the following form:

A try-catch statement may also be followed by finally, and it should have the following form:

Bibliography

Clone this wiki locally