How To Write Clean Java Code PDF
How To Write Clean Java Code PDF
Javarevisited T HINGS T O LEARN GET P UBLIS HED JAVA P ROGRAMMING RES OURCE ARCHIVE BLOG
You have 2 free stories left this month. Sign up and get an extra one for free.
HardFreedomCom Follow
May 17 · 4 min read
During my first internship, I was asked whether or not I had read Robert
C. Martin’s Clean Code. I had no idea what this book was about, but it
later became my “must-read” for every developer.
1. Structure
Before even starting to write any code, think about your program:
classes, functions, structures, and so forth. Use packages and do not add
too many classes to one package unless the classes fit within the same
category. The more you plan, the easier it will be to keep writing your
code — otherwise, you will have to restructure and refactor your code in
the future.
2. Naming
You may say it’s obvious and everybody knows that good naming of
classes, methods, and variables is very important. Yes, maybe…but don’t
forget about it when you write code. You can always change
RspValidation.java to ResponseValidation.java or print() to
printResponse(). Names of variables should have their intents. I hope that
I never see String asd in my programs.
Finding correct names and words for your code helps to avoid additional
comments regarding explanations about the code. Having said that, I
suggest writing explanatory comments for tricky methods or places with
non-standard implementations. Such comments might explain business
case or something else that might be important for the developers who
will maintain it.
Don’t forget to use camel cases for Java code. Class names should start
with a capital letter (please).
Your method has to solve only one problem. Avoid writing big
methods, because other developers might find them difficult to read.
Decompose your method into a few perfectly named methods — it will be
easier to test them, too.
4. Method parameters
Avoid using many parameters in method as it might be difficult to work
with a method that has many parameters.
For example:
private addUserInformation(String name, String street, String
5. Duplication
If two of your methods have repetitive code — extract it to the third
method or a separate class, and use it for all the repetitions. Some
developers say that it is easier to see everything in one method, but I
think — less code, no repetitions, easier to maintain, and avoid bugs. The
best IDEs have the ability to extract, in two clicks, the repetitive code into
a separate method.
6. Hardcoding
Avoid it. I know… it is so easy to hardcode something when you are in a
rush. But heavily hardcoded code is so difficult to maintain and leads to
bugs and mistakes. As much as possible, use constant classes,
.yml/.properties, generics, or whatever your team approves.
7. Logs
I once had to maintain the API, which was very well logged. Nobody from
the company was acquainted with this code because it was written by a
developer who left the company. I was also a stressed new employee.
I have provided the most important rules to keep your code clean. It looks
easy, but even senior developers have to review their code to ensure it is
understandable and readable. Just like any pedant, you can clean it to
perfection — but don’t forget to evaluate project terms and priorities!
182 claps
WRIT T EN BY
HardFreedomCom Follow
Javarevisited Follow
How to make your Mac Quirky T hings T hat An awesome guide on Setting up CI/CD using
more e ectively for Programmers Say how to build REST ful Docker, AWS ECR and
developers? T imothy Mugayi in Better APIs with ASP.NET Core Github Actions (Part-1)
Saneryee Studio in T he Programming Evandro Gomes in Ashutosh Karna in FAUN
Startup freeCodeCamp.org
Build Your Own Blog Part 1: Build A News- Building a simple Build a Multi-Selection
with Django —Part 1 Gathering Desktop App application using List Using RecyclerView
Riley Predum in T he Startup for MacOS Nuxt.js/Vue.js and Mustufa Ansari in Better
Kevin Anderson in T he Startup Django —Part I Programming
Eduardo da Silva