Refactoring with aid of apache commons & guava libraries

Purposes

Refactoring paradigm:

By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you'll find that it is easier to extend and maintain code.

—Joshua Kerievsky, Refactoring to Patterns

 

 

Utility tools paradigm:

Don't reinvent when it ready to use

Agenda

  • Refactoring + Utility substitution
  • Apache Commons arsenal 
  • Guava arsenal
  • Live refactoring session

refactoring

Refactoring principles

  1. Reduce code;
  2. Avoid clever code - keep it simple;
  3. Make it small and cohesive - single responsibility;
  4. Eliminate duplication - DRY;
  5. Eliminate code dependencies;
  6. Write self documenting code - make comments unncessary;
  7. Code should be understandable in seconds;
  8. Avoid primitive obsession - focus on creating higher level abstractions;
  9. Checkin frequently, take small steps - every commit should be only one change;
  10. Keep code at one level abstraction - each method should do one thing. 

When not to Refactor?

  • Never refactor while making any other code change.
  1. When you spot a code smell - take a note of it
  2. Finish your change
  3. Commit
  4. Refactor


  • Never refactor alone. Unit/Integration Tests and Team Code Review are your best friends. 

When To refactor?

  1. You can refactor before or after a bug fix or a functionality change;
  2. If you think the change will improve the design of the code;
  3. If you think the change will improve the readability of the code for other developers.

Usefull resources

Apache Commons

The Commons is an Apache project focused on all aspects of reusable Java components

Apache common utilities

Lang

  • StringUtils
  • ToStringBuilder, HashCodeBuilder
  • CollectionUtils, ArrayUtils

IO

  • IOUtils
  • FileUtils
  • FileSystemUtils
  • FileFilter
  • LineIterator

BeanUtils, ConcurrencyUtils

Google Guava

com.google.common.annotation

com.google.common.base

com.google.common.collect

com.google.common.io

com.google.common.base

com.google.common.net

com.google.common.primitivies

com.google.common.util.concurrent

When to use guava?

  • Temporary collections
  • Im-Mutable collections
  • String Utils
  • NullObject pattern
  • Functional programming style
  • IOUtils

Conclusions

  • Do Refactoring
  • I could just write it by myself. But...use utility library
  • Effective Java 2e, Item 47

refactoring & utilities

By Yegor Bondar

refactoring & utilities

refactoring & utilities - Apache Commons & Guava

  • 1,228