Share On Twitter Facebook Google+ LinkedIn Pinterest Tumblr Reddit
Question

What is the good programming practices in Java?

Tags: java
Date:
Status:Unresolved
Question Id:35

What is the good programming practices you think in Java?

The reson I ask this question:

I always wonder if I am doing in the wrong way so I want to know more about the good practices.

One thing I am always confused with:

Should I always use this.xxxto refer the instance variable in order to distinguish them from local variables?

Answer
Date:
Correct:No

There are several ways to look at 'good coding practices'

  • Coding styles/idioms that is less prone to errors and guard against introducing new errors, thus leading to 'correct' code
  • Coing styles/idioms that are easier for others to read and maintain

In the first case, programmers adopt certain styles to guard themselves against introducing 'accidental' errors that can sneak into their code:

for Example in C, the following code is valid (but has a subtle bug)

if (x = 1)  // always evaluates to 'true' (programmer meant '==', not '=')
{
   // do something
}

so, in order to force the compiler to catch this error, you could adopt a style such as

if (1 == x) // if you accidentally typed '1 = x' the compiler will flag an error
{
   // do something 
}

Such styles/idioms are regarded as 'good' practices and there a quite a few that can be found for Java in books like 'Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin'

Regarding the second case, writing 'this.xxx' enables the reader of the code to differentiate between a local/instance variable, so in the spirit of code readability, it is marginally better than directly referencing the variable as 'xxx'.

Answer
Date:
Correct:No

The popularity of Java programming language can be predicted from the scope of its use. From developing web applications to Android applications, this programming language is highly used to develop applications/code with multiple functionalities of all small to big business enterprises/niche like automatic watch winder technology, hospitality, real estate or any media company like Jason Hartman foundation.

While writing code, the developers work hard to write bug-free code with the least complexity and the most functionality. Whether you are a beginner source code writer or an expert, there are some rules that you should always follow to get the best code for your client. Below are the rules for bug-free code writing.

Rule 1: No Need to Depend on Initialization

In Java, developers always depend on the use of constructors to initialize an object. But this is a big myth that most developers follow. There are numerous ways to allocate an object without calling a constructor.

To do this, you can follow any of the following steps:

  • You can declare all the variables as private. To access the object outside the class, you can then use the GET and SET method.
  • For each object, write a new private boolean variable and initialize it.
  • Write a non-constructor class that will ensure that each object is initialized before calling anywhere in the code.

Rule 2: Secure Your Classes, Method, And Variables

In your code, you will make some classes, methods, and variables as private and some as public. Private classes can’t be accessed easily, making them a secure point of the code. But the public methods, variables can be easily accessed and become a point to be attacked. So, try to make them with limited scope.

Always remember to make the classes, methods, and variables public when you have the only option to do so.

Rule 3: Always Predefine the Scope

Most developers totally depend on the scope of the packages, but you should always predefine the scope of your code. There are many classes that aren’t closed on their own; leaving a loophole for the attackers. A hacker can use a single loophole to insert his/her own class that can use the sensitive information from your code. JVM is not closed by default, allowing you to close your classes within the package.

Rule 4: Avoid Using Inner Classes

Usually, developers use the inner classes whenever they're needed in other classes within the same package. The inner classes are usually accessible to all the other classes in the same package, and it is already mentioned that you should predefine the scope of each and every class you are creating in the code.

Rule 5: Ensure Noncloneable Classes

Java has a feature to clone its own classes whenever required. But this feature can also be used adversely by the hackers. A hacker can easily use the java.lang.Cloneable to make duplicate instances of the code and steal necessary information from your code.

To get rid of this issue, all you have to do is to add the following code in each and every class of your code.

 
 

 

If you want your class to be cloneable, and you've considered the consequences of that choice, then you can still protect yourself. If you're defining a clone method yourself, make it final.

If you're relying on a nonfinal clone method in one of your superclasses, then define this method:

 
 

 

There is no one on the planet that can be considered a 100% expert of Java. But by keeping mindful of the concepts of java programming and your own experience, you can easily code without encountering any bug.

Start Coding!

Your Answer

Review Your Answer