Docsity

Java Programming Style Guidelines: File Headers and Block Style, Study notes of Computer Science

Toggle sidebar
bg1
Java Style Guidelines
File Headers
The following should appear at the top of each source file:
// <file name>
// CS414, Assignment #x
// <Student Name>
// <email address>
/*
This should be a comment describing the contents of the file.
List all of the classes by access specifier as follows:
Public Classes:
Class1 - a brief description
Class2 - a brief description
Class3 - a brief description
Protected Classes:
None.
Private Classes:
AprivateClass - a brief description
*/
If you use a version control system that inserts revision comments directly into your file
(a good idea!), have them inserted at the end of the file after the source code. The
rationale for this is that the clarity and readability of the program are just as important as
the semantics that it embodies. Placing revision comments near the top of the file only
makes it more difficult to find things. Each time a revision is made, a new set of
comments (presumably) are added which pushes everything that follows further down,
breaking whatever conceptual integrity the reader has about the file. What is important is
that the comments be present in the file, assessable if needed. Placing them at the end
achieves this and makes the file easier to read.
Block Style
Use the following style of indentation and brace alignment (indentation levels should be
four spaces): Please do not use tabs.
class SomeClass
{
int fSomeStateVariable = 0;
// Invariant( fSomeState >= 0 && fSomeState < 10 )
public void foo()
{
pf3

Partial preview of the text

Download Java Programming Style Guidelines: File Headers and Block Style and more Study notes Computer Science in PDF only on Docsity!

Java Style Guidelines

File Headers

The following should appear at the top of each source file:

//

// CS414, Assignment #x // //

/* This should be a comment describing the contents of the file. List all of the classes by access specifier as follows:

Public Classes: Class1 - a brief description Class2 - a brief description Class3 - a brief description

Protected Classes: None.

Private Classes: AprivateClass - a brief description */

If you use a version control system that inserts revision comments directly into your file (a good idea!), have them inserted at the end of the file after the source code. The rationale for this is that the clarity and readability of the program are just as important as the semantics that it embodies. Placing revision comments near the top of the file only makes it more difficult to find things. Each time a revision is made, a new set of comments (presumably) are added which pushes everything that follows further down, breaking whatever conceptual integrity the reader has about the file. What is important is that the comments be present in the file, assessable if needed. Placing them at the end achieves this and makes the file easier to read.

Block Style

Use the following style of indentation and brace alignment (indentation levels should be four spaces): Please do not use tabs.

class SomeClass { int fSomeStateVariable = 0; // Invariant( fSomeState >= 0 && fSomeState < 10 )

public void foo() {

// Method description: This method… // Require( conditions that must be true to call method ) // Ensure( conditions that must be true after method has // executed)

int i;

for ( i = 0; i < 10; ++i ) { System.out.println( “Line “ + i );

if ( i % 2 == 0 ) { // Print a blank line. System.out.println( “” ); } } } // public void foo()

} // class SomeClass

In general, both opening and closing braces should line up with the first character of the keyword (e.g. if, while, for, etc.) that they correspond to. Statements within the block should be indented four spaces.

Note the comments on the closing braces of the class specification and the function definition. These are particularly important as an aid to finding things in the file and for maintaining one's orientation. The commenting convention includes the complete signature for the class and the function. This crucial for distinguishing overloaded method names.

Variables

  • The names of class state variables (fields) should begin with “ f ”, and each word in the name should be upper case, as in fCurrentSpeed. This convention is borrowed from the old Taligent guidelines for C++. The rationale is that the prefix makes it easier to identify instance variables that form part of the state space of the enclosing class.
  • Static fields should begin with the prefix “ fg ”: fgNumberOfInstances. This makes it easier to distinguish class (i.e. static) variables from instance variables.
  • Local variables defined within a block (e.g. function, condition, etc,) should have the first word of the name all lower case, which each remaining word beginning with an upper case letter: thisIsLocal
  • Place all state variables at the top of the class before any of the methods. Order them according to their access specifiers, with public appearing first, followed by protected , and finally private. Place class variables (those with the static modifier) before instance variables.

Methods