Docs, guidelines and compromising photos
To make changes to these pages, checkout the wego.github.io project and the GitHub Pages docs.
Everyone has an opinion on how to write Java. Here's ours and remember
We highly advise the developer to use Eclipse when developing Java apps. You need to download the latest Eclipse IDE. Please be sure to configure the following in your Eclipse instance:
camelCase
when naming objects, functions, and instances.PascalCase
when naming constructors or classes.NAMES_LIKE_THIS
for constant values.StringUtils.equals
StringUtils.split
. JDK's String
related functions are not null-safe.ThreadFactoryBuilder
Cache
Ordering
Maps
ImmutableMap
ListenableFuture
.DateTime
Duration
Period
DateTimeFormatter
. JDK's Date
SimpleDateFormat
are not thread-safe.Always use logger
and do not use System.out
for logging. Additionally, when doing logging, always enclose your debug messages with a checker to determine whether debug has been enabled. Logging can cause performace issues. With this practice, we can reduce the effects of logging by changing log level.
// bad
logger.debug("This is a " + message)
// good
if (logger.isDebugEnabled()) {
logger.debug("This is a {}", message);
}
if (logger.isTraceEnabled()) {
logger.trace("This is a {}", message);
}
Always use braces after your if
else
for
while
even in the case you need to run one line of code. This is to reduce the errors when you do refactoring code in the future.
// bad
if (StringUtils.equals(result, "bad"))
bad();
else
veryBad();
// good
if (StringUtils.equals(result, "good")) {
good();
} else {
veryGood();
}
Do not create static String
unless you want to share them between functions. In-code strings are already cached in a pool by JVM.
Create thread-safe objects and re-use them instead of creating new object everytime in a function or in a for
loop.
Name your theads and make them daemon when you need to create a thread pool. This makes debugging easier and removes blocking in the main thread when it needs to quit.
Executors.newCachedThreadPool(new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("some-fancy-theads-%s")
.build());
For more guidelines, please follow closely Oracle's Coding Convention.