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
- Reduce code;
- Avoid clever code - keep it simple;
- Make it small and cohesive - single responsibility;
- Eliminate duplication - DRY;
- Eliminate code dependencies;
- Write self documenting code - make comments unncessary;
- Code should be understandable in seconds;
- Avoid primitive obsession - focus on creating higher level abstractions;
- Checkin frequently, take small steps - every commit should be only one change;
- Keep code at one level abstraction - each method should do one thing.
When not to Refactor?
- Never refactor while making any other code change.
- When you spot a code smell - take a note of it
- Finish your change
- Commit
- Refactor
- Never refactor alone. Unit/Integration Tests and Team Code Review are your best friends.
When To refactor?
- You can refactor before or after a bug fix or a functionality change;
- If you think the change will improve the design of the code;
- 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