My 4 tips on how to write clean and more efficient code in Java

Nikola Obradovic
4 min readNov 20, 2020

--

Photo by The Creative Exchange on Unsplash

Even though my title mentions Java these tips can be applied to any programming language or technology, however the examples I provided will be written in Java 11. Writing highly maintainable and easy to understand code is essential for any good software engineer! Below I will present you my 4 tips that will guide you to less error prone and much cleaner code base.

Properly name your variables

At first sight this tip might sound a bit trivial to you, but it’s actually one of the most important steps on writing highly readable code and that’s why I think it deserves the first place on this list.

Write your method and variable names so that you (or anybody else) can understand what is their responsibility just by looking at their names.

To display the importance of proper variable naming we’ll analyze one trivial method.

    public static void calculate() {
int[] arr = new int[6];
for(int i = 0; i < 1000; i++) {
var n = new Random().nextInt(6);
arr[n]++;
}
var index = 1;
for(int t : arr) {
System.out.println(index++ + "\t" + t);
}
}

By looking at this method you can see that there is an array, two for loops and some result at the standard output, but still it’s a bit difficult to tell what does this method exactly do at the first look.

Let’s take a look at the same method with proper variable names.

    public static void printDiceRollResults() {
int[] appearanceTimePerDiceSide = new int[6];
int rollCount = 1000;
for(int i = 0; i < rollCount; i++) {
var diceRollResult = new Random().nextInt(6);
appearanceTimePerDiceSide[diceRollResult]++;
}
var diceSideToDisplay = 1;
for(int diceSideAppearanceCount : appearanceTimePerDiceSide) {
System.out.println(diceSideToDisplay++ + "\t" + diceSideAppearanceCount);
}
}

If we look at the second example, it should take us significantly less time to understand what this method does and nothing was changed except for method and variable names!

Please keep in mind that this is only trivial example i just came up with. Now image if you were working with much more complex method and you have to debug it ASAP. Setting proper variable and method names take a little bit of your effort but it will help you and your colleagues a lot in the future.

Test-driven development (TDD)

Test-driven development basically means that you write tests first (based on some requirements) and then you write the actual implementation of the desired functionality so all of the previously written tests pass.

Solving problems on the TDD way provides you lots of benefits, the whole list would take the whole page so I’ll just list some of them: higher productivity, significantly easier to detect errors, your implementation will be cleaner as you will focus on requirements of the test, you will get instant execution result so it will be much easier to implement functionalities, the code will be easier to debug, …

Write S.O.L.I.D. code (principles)

The SOLID principles are probably the most known principles in object-oriented software engineering. Those principles are:

S. Single-responsiblity principle
O. Open-closed principle
L. Liskov substitution principle
I. Interface segregation principle
D. Dependency Inversion Principle

By following these principles your code is guaranteed to be highly maintainable and well structured (easy to understand). Besides S.O.L.I.D. other important principles are:

DRY — Don’t repeat yourself
KISS — Keep it simple, stupid
YAGNI — You ain’t gonna need it

Most of these principles are self-explanatory. Since they are not in the main focus of this post I won’t be going through them in detail now, perhaps that can be subject for some of my future posts.

Don’t be afraid of Pair programming

I believe that this is the best way to actually improve and really write higher quality code. Your experience level doesn’t matter, even if you’re senior with 10 years of experience you cannot see things from the same perspective as somebody else. I would recommend you to pair program at least once in a week or two. Remember two heads are better than one!

Applying this tip directly might be a bit hard in these difficult times. If you’re stuck and there is no way for your to pair program with someone I suggest you to at least ask your friend or colleague to review your code.

Feel free to reach out

I honestly hope you enjoyed reading my first article! If you have any questions or suggestions feel free to leave a comment below, I’ll make sure i get back to you.

--

--

Nikola Obradovic
Nikola Obradovic

Written by Nikola Obradovic

I’m a software engineer, currently I’m coding in Java for most of the time. My goal on Medium is to acquire new knowledge and share it with everybody here.

No responses yet