How to write clean code in Java language? In 5 simple steps
By: Sajjad Ahmed
Tags: Code, JavaScript, Java, Programming, Coding

Photo by Christopher Gower on Unsplash

This article will review some tips for writing clean, easy-to-understand code in the Java language. It is recommended to follow them when writing any programming code, not only when working in teams, as you know, you may be in the future who cannot read and understand the code that you wrote a few years or even months ago.

Type short, expressive code

Programming problems can be solved in hundreds of ways, but the best way is the shortest and fastest at the time of execution, so when you solve a specific software problem, think about how to solve it in the shortest number of steps and the least impact on the device of course.

Everyone can solve problems, but the smart is the one who solves them in the easiest and fastest way possible, even if it takes more time to think, and seeing the ideas of others will help you to look from different angles of the problem so that you can develop your own idea.

And always remember to leave the keyboard as soon as you encounter a problem, and do not press any letter before you think about the problem from all sides and how to solve it.

When choosing a specific method to solve a software problem, you should think about whether or not the solution method is expressive. Sometimes we choose a clear, slightly long way to solve a software problem instead of a complex and short one.

It is important for the code reader to be able to understand how the code works as well, and try to solve the problem step by step so that the reader understands the solution method and do not proceed with the last step in the first line, for example.

And always remember the saying “The best of words is what is less and than that.” The code must also combine the two adjectives, and one is not sufficient.

Pick understandable names for the things you identify

In any programming language you use, you will find that you inevitably define a lot of things such as variables, classes, and functions, which must be given their own names and these names are called identifiers.

It is true that you can name the variables using the combination of letters and numbers that you want, but there are specific rules for naming everything that is preferred or even that you must always follow, otherwise your identifiers will be more like talismans than as names.

And naming methods that do not follow these rules may cause a misunderstanding of whoever is reading your code, for example if you name the class getUsername as we usually do with functions, then those who read the code think that this is a function name and not a class name, and so on.

In the Java language the naming rules are as follows:

Never use the $ symbol in the designation of any identifier.

Do not use numbers except in cases of extreme necessity when you give an important meaning to the name. For example, do not define the variable name and name1 only because you want to use the same name, but for another variable.

Write the identifier to express the meaning and purpose of the identifier. Never call it generic names such as data or SomeFunctions and focus on this point well.

Variables are defined by a name and not by a verb, for example, use name instead of getName as a name for a variable, and the name of the variable must start with a lowercase letter, and if the name consists of more than one word, separate the words by writing the first letter of the next word in its large form such as employeeSalary and no You should never use the _ symbol to separate words as we do in Python.

The variables of the final type are all written in uppercase letters separated by the symbol _ like ROOT_PATH.

Functions are defined by a verb and not by a noun, and the first letter of its name must begin with a lowercase letter, and separate words by writing the first letter of the next word in an upper case, such as getName.

Classes, Interfaces, and Enumerations are placed as complete nouns (not verbs) without abbreviations whose name begins with a capital letter, and words are separated by capital letters as well, such as AdminAccount.

Do not write any name in the form of abbreviations for a variable, function, or row such as DB, and write the full name instead.

The enumeration values ​​are called the same way as the final variables, they are also static values.

Packages are written in lowercase names, not in the plural, such as control and not controls.

Do not repeat the code more than once

Copying a certain code and pasting it around the program makes reading the code much more difficult, and you may need at some time to change the code and then you will have to search for it in all the program and change it, which wastes your time and effort.

And believe me, there will be many disasters due to hidden errors that you will not notice in the way the duplicate code works.

Try creating a (Method) function to do this repetitive work and call it when needed. Putting the code in a function that protects it from errors and isolates it as one unit from the rest of the lines of code.

Post comments when needed

Comments help us explain the code to the reader, and it is like salt, so do not reduce or increase it, and always remember to make sure in the event that the code is not clear by writing comments explaining that code.

And if you want to explain how a class, function, or the property of the property works, you can use javadoc to write that, and javadoc has a feature that can be displayed to the programmer in development environments such as Netbeans, where you find that when you write the name of the function, an explanation of it appears, and this is not done In the case of writing your notes in the form of a comment.

Code that is not cleaned will eventually get dirty

Always remember that modifying and updating the code will complicate the program, so always take a look at the code and try to clean and simplify it as possible.

It should be noted here that IDEs such as Netbeans provide simple tools to facilitate this process for you, for example you can change the name of a variable or function and it will modify it for you in all files.