Ultimate Guide to Clean Code with over 30 Java Refactoring Examples

Clean Coding in Java with examples

Ali Zeynalli
4 min readJun 24, 2021
Photo by Philipp Berndt on Unsplash

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

  1. Use intention revealing names: Name of a class, function or variable should tell you what it is all about
  2. 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
  3. No need for prefixes: e.g. You don’t need to name input parameters with prefix p*
  4. 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

--

--

Ali Zeynalli

IT-Architect | BMW Group | M.Sc. Technical University of Berlin