Logo Questions Linux Laravel Mysql Ubuntu Git Menu

What is the Convention for returning a value?

I wrote the following code in my application. ( office work )

public List<Outlet> getAllOutletForTouch() {
    return outletDao.getOutlets();

This is the code one of my colleagues wrote.

public List<Outlet> getAllOutletsForMobile() {
    List<Outlet> outletList = outletDao.getOutlets();
    return outletList;

He created a new variable, assigned the values, and then returned the values; whereas I just returned the values directly calling the method. What is the convention for doing this?

like image 350
Imesh Chandrasiri Avatar asked Mar 25 '23 07:03

Imesh Chandrasiri

1 Answers

As such there is no considerable performance difference in 2nd option as compare to the 1st, even on large scale, since as soon as the reference goes out of scope it will be GCed.

But it mostly about the coding style. IMO and as @Luiggi said in comments, 1st option is more readable but doesn't allow you to debug on return. If the return statement can throw exception that you might wanna debug, you need to go with 2nd option.

like image 109
Ankit Avatar answered Apr 06 '23 00:04