Posts

Showing posts with the label best practices

Running SQL Queries on Production/live Databases - 5 Things to Remember

Did you ever face a situation where some of your innocuous looking action has cause production issue and that too big time? Well, I hope you have not because it's certainly, not a pleasant experience. One of such innocuous looking action is running SQL queries on production databases. I had that in past very early in my career where I removed some config as duplicates only to find after a week that it stopped publishing messages to one of the downstream. When you work in complex systems which has so many components, millions of lines of code, thousands of configuration and many databases with hundreds of tables, you have to be really careful with anything you do. Often there is on the real way to perform a production-like testing, hence the best bet is to keep your change as much isolated and limited as possible. Read more �

10 Programming Tips to Create Maintainable Java Applications

Hello all, today is 31st December, the last day of 2016 and this is probably the last post on Javarevisited for this year and I want to make it special. That's why I am going to share you some tips on creating maintainable Java applications which will help you not just in the new year, but also in all the coming years in your software development career. One aspect of development, which is often overlooked by developers is to create applications which are both easy to maintain and support . Since a software spends 90% of its lifetime in maintenance mode, it's very important that your application is easy to configure, support, and maintain. Also, Java application is no different than any other software, you must pay attention to these key properties of writing production quality code, hence this tips also applies to any programming language which is used to create real-world software which is used by real users. Read more �

Best practices to name your JAR file in Java

If you are an author of an internal, proprietary Java library or an external open source library, or you are one of those lucky developers who ship Java application by yourself then you should follow these best practices while naming your JAR files. These best practices are a result of the practical experience of using hundreds of Java library and application. Following these best practices will help in better management of JAR files. It's part of my other best practices articles e.g. best practices while naming variable, writing comments, overriding methods, muli-threading , JDBC , and best practices while dealing with passwords. If you are interested in learning more best practices, you can always search those articles on this blog. Read more �

10 Exception handling Best Practices in Java Programming

Exception handling is an important part of writing robust Java application. It�s a non functional requirement for any application, to gracefully handle any erroneous condition like resource not available, invalid input, null input and so on. Java provides several exception handling features, in built in language itself in form of try , catch an d finally keyword . Java programming language also allows you to create new exceptions and throw them using throw and throws k eyword. In reality, Exception handling is more than knowing syntax. Writing a robust code is an art more than science, and here we will discuss few Java best practices related to Exception handling. Th ese Java best practices are f ollowed even in standard JDK libraries, and several open source code to better deal with Errors and Exceptions. This also comes as handy guide of writing robust code for Java programmers. Read more �

How to create Immutable Class and Object in Java - Tutorial Example

Writing or creating immutable classes in Java is becoming popular day by day, because of concurrency and multithreading advantage provided by immutable objects. Immutable objects offers several benefits over conventional mutable object, especially while creating concurrent Java application. Immutable object not only guarantees safe publication of object�s state, but also can be shared among other threads without any externa l synchronization . In fact JDK itself contains several immutable classes l ike String , Integer an d other wrapper classes. For those, who doesn�t know what is immutable class or object, Immutable objects are those, whose state can not be changed once created e.g. java.lang.String , once created can not be modified e.g. trim, uppercase, lowercase. All modification in String result in new object, see why String is immutable in Java for mor e details. In this Java programming tutorial, we will learn, how to write immutable class in Java or how to make a class immu...

Method and Constructor Overloading Best Practices in Java

You need to be careful while overloading a method in Java, especially after the introduction of autoboxing in Java 5. Poorly overloaded method not only adds confusion among developers who use that but also they are error prone and leaves your program on compiler's mercy to select proper method. One of the best examples of a poorly overloaded method is the remove method of ArrayList . There are two versions of remove, first, one which takes an Object as argument i.e. remove(Object element) and the second one, which takes an index as argument i.e. remove(int index) . It worked fine until Java 1.4 where there is clearly a distinction between primitive types and objects type but in Java 1.5, where you can pass an int primitive to a method which accepts an Integer object, creates some nasty problem. Read more �

Why getter and setter are better than public fields in Java

public modifier vs getter and setter method in Java Providing getter and setter method for accessing any field of class in Java may look unnecessary and trivial at first place, simply because you can make field public and it�s accessible from everywhere in Java program . In fact many programmers do this in there early days but once you start thinking in terms of enterprise application or production code, you will start seeing how much trouble it can create in terms of maintenance . Since as per SDLC process, software spends more time in maintenance than development, it�s worth keeping ease of maintenance as one of goal in development. In reality using getter and setter method in Java is one of the Java coding best practice much like using @Override annotation while overriding method in Java . Main problem with making field public instead of getter and setter is that it violates Encapsulation by exposing internals of a class. Once you exposed internals of class you can not change i...

Why use @Override annotation in Java - Coding Best Practice

@Override annotation was added in JDK 1.5 and it is used to instruct compiler that method annotated with @Override is an overridden method from super class or interface . Though it may look trivial @Override is particularly useful while overriding methods which accept Object as parameter just like equals , compareTo or compare() method of Comparator interface. @Override is one of the three built in annotation provided by Java 1.5, other two are @SuppressWarnings and @Deprecated . Out of these three @Override is most used because of its general nature, while @SuppressWarnings is also used while using Generics, @Deprecated is mostly for API and library. If you have read my article common errors while overriding equals method than you have see that one of the mistake Java programmer makes it, write equals method with non object argument type as shown in below example: Read more �

Top 10 JDBC Best Practices for Java Programmer

Java JDBC Best practices JDBC Best Practices are some coding practices which Java programmer should follow while writing JDBC code. As discussed in how to connect to Oracle database from Java , JDBC API is used to connect and interact with a Database management System. We have touched some of the JDBC best practices in our last article 4 JDBC Performance tips , On which we have discussed simple tips to improve performance of Java application with database. By using JDBC you can execute DDL , DML and Stored Procedures. JDBC Best practices is probably most significant set of coding practices in Java because it significantly affect performance of Java application. I have seen substantial performance gain by simply following common JDBC best practices like running queries with auto commit mode disable. One of the query which we used in our example of JDBC Batch update was taking almost 30 second to finish with auto commit mode enabled but it just took under one second with auto commi...