logo
logo
Sign in

7 Things You Should Never Ever Do In Java

avatar
Allen Bauman
7 Things You Should Never Ever Do In Java

Java is a powerful, robust programming language that can be used to create almost anything. However, it’s not without its quirks and pitfalls. If you’re new to Java or if you just want to make sure your code is as efficient as possible, here are 7 things you should never ever do in Java:

1. Don't invent your own language.

Java was created as a general-purpose programming language that could be used for different kinds of applications. It wasn't designed specifically for any particular application domain (such as games or web applications). One of the reasons why Java became so popular was because it was already supported by many platforms (such as Windows and Linux).

This means that when you use Java, you don't have to worry about whether your code will work on another platform or not — it will just work because all platforms support Java.

2. Don't use raw types.

Raw types are basically classes that have no implementation and have not been created by a compiler or IDE. Here is an example of a raw type:

public class A { public int x; public int y; public A() { x = 10; y = 20; } }

This class is a raw type because it has no implementation and no compile-time verification. The compiler just sees it as a bunch of fields. If you try to compile this code, the compiler will complain about the missing constructor, because there's no way to create an instance of this type.

3. Don't be too chatty.

The Java programming language has many features that allow you to write code in a way that makes it easy for humans to understand what your code does. But there are times when you should not use them or use them sparingly because they can make your code harder for other programmers to understand.

A good example of this is the use of comments in your code: They should only be used when absolutely necessary and never as a way to explain what the code does. Do not comment out large blocks of code because it will make your code difficult for other programmers to read and understand. Try commenting out short snippets instead so that other programmers know what functionality was removed from the program but still see the rest of the program clearly.

4. Don't forget to document your code.

Code without documentation is like a house without windows – you might be able to see inside, but no one else can. The same goes for code: if there’s no documentation, it’s hard for anyone else to understand what you did or why you did it in the first place. 

Lack of documentation is one of the main reasons why tech debt appears and cripples projects.  Documentation allows others to understand how your program works and makes it easier for them to modify or extend your code if they need to do so later on down the road (and they will).

5. Don't be afraid of exceptions.

Exceptions are one of the most powerful features in Java because they allow you to react appropriately when something unexpected happens in your program. In fact, every time an exception is thrown by an application, it means that there is something wrong with your code and you need to take some action accordingly. So don't try to ignore them or suppress them — embrace them!

6. Avoid one-letter variables.

In Java, it's a good idea to use short names for local variables that are only used once. This is especially true when you're writing unit tests because you don't want to have to remember what each variable stands for every time you read the code. But if you use a one-letter variable name, it's easy for another programmer to mistakenly assume that it represents something else later on in the code. For example:

//Bad code:

if (i==0) { //do something, like starting at index 0

} else { //do something else, like starting at index 1

}

7. Don't make garbage creation a habit.

Garbage creation is the most common issue that Java programmers face. Garbage creation refers to allocating objects without using them. This can be done in many ways, but the most common one is creating an object, then immediately throwing it away by assigning null to it.

The following code snippet shows how this can be done:

String name = null; // Allocate an object and assign null to it right away

String name2 = "Foo"; // Allocate another object and assign it a value

name2 = name; // Assign the first object to the second object

Takeaway

If you want to program well in java, avoid these bad habits. Remember: everybody makes mistakes when programming—the important thing is that you have the right attitude and keep trying!


collect
0
avatar
Allen Bauman
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more