How to Simplify and Improve Code


Remove Redundant

  • Redundant comments that just repeats code.
  • not needed else
Remove implicit
  • remove “public final” in interface.
  • private in private class
  • public in non-public methods

Inline

  • inline the message instead of extract it as constant, if the message is used only once (even we may reuse it in future).
  • inline a standalone class as an anonymous class or inner class
  • merge classes when they are always used together and no reason for each class to exist as a standalone class.
  • inline if a variable/method is used only once

Reduce the nesting/wrapping caused by if or lambda

  • by extracting methods

Simplify lambda

  • Use method reference
  • Reduce complex lambda by extracting methods or changing it to method

Use static import to simplify code

  • Use static import when the method is clear without additional context of the outer type
  • Good static import examples:
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.nio.charset.StandardCharsets.UTF_8;
  • bad static import examples:
import static com.google.common.base.Optional.of;

Import nested class

import com.google.common.base.Joiner.MapJoiner;
  • bad import nested class
import com.google.common.collect.ImmutableSet.Builder;

Merge

  • merge multiple consecutive stream.map to one
  • merge 2/multiple methods that are always used together
    • generateXTableData and seedXTable into one
Merge multiple if
  • merge multiple if
  • merge multiple if including implicit
if (request.hasPageSize()) {
  checkArgument(
      request.getPageSize() > 0,
      String.format(
          "Invalid page size: %d, it has to be positive value.", request.getPageSize()));
}
// to
checkArgument(
    !request.hasPageSize() || request.getPageSize() > 0,
    "Invalid page size: %d, it has to be unset or be positive value.",
    request.getPageSize());

Avoid String.format when the method already supports formatting

  • like Preconditions.checkArgument

Method that takes vargs

  • install(A.class);install(B.class);... to install(A.class, B.class, C.class)

Optional

Misc Tips about Code Simplification

  • Use other util methods or test rule.
  • early return (with the negative condition)

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)