12 code writing rules for Junior Java Developers

12 code writing rules for Junior Java Developers

Many juniors write their first Java project and then have that project reviewed by other developers. Sometimes such projects collect a large amount of criticism. And that's another thing no programming bootcamp can teach you. There are 12 main rules in Java to make your code clearer and more understandable.

Rule 1: The code should not go beyond the screen

This rule is obvious to anyone who has written even a single piece of code in the Linux environment. However, it is unknown to many young programmers because they have never dealt with Linux standards. Nowadays we are dealing with great quality monitors with high resolutions and it is very easy to write functions that make the given code unreadable and very difficult to understand, not only for someone new to the project.

Rule 2: Maximum three curly braces

In IT it is common to say that anything above three curly braces is artificial intelligence. This principle also comes from the Linux environment, and from the words of Linus Torvalds himself that if code is indented more than three, it is code that needs to be rewritten. Think for yourself if it makes more sense to nest if… else and other control statements and conditions?

Rule 3: Short classes and functions

Professional programming uses an average of 200 lines per class and 20 lines per method. Anything above this value obscures the code and causes your code to break many programming rules. In addition, complex classes and methods are much more difficult to test than simpler elements.

Rule 4: Validating the input parameters of the function

This is another disturbing feature in the code. Programmers often look for shortcuts and throw a NullPointerException every now and then. Instead, you can validate the input parameters. Also note that new Java implementations have a parameter

  • XX: + ShowCodeDetailsInExceptionMessages

which shows what variable throws a NullPointerException. At the same time, remember that this parameter should only be used in a development environment as it affects application performance and security.

Rule number 5: Consciously manage your resources

Most Java programmers find that Java automatically deletes all kinds of garbage from memory. However, this is a huge trap and leads to huge memory leaks, mainly related to sockets, files, and database connections. A good programmer always knows what resources the JVM manages and what resources should be managed on their own.

Rule number 6: Algorithmic complexity matters

The complexity of the algorithms was in college, right? What do you remember about it? Or maybe you have already forgotten that such a thing exists? I, too, fall into the trap that sometimes I rely too much on Java capabilities and pre-built JDK components, which supposedly provide the most optimal algorithms to handle, for example, sorting. And this leads to a huge problem. Very often, enormous amounts of man-hours are spent on solving performance problems, and often this is not needed. Performance bugs can be easily fixed while writing the code, just understand the complexity of each JDK function and how those functions affect your code.

Rule number 7: Use Test Driven Development

TDD has almost become a revealed truth when it comes to software development, but new programmers are unable to apply this principle. We often have tight deadlines which, according to business, limit the time we spend writing tests. If anyone on your team tells you about testing this way, it's high time to change the team. Projects that do not use TDD require a lot of quality engineers to find problems in the code. Another well-known excuse is that we are currently improving old code that has no written unit tests. Since there are no tests, maybe it's time to write them?

Rule number 8: Take care of your memory

My generation comes from a time when memory was a scarce resource, and 64 kb of RAM was all that home computers had. Now people are complaining even if they have 64 GB of RAM. Not only does the Windows operating system eat up huge amounts of memory, the same execution of HelloWorld in Java can be a pain. Although a solution called Modular JDK Project has already appeared, the adaptation of the old code to new solutions is extremely labor-intensive. Therefore, you should minimize the memory consumption of the application, even if we don't have to.

Rule number 9: Maintain memory allocation

It is imperative to remember how objects are created and how many objects are actually created. This plays a leading role in application performance issues. Very often there are situations when someone clones classes or objects, which is completely unnecessary. Of course, there are some reasons to pass some things by reference, but if you use class and object cloning unnecessarily, you are wasting a huge amount of memory. This is a very hard problem to spot if you have a lot of Young Generation Java resources and Garbage Collector cleans up all short-lived objects properly. Remember that allocating objects is an expensive operation.

Rule number 10: Take care of floating point numbers

You absolutely need to pay attention to the data types you use for floating point calculations. This element is a huge problem all the time when it comes to calculating numbers. I know it's easiest to use float and double to represent currencies, but we don't really need that many decimals. Losing the precision of calculations is still a huge problem, but Java partially solves it by providing a type called BigDecimal.

Rule number 11: Design modularly

If the application is not written in microservice architecture, then you must consider how to divide it into smaller elements and introduce a modular structure. After all, all this is offered by maven and is available in Java. After all, you don't really use the same libraries everywhere. For example, it is not uncommon for the Soap module to import thousands of redundant classes. Sometimes it is worth reading about microservice architecture and dividing the application into smaller services in order to communicate with them via API.

Rule 12: Understand threads and multithreading

Over 90% of juniors under my wing do not understand how multithreading works. They use strange language constructs, some of which are already obsolete. For example, they don't know that there is a difference between a synchronized block and the lock itself. Additionally, most of them have not heard of the java.util.concurent. * Package. To efficiently develop in Java, you need to know this package as it provides many amazing solutions for multi-threaded applications and asynchronous backends.

Summary - that is, fear to be afraid

Java is one of the easiest languages to learn. It is a fact. But writing your app correctly in this language means there's a tremendous amount of knowledge to be learned. The problem sometimes is with the design of the memory model in the virtual machine, and sometimes with what the developer is doing. I hope you find these guidelines useful and that they make your Java programs better.

Share this Post