15May
10 Practices You Should Avoid to Become a Good Java Developer
10 Practices You Should Avoid to Become a Good Java Developer

Introduction

Java is an object-oriented, case sensitive and class based programming language. It strictly follows the concept of OOPs(Object oriented programming language). The byte code provided by the JVM enables a programmer to type the code once and run on any machine later which makes it a platform independent language.

Java was developed by James Gosling at Sun Microsystems and it was named initially as OAK.  Java is the name of an island in indonesia. This programming language is beginner friendly and is being loved and adopted by most of the programmers.

Features of Java

Following are the features of Java:

  • Simple
  • Robust
  • Object Oriented
  • Secure
  • Architecture independent
  • Portable
  • Platform independent

Now, let us explore the habits to avoid becoming a good java developer.

Use the best naming conventions

Naming a variable or a function is as important as keeping a little baby’s name. While writing a program, make sure that the variable declared is pronounceable and human understandable.

Also make sure that it follows all the guidelines for naming a variable such as:

  • If the variable name starts with a letter, it must be preceded by an underscore (_).
  • In a variable name, a digit cannot be used as a prefix.
  • A variable must consist of alphabets(a-z,A-Z) or digits(0-9).
  • It is essential to note that variable names are case-sensitive (Age, AGE, and AGE respectively are three different variables).
  • Variable names can be as long as you like.
  • Spaces are not allowed in variable names.
  • No Go keywords can be used in the variable name.

Visibility of class members and member functions

Whenever you write the class member variables make sure to write the most restricted variable on the top and the least restricted variable on the bottom. It also means that hierarchy of the access specifiers must be – protected→ private → public. Also make sure that the same data type variables are clubbed together as like all the integer variables must be together and float must be together. Also all the members must be stored in the alphabetical order. The concept of storing the private member first and public at last help us memorise the one-category variables very easily.

One such example of a perfect code is:

public class StudentManager {
    private String Column;
    private int Message;
    private int Rows;
 
 
    float columnWidth;
    float rowHeight;
 
    protected String[] columnNames;
    protected List<Student> listStudents;
 
    public int numberOfStudents;
    public String title;
 
}

Libraries are important

At times, when a programmer writes a code and runs it. A lot of errors are encountered due to not including the important libraries.

For example, you are running a string function called substring, but at the run time it throws an error. All your logic in the code is perfect but you are not able to figure out why it is giving errors. This might eat up a lot of your time as the import java.lang package was missing.

So, make sure before implementing an in-built function you have added its package.

Break in switch

When we don’t apply a break in switch cases it automatically executes all the cases written down there. This may result in an unexpected output at times. Sometimes, the small break statement remains unexplored until it goes into the production and if it goes into production it can be embarrassing and disastrous.

Please do not avoid using break in switch statements. In some of the cases, use static code analyzers like FindBugs and PMD to tackle the tricky situation.

Free the resource

For beginners, it is important to free the variable when it goes out of scope or when you are done using it. Similar caution must be taken if an exception is thrown and action is to be performed. Some of them might say that java has its own garbage collector, yes it is true, but sometimes the collector does not start which might lead to memory overflow and the program might get stuck. Thus, keep in mind to free the resource once when the use is over.

Unavoidable redundant declaration

Do not initialise the data members of the class as 0, null or false. These values are by default initialised values of member variables in java.

Example 1, the below code is redundant:

public class School {
    private String name_student = null;
    private int age_student = 0;
    private boolean status = false;
    }

Example 2 having redundant declaration:

public class School {
    private String name_student;
    private int age_student;
    private boolean status;
    
	School()
{
   	name_student = null;
     age_student = 0;
   	status = false;
	}
}

Loops

The basic practice to use a loop was using indexes or counters. But with the evolution of Java 5, enhanced forEach loop is the trend. Sometimes the index variable can be error-prone as it can change somewhere in the program and disturb the logic of the program. Using forEach can make the code more clearer and sunnict. Below is an example of forEach and for loop using indexes –

For loop example

int Size = 20_971_520;
    long Balance = 1_000_000_000_000L;
    float pi = 3.141_592_653_589F;

Now the three variables size, balance and pi are more human readable.

Private class members

The best practice is to declare all the data members as private. We should minimise the accessibility of class members. The practice is very useful in hiding information or encapsulation during the system design process.

For example

Class Student
{
    int age = 10;
   String name = “Alice”;
}

The field such as age and name can be changed very easily as –

Student student = new Student();
student.name = “”;
student.age = 15;

Spring framework familiarity

Modern Java development relies heavily on the Spring framework. It has been reported that companies are using Spring framework components, such as Spring MVC and Spring Boot, for the development of microservices and web apps. The Spring framework has many advantages that allow developers to turn a local Java method into a management operation or remote process by understanding all of the advantages it offers.

Taking the approach of someone who has never used Spring before, we recommend you begin familiarising yourself with the principles of Inversion of Control (IoC), Dependency Injection (DI), and Spring MVC in general before attempting to use the more advanced features.

Spring Boot will certainly be the next big milestone in the development of web applications and the capabilities that it brings to the table for web application development.

Spring Boot will certainly be the next big milestone in the development of web applications and the capabilities that it brings to the table for web application development.ome from a more XML-based background).

OOPs practices

A common mistake that every beginner makes is learning the definitions of OOPS concepts. Implement all the OOPs concepts such as data abstraction, polymorphism, inheritance by considering real-life scenarios. Learn how to apply OOPS principles while system design, object modelling, SOLID principles, etc.

Conclusion

Java is the most important industry language that is in trend. Companies like Airbnb, google, Uber, Paytm, Microsoft, etc, use java in their applications. When it comes to API and spring frameworks it works extremely smooth and in-demand language.

Also, in testing like selenium testing, unit testing, java is the basic requirement.

Read some important topics of Java Programming:

  1. Java String Pool
  2. Java Trim()

Happy Learning!

3. Express.js Lessons. Templating with EJS: Layout, Block, Partials

In real life we generally have more than one template. Moreover, if we create a website with several pages, it usually happens that a number of them are of the same design. The template systems should consider this aspect. Unfortunately, ejs doesn’t deal very well with this task. That’s why we are going to install a different templating system named ejs-locals(let us add it to app.js):

Leave a Reply