Methods in Java


Methods_In_Java

Variables are used to store data in java run-time.  Methods are used to process access and manipulate the variables. Methods are primary building block in enterprise application as it holds the business logic of an enterprise. Each object in java talks with other objects by invoking methods. Good programmers write in a modular fashion which allows for several programmers to work independently on separate concepts which can be assembled at a later date to create the application.  The use of methods will be our first step in the direction of modular programming.  Let us discuss more about methods with example of  Premium payment for the insurance policy.a

 

public class PremiumCollector {
public void payPremium(int id, float amount)  {

Insurance insurance = getDetailsofInsuranace(id);

pendingPayment = getPendingPaymentDetails(id);

addPaymentToInsurance(insurance,amount,pendingPayment);

System.out.println(“Premium paid successfully”);

}

}

In OOPS, we build an application using classes and methods. Writing a method would be simple and it would take few parameter, process the values and return the result if required, otherwise null. While write a method we have to follow industry standard practices.  Let us discuss one by one here.

Method name should be camel-Case, means that use lowercase for the first word and capitalize only the first letter of each subsequent word that appears in method name.Method name should be verbs such as payPremium, addPayment, getName. Try to design method with maximum of 4 parameters. Writing method with only one task and with minimal arguments increases the re-usability of the code and easy to understand.  For instance look at the above method payPremium. It is calling 3 methods and all the methods can be used by other methods if required. Any method want to get premium pending amount, it can use getPendingPaymentDetails(). Java method is pass by value. Not pass by reference. It would pass the copy of the argument values into methods.

Method would return value, otherwise void. Always return proper value or empty values from methods. Do not ever return “null” to caller from method. It would required all the caller to handle the null and have to add null check. So better return empty value, empty arrays, empty collections, not null. Another important coding practice to consider is  don’t mute the exception inside the method without error logging error message or returning to caller.  If exception in method, handle it properly or throw to caller to handle. Don’t mute the exception as it would make the debugging tough if any issue in that code. In worst case, log the exception message in catch block. For example look at below example,

public void payPremium(int id, float amount) {

try{

Insurance insurance = getDetailsofInsuranace(id);

pendingPayment = getPendingPaymentDetails(id);

addPaymentToInsurance(insurance,amount,pendingPayment);

System.out.println(“Premium paid successfully”);

}catch(Exception e){}   //Do not mute exceptions. It will make debugging complex.

}

 

Best practice would be as below,

 

public void payPremium(int id, float amount) throws Exception {

try{

Insurance insurance = getDetailsofInsuranace(id);

pendingPayment = getPendingPaymentDetails(id);

addPaymentToInsurance(insurance,amount,pendingPayment);

System.out.println(“Premium paid successfully”);

}catch(Exception e){

System.out.println(“EXCEPTION: exception in payPremium. exception is” + e.getMessage());

throw e;

}

 

Finally comments for methods. Write a single line comment at complex logic lines involved. Don’t write comments for all the lines or for simple if…else condition. Write comments for line which takes some complex logic or business decision.  Next write doc comments for all the exposed API elements like below, /

** This method would pay the premium for insurance policy.

It gets insurance id and amount to be paid.

@Param id

@Param amount */

public void payPremium(int id, float amount) throws Exception {

………………………………….

}
We all discussed design principle of writing methods  method name, method argument list, method exception handling, return values. Kindly share us if you feel anymore fact to be considered while design a method. Happy Reading!!

You may also like