Ultimate Guide to Clean Code with over 30 Java Refactoring Examples
Clean Coding in Java with examples
Clean Code is a key to a better, performant, efficient software system. For almost all software products, except very tiny ones, there will be lots of code and some of them will be indeed bad code. Over time developer teams will change, technologies will change, viewpoints to certain patterns will change and at the end of the day, some code will be bad code. So there is no way to guarantee that source code of a software product will be of only clean code. But there are some golden rules that experienced developer should always have in mind when coding. In this blog I am going to go through some important smells and heuristics of refactoring with java examples.
Naming
- Use intention revealing names: Name of a class, function or variable should tell you what it is all about
- Avoid misinformation: Naming should not lead to false information. A variable should not be called incomeList, unless it is a List in terms of Computer Science
- No need for prefixes: e.g. You don’t need to name input parameters with prefix p*
- Avoid mental mapping: Don’t rely on that, users will associate the naming same as you. e.g. variable r is not always url for everybody