Note that the examples discussed in the lectures, labs, and readings may not comply with these guidelines as they were prepared by different people and at different times.
.jar
file (which is just
a collection of .class
files) in your working
directory (or CLASSPATH).
You must also have the following "checks file" for the BMS Style Guide in your working directory.
From a command shell you can then run Checkstyle on all of
the .java
files in your working directory as follows:
java -jar checkstyle-6.2-all.jar -c BMS_checkstyle.xml *.java
You can also have Eclipse automatically format your code. To do so, download the following formatter:
and then, in Eclipse, click on Window+Preferences+Java+Code Style+Formatter+Import.
.jar
file and
the checks file (see above), you need to
configure it so that the "Checkstyle home directory" points to
the directory/folder containing the Checkstyle .jar
file (by clicking on Browse) and the
"Checks file" points to the checks file you downloaded
(again, by clicking on Browse).
The name of the file and the name of the class must coincide
exactly. For example the Queue
class should be
in a file named Queue.java
.
Note that Java is case-sensitive (even though some versions of
MS-Windows are not).
This is so that automated testing tools have a place to insert comments/remarks if necessary.
The only exception to this rule is that all constructors must
be listed first. For example, in a Queue
class, the
constructor(s) should be first, the pop
method should
be second, and the push
method should be third.
So, class/instance variables should be declared at the top of the class (before any methods are declared) and variables that are local to a method should be declared at the top of that method (before any other lines of code). The only exceptions to this rule are:
In the constructor of a derived class the call to the constructor of the parent class must come first.
Index variables in loops may be declared locally.
For example, variables of type double
should be declared
before variables of type int
which should, in turn,
be declared before variables of type Node
.
In addition, each "word" within a class name should start
with an uppercase letter.
For example, TextMessage
and
SimpleTrafficMonitor
are both appropriate class names.
Further, "words" within a "constant" name must be delimited by
an underscore character.
For example, EXTREMELY_UNHEALTHY
is an
appropriate name for a "constant".
Further, each "word" within a variable name should start
with an uppercase letter.
For example, importantMessage
and
campusMonitor
are both appropriate variable names.
In general, even single-character variable names should be lowercase. However, in some situations, mathematical notation uses uppercase letters. In such situations, uppercase variable names may be used. For example, matrices are often written using uppercase letters. So, an expression like (b = A*x) would be appropriate.
Variable names like aaa
are not appropriate.
Index variables and counters can, however, have names like
i
and j
.
For example, you must not have statements like
int i = 0;
. (Though this is inconvenient in
some situations, it prevents a common mistake made by beginning
programmers.)
Do not rely on default visibilities.
Indeed, they must have private or protected visibility unless there is a very good reason for them to have public or package visibility.
This comment should describe the complete class (rather than the methods in the class).
This comment should describe the methods, its parameters, and its return value.
javadoc
is a program (written in Java) that creates external documentation
(in HTML) from block comments. All of the Java documentation was,
in fact, created this way. javadoc block comments start with
/**
and end with */
.
//
rather than /* ... */
.
This isn't a requirement but it will make your life easier since you can't nest block comments. There is nothing more annoying then trying to "comment out" a section of code while you are debugging and being unable to do so because it contains block comments.
* * This work complies with the JMU Honor Code. *
@author
element.
.
operator,
operators must be separated from their operands by one space.
switch
statements.
// The correct style for a switch statement // switch (seatLocationCode) { case 100: vip = true; price = 40.0; break; case 200: price = 30.0; break; case 300: price = 15.0; break; default: price = 0.0; break; }
Copyright 2019