Note: It is probably easier to get to know these rules by running the automated style checker, since most of them won't make sense until you are a Java expert. You can do this by submitting to the autograder or by running the style61b.py script on your Java files.
To run the style61b.py script, first make sure you have the latest skeleton by running git pull skeleton master
in your repository. Ensure that the course libraries are on your classpath as set up. If you don't know how to do so, take a look at (this guide from the spring offering of CS61B)[http://cs61b.ug/sp16/materials/lab/lab3b/lab3b.html#a-windows-setup]. Note that a few things will differ: your login is two letters, and our folder is called lib
, not javalib
, and of
course as always, your directory structure will vary. For example on Mac or Linux, if your repo name is ab
, then you want /PATH/TO/YOUR/REPOSITORY/REPLACE/ab/lib
to be on the class path and do so by adding export CLASSPATH='/PATH/TO/YOUR/REPOSITORY/REPLACE/ab/lib:.'
to the end of your ~/.bashrc
.
You will also need to download the checkstyle jar:
https://github.com/Berkeley-CS61B/skeleton-sp16/raw/master/javalib/checkstyle-6.15-all.jar
Place the checkstyle jar in your lib folder. DO NOT add this to your repository (it is ignored by default).
Then navigate to your lib folder (or javalib if that's what it's named), and find the style61b.py
script. You can move this script around or leave it here; either way, you run it by doing the following command:
python style61b.py [list of paths to files to style check]
For example, if you wanted to style check all the java files in the current directory, you would do:
python style61b.py *.java
The below is a set of rules that our style checker attempts to check for, and that you should follow. These are adapted from the Google and Sun style guides (which are industry standards) and made more lenient for your programming ease. Credit to Paul Hilfinger, for writing the original guide.
Whitespace
- Each file must end with a newline sequence.
- Files may not contain horizontal tab characters. Use blanks only for indentation.
- No line may contain trailing blanks.
Do NOT put whitespace:
- Around the
<
and>
within a generic type designation (List<Integer>
, notList <Integer>
, orList< Integer >
). - After the prefix operators
!
,--
,++
, unary-
, or unary+
. - Before the tokens
;
or the suffix operators--
and++
. - After
(
or before)
. - After
.
- Around the
DO put whitespace:
- After
;
,,
, or type casts (e.g.,(String) x
, not(String)x
). - Around binary operators (e.g.,
*
,+
) and comparison operators. - Around assignment operators (e.g.,
=
,+=
). - Around
?
and:
in the ternary conditional operator (x>0 ? x : -x
). - Around the keywords
assert
,catch
,do
,else
,finally
,for
,if
,return
,synchronized
,try
, andwhile
.
- After
In general, break (insert newlines in) lines before an operator, as in
... + 20 * X + Y;
- Do not separate a method name from the
(
in a method call with blanks. However, you may separate them with a newline followed by blanks (for indentation) on long lines.
Indentation
- The basic indentation step is 4 spaces.
Indent code by the basic indentation step for each block level (blocks are generally enclosed in
{
and}
), as inif (x > 0) { r = -x; } else { r = x; }
Indent
case
labels an indent past their enclosingswitch
, as inswitch (op) { case '+': addOpnds(x, y); break; default: ERROR(); }
- Indent continued lines by the basic indentation step.
Braces
- Use
{ }
braces around the statements of allif
,while
,do
, andfor
statements, unless it is a one line statement. Place a
}
brace on the same line as a followingelse
,finally
, orcatch
, as inif (x > 0) { y = -x; } else { y = x; }
- Put the
{
that opens a block at the end of a line. Generally, it goes at the end of theif
,for
,while
,switch
,do
, method header, or class header that contains it. If line length forces it to the next line, do not indent it, and put it alone on the line.
Comments
- Methods should have javadoc comments explaining the behavior, parameters (using
@param
tags or otherwise), and return type. - Methods that return non-void values must describe them in their Javadoc
comment either with a
@return
tag or in a phrase in running text that contains the word "return", "returning", or "returns". - Each Javadoc comment must start with a properly formed sentence, starting with a capital letter and ending with a period.
Names
- Names of
static final
constants must be in all capitals (e.g.RED
,DEFAULT_NAME
). - Names of parameters, local variables, and methods must start with a lower-case letter, or consist of a single, upper-case letter.
- Names of types (classes), including type parameters, must start with a capital letter.
- Names of packages must start with a lower-case letter.
- Names of instance variables and non-final class (
static
) variables must start with either a lower-case letter or_
.
Imports
- Do not import the same class or static member twice.
- Do not import classes or members that you do not use.
Assorted Java Style Conventions
- Write array types with the
[]
after the element-type name, not after the declarator. WriteString[] names
, notString names[]
. - Write any modifiers for methods, classes, or fields in the following order:
public
,protected
, orprivate
.abstract
orstatic
.final
,transient
, orvolatile
.synchronized
.native
.strictfp
.- Do not explicitly modify methods, fields, or classes where the modification is redundant:
- Do not label methods in interfaces or annotations as
public
orabstract
. - Do not label fields in interfaces or annotations as
static
,public
, orfinal
. - Do not label methods in final classes as
final
. - Do not label nested interfaces
static
. Do not use empty blocks ('{ }' with only whitespace or comments inside) for control statements. There is one exception: a catch block may consist solely of comments having the form
/* Ignore EXCEPTIONNAME. */
- Do not try to catch the exceptions
Exception
,RuntimeError
, orError
. - Write
b
rather thanb == true
and!b
rather thanb == false
. Replace
if (condition) { return true; } else { return false; }
with just
return condition;
- Only
static final
fields of classes may bepublic
. Other fields must beprivate
,protected
, or package protected. - Classes that have only
static
methods and fields must not have apublic
(or defaulted) constructor. - Classes that have only
private
constructors must be declaredfinal
.
Avoiding Error-Prone Constructs
- If a class overrides
equals
, it must also overridehashCode
. - Local variables and parameters must not shadow field names, unless it is for a setter or a constructor.
- Do not use nested assignments, such as
if ((x = next()) != null) ...
. Although this can be useful in C, it is almost never necessary in Java. - Include a
default
case in everyswitch
statement. End every arm of a
switch
statement either with abreak
statement or a comment of the form/* fall through */
Do not compare
String
literals with==
. Writeif (x.equals("something"))
and not
if (x == "something")
There are cases where you really want to use
==
, but you are unlikely to encounter them in this class.
Limits
- No file may be longer than 2000 lines.
- No line may be longer than 100 characters.
- No method may be longer than 100 lines.
- No method may have more than 8 parameters.
- Every file must contain exactly one outer class (nested classes are OK).