Cleaning Up Code 101: Learning from Uncle Bob’s ‘Clean Code’ Refactoring Examples (3)
This time is a bigger sample.
Beginning
In previous posts, we went through 2 sample codes and found clean code has the below attributes.
- Make it focus. move the variables closer to their usage.
- Early return.
- No nested if.
- Check duplicates. extract method.
- Minimalism. Only check once, and do it once.
- Try not to use comments to explain the code (but make the code self-explain)
- Break down statements into smaller functions. for clarity purposes. make the code intention as explicit as possible.
- The caller should be above Callee.
- Once done. scroll the class from the top down. you should feel like reading an article.
- Spend time on names.
- Simplicity does not always mean the number of lines. sometimes a smaller function could be turned into a class for better readability.
- “half refactored” code is a waste of time.
So you may ask, “What about abstraction”? and “dependency injection”?
Here it is. and let me show you how I did it wrongly (so you don’t have to)
The original code
public class Args {
private String schema…