-
Notifications
You must be signed in to change notification settings - Fork 4
Java Style Guide
One of the goals of CS2030 is to move you away from the mindset that you are writing code that you will discard after it is done (e.g., in CS1010 labs) and you are writing code that no one else will read except you. CS2030 prepares you to work in a software engineering teams in many ways, and one of the ways is to enforce a consistent coding style.
If everyone on the team follows the same style, the intend of the programmer can become clear (e.g., is this a class or a field?), the code is more readable and less bug prone (e.g., the Apple goto fail
bug). Empirical studies support this:
QUOTE
"It is not merely a matter of aesthetics that programs should be written in a particular style. Rather there is a psychological basis for
writing programs in a conventional manner: programmers have strong expectations that other programmers will follow these discourse rules.
If the rules are violated, then the utility afforded by the expectations that programmers have built up over time is effectively nullified.
The results from the experiments with novice and advanced student programmers and with professional programmers described in this paper
provide clear support for these claims."
Elliot Soloway and Kate Ehrlich. "Empirical studies of programming knowledge." IEEE Transactions on Software Engineering 5 (1984): 595-609.
Many major companies enforce coding styles, and some have published them. For CS2030, we base our (simplified) coding style on Google's Java Coding Style. You should bookmark the link because you need to come back to it again and again.
-
No tab Use only whitespace.
For
vim
users, you can add the following line in your~/.vimrc
file:set expandtab
So that when you press it is expanded to whitespace.
Most other source code editors have similar configuration.
-
Exactly one blank line after import statements and exactly one top-level (i.e., non-nested) class.
-
Each top-level class resides in a source file of its own.
-
When a class has overladed methods (e.g., multiple constructors or methods of the same name), they appear sequentially with no other code in between.
-
Braces are always used (even if the body is empty or contains a single statement}
-
Use "Egyptian brackets":
- Opening brace have no line break before; but has line break after
- Closing brace has a line break before; and has a line break after (except when there is
else
or comma following a closing brace)
Example:
if (x == 0) { x++; }
is good.
if (x == 0) { x++; } if (x == 0) { x++; } if (x == 0) { x++; }
are not good.
-
Block indentation is exactly four spaces.
if (x == 0) { x++; for (i = 0; i < x; i++) { x += i; } }
For
vim
users, in~/.vimrc
, add the following:set tabstop=4 set shiftwidth=4 set autoindent set smartindent
To help you with indentation.
Most other source code editors have similar configuration.
-
Each statement is followed by a line break, no matter how short the statement is.
x++; i++;
is bad.
x++; i++;
is good.
-
Each line is limited to 80 characters in length. You can break a long line into multiple lines to enhance readability, this is called line wrapping. When you do so, each continuation line is indented at 8 spaces (twice the normal indentation) from the original line.
System.out.println("Daenerys of the House Targaryen, the First of Her Name, The Unburnt, Queen of the Andals, the Rhoynar and the First Men, Queen of Meereen, Khaleesi of the Great Grass Sea, Protector of the Realm, Lady Regnant of the Seven Kingdoms, Breaker of Chains and Mother of Dragon");
is bad.
System.out.println("Daenerys of the House Targaryen, the First of" + " Her Name, The Unburnt, Queen of the Andals, the Rhoynar and the" + " First Men, Queen of Meereen, Khaleesi of the Great Grass Sea, P" + "rotector of the Realm, Lady Regnant of the Seven Kingdoms, Break" + "er of Chains and Mother of Dragon");
is also bad.
System.out.println("Daenerys of the House Targaryen," + "the First of Her Name, The Unburnt, Queen of the Andals," + "the Rhoynar and the First Men, Queen of Meereen," + "Khaleesi of the Great Grass Sea, Protector of the Realm," + "Lady Regnant of the Seven Kingdoms, Breaker of Chains and" + "Mother of Dragon");
is ok.
note
"80 vs 100"
While we prefer lines to be limited to 80, we are OK if the length is up to 100. Any longer, however, will be frowned upon.
-
There should be a blank line between constructors, methods, nested classes and static initializers. Blank lines can be used between fields to create logical groupings.
-
White space should separate Java keywords from parenthesis and braces, and be added on both sides of binary operators (
+
,-
,/
, etc) as well as:
in enhanced for. A space should also appear before and after//
commentsif(x==0){ x++;//to make sure x is at least one. for(i=0;i<x;i++){ x+=i; } }
is bad.
if (x == 0) { x++; // to make sure x is at least one. for (i = 0; i < x; i++) { x += i; } }
is good.
-
One variable per declaration.
int x, y;
bad.
int x; int y;
good!
-
No C-style array declaration
String args[];
not good.
String[] args;
good!
-
Switch statement always include a
default
case. -
One annotation per line. Always use
@Override
.@Override public boolean equals(Object o) { // details omitted. }
-
Indent comments at the same level as the surrounding code. For multiple comments, align
*
with the previous line./* * Not a good style */ /* * Good style */
-
Class modifier appears in the following order:
public protected private abstract default static final transient volatile synchronized native strictfp
Example:
static public void main(String[] args)
is bad.
public static void main(String[] args)
is good!
-
Class names are written in UpperCamelCase, method names and field names in lowerCamelCase, constant names in ALL_CAPS_SNAKE_CASE. Type parameters in single capital letter.
-
Caught exceptions should not be ignored.
-
Static fields and methods must be accessed with class name.
Peer Learning
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Stu
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
SSH Without Password
Copying Files From PE Nodes
Using tmux
CS2030 Contents
Lecture 1 SummaryLecture 2 Summary
Access Modifiers
Lecture 3 Summary (Polymorphism)
Compile Time Type VS Runtime Type
Abstraction, Encapsulation, Inheritance, and Polymorphism
SOLID Principles
Class VS Abstract Class VS Interface
Comparable VS Comparator
Generic Types T
HashMap
Raw Types with Generic
Lambda expression
PECS (Producer Extends Consumer Super)
Optional
Streams
Parallel Streams
Monad
Functors and Monads with Category Theory