JavaStandards.doc

advertisement
Java Standards
Complements of Ron Zucker
Contents:

Introduction

Naming Conventions

Indentation

Comments

Samples

Recommendations
Introduction.
The following standards have been developed to create a consistent look and feel to the Java language
code. Adherence to these standards will not only create a professional looking product it will also be a
major factor in documenting, debugging, and maintaining your code. The Java Development Kit (JDK)
provides a program, called javadoc, which will create an HTML document. This document is consistent
with the API documentation and has a very professional appearance. You may see a sample of a Java
application that follows the standards and the documentation provided by the javadoc program in the
samples section of this document.
Naming Conventions.
User defined names must be descriptive and adhere to the following conventions:
1.
Class names: Each word in a class name must begin with an uppercase letter. Examples:
Math, MyClass, YourOwnClass
2.
Method names: The first word must begin with a lowercase letter all remaining words
must begin with an uppercase letter. Examples: sin, getData, setMyData
3.
Variable names: must be all lowercase with the exception of final variables (constants)
they must be all uppercase (this convention is NOT always adhered to by the Java API)
Examples: radius, mybirthdate, PI
Indentation.
Indentation must be consistent and used to clarify the meaning of the logic (indented in this section means
indented a minimum of 2 spaces)
1.
Class
1.
general info should begin in column 1
2.
variables declarations should be indented
3.
2.
3.
method declarations should be indented and should align with variable
declarations
Methods
1.
method variables declarations should be indented
2.
Java statements should be indented and should align with variable declarations
See statement indentation requirements below.
Java statements
1.
Sequential statement within a block must be aligned consistently. If a statement
is too long to fit on a single line, the continuation of that statement must be
indented.
2.
Block definitions {} must be aligned with the statement above or if the first
statement, must be indented using either the Class or the method rules. The
contents of the block must be indented and aligned.
3.
Selection statements must be indented as follows:
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
if
{
(cond)
true statements;
}
else
{
false statements;
}
switch (variable)
{
case 1:
{
cond1;
break;
}
case other:
{
condother;
break;
}
}
You may also put the opening bracket at
the end of the
28.
initial selection line as may be seen
in many texts
29.
4.
Looping statements must be indented as follows:
for (loop conditions)
{
loop statements..
}
while (cond)
{
loop statements..
}
do
{
loop statements..
} while (cond);
Comments.
The use of comments cannot be stressed enough. Comment your program using the single line
comment // or the multiline comment /* ... */. In addition the following comments must be used.
30. At the beginning of each class the following comments must appear:
/**
* a brief description of the class
* @author students name
*/
(note the /** must be on the first line. This has special
meaning to javadoc)
2.
/**
*
*
*
*
Prior to each method the following must appear:
a brief
@return
@param
@param
description of the method
the value to be returned if any
brief description of the parameter1 if any
brief description of the parameter2 if any
...
*/
(note the /** must be on the first line. This has special
meaning to javadoc)
Samples.
The following is a sample of a Java Application Using Standards...
import java.io.*;
/**
*
which
*
*
*/
public
{
This program will read a sequential file ("javaapp.dat")
was created by WriteFile.java
@author Ron Zucker
class ReadFile
/**
*
*
this function receives data from a sequential file
@return a string containing the data read from the
file
*
to be read
@param
infile BufferedReader with the name of file
*/
public static String getData(BufferedReader infile)
throws IOException
{
String readdata;
try
{
readdata=infile.readLine();
}
catch (EOFException e)
{
System.out.println("end of file" +e.toString());
readdata=String.valueOf("");
}
return readdata;
}
/**
*
This is an application that reads data from a
sequential file
*
and outputs the data to the screen
*/
public static void main(String[] args)
throws IOException
{
String outString;
BufferedReader input;
DataOutputStream output;
/*
this code assigns the file javaapp.dat to a
datastream
which is roughly equivalent to an open...
*/
input= new BufferedReader(new
FileReader("javaapp.dat"));
outString=getData(input);
// this is the loop to continue until an eof is found
while (outString!=null)
{
System.out.println("value read was:"+ outString);
outString=getData(input);
}
//
close the input file...
input.close();
// Say goodnight Gracie...
System.out.println("all done");
}
}
Sample documentation created using "javadoc -author readfile.java" (javadoc is a
program included in the JDK)
Recommendations.
The following represent recommended Java code:
o
For utility classes add a toString() method which will return a String containing:
1.
The name of the class
2.
The value of a key identifier in the class
3.
and optionally concatenate it with the toString() method of the super class
This method is useful for debugging purposes. The following is an example of a
toString() method:
public String toString()
{
return "Class ABC " + abcinstancevar + " of " +
super.toString();
}
Additions, corrections, comments please email to rzucker@unf.edu(click to send
mail)
Last modification date was 05/18/1998 08:29:44
Download