Monthly Archives: January 2018

Java Coding Style

Guava: Preconditions.checkNotNull
Guava: Preconditions.checkArgument
JDK: Objects.requireNotNon
NullPointerException or IllegalArgumentException
1. Many APIs in the JDK pro-actively throws NullPointerException. This suggests that throwing NPE is a Java convention.
2. Effective Java Item 60: “Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds of illegal arguments and states. If a caller passes null in some parameter for which null values are prohibited, convention dictates that NullPointerException be thrown rather than IllegalArgumentException. Similarly, if a caller passes an out-of-range value in a parameter representing an index into a sequence, IndexOutOfBoundsException should be thrown rather than IllegalArgumentException.”
3. Preconditions.checkNotNull(parameter) is clearer than Preconditions.checkArgument(parameter != null, “parameter can’t be null”)

static import takes first place. Because there was a Bug prior to JDK8.

Prefer dvdPlayer to DVDPlayer. Because you can’t tell HTTPSID(httpSid or httpsId)

Prefer 1TBS to K&R. Because moving codes in K&R may lead messy. 1TBS always has bracket to be clear.
int i;
for (i = 0; i < 10; i++) {

int i;
for (i = 0; i < 10; i++)

stylistic convention

2PC, 3PC

There are two basic distributed transaction protocols: two-phases-commit and three-phases-commit.


Assumption, coordinator and cohorts are never down.

If coordinator is down, all other cohorts will be blocking and waiting for its recover. If a cohort is down and when it wakes up, it will get notified by coordinator(commit or rollback).


For 3PC, coordinators asks for canCommit to all nodes. Only if all nodes answer YES, coordinate enters prepared phase and send preCommit to all nodes.

When majority nodes ACKed preCommit, coordinator sends commit to all nodes. It enters commit state.

In above phases, if coordinator/cohorts failed, timeout, then it sends abort message.

also compare with zookeeper protocol