4

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

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

This is the code one of my colleagues wrote.

@Override
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?

Justin
  • 24,288
  • 12
  • 92
  • 142
Imesh Chandrasiri
  • 5,558
  • 15
  • 60
  • 103
  • 1
    As long as you're consistent it doesn't matter which one you use, IMO. Consistency is the biggest thing to me – Patashu Apr 17 '13 at 05:00
  • The first method just saves you the memory the `outletList` reference consumes. – codeMan Apr 17 '13 at 05:00
  • 7
    This is not optimization, is just code style. For readability, is the same. For debugability, I would choose the second. – Luiggi Mendoza Apr 17 '13 at 05:00
  • 2
    @codeMan can you prove what you said? – Luiggi Mendoza Apr 17 '13 at 05:01
  • If the list is made final in the second case, then both the samples would be similar I guess. – Sudhanshu Umalkar Apr 17 '13 at 05:01
  • @Luiggi: A reference is unnecessarily created in 2nd case. you agree? – Lokesh Apr 17 '13 at 05:03
  • is there any performance degrade when it comes to large scale programming. since this code is going to be used in a very large scale! and please don't down vote this question since it might be important to beginners in programming to learn good practice! – Imesh Chandrasiri Apr 17 '13 at 05:04
  • @LuiggiMendoza yeah, `outletList` being a reference variable needs to be sotored and it consumes some memory. There is nothing to prove. – codeMan Apr 17 '13 at 05:05
  • @codeMan AFAIK it will be the same reference... – Luiggi Mendoza Apr 17 '13 at 05:06
  • 1
    @DimalChandrasiri :- Downvote.. The question deserves a up-vote – Jigar Pandya Apr 17 '13 at 05:07
  • @LuiggiMendoza what do u mean by `same reference`?? – codeMan Apr 17 '13 at 05:10
  • A single assignment of a local variable is extremely cheap and often free. I guess the best way boils down to personal taste (or official coding guideline in your case). Refer this question for details: http://stackoverflow.com/questions/14825172/is-it-faster-to-create-a-new-object-reference-if-it-will-only-be-used-twice – prashant Apr 17 '13 at 05:11

4 Answers4

3

I would prefer first one in your case. You are unecessarily creating a new reference in 2nd case which goes to thread stack occupying some memory. So i would go with first.

EDIT:

Elaborating my answer based on comments. An object is created on heap but all the references to that object go to Thread Stack.

So if multiple threads refer to same object then they will store their own reference in their stack.

EDIT:

Check the link Where is allocated variable reference, in stack or in the heap? for details of how references are stored

Community
  • 1
  • 1
Lokesh
  • 7,810
  • 6
  • 48
  • 78
  • so the better way is to just return the value directly! – Imesh Chandrasiri Apr 17 '13 at 05:05
  • -1: Can you really prove that you're occupying memory in 2nd case? I don't see any generated byte code that proves your theory. – Luiggi Mendoza Apr 17 '13 at 05:05
  • 1
    @Luigi: Where will "outletList " be stored? it has to go thread stack. – Lokesh Apr 17 '13 at 05:06
  • So now you're saying this will be on the Stack and not on the Heap? Are you really sure what are you talking about? – Luiggi Mendoza Apr 17 '13 at 05:07
  • 1
    @Luigi: Yes i am. Objects are stored on heaps no references. Where do you think references are stored? – Lokesh Apr 17 '13 at 05:07
  • Would also prefer the first variant. In my opinion the code is more readable. – Micha Apr 17 '13 at 05:08
  • I would expect a good answer that proves what is saying, not just based on opinions (and misleading about Heap and Stack memories...). Please provide a well-explained answer i.e. [this one](http://stackoverflow.com/a/13531615/1065197) – Luiggi Mendoza Apr 17 '13 at 05:14
  • 1
    @Luiggi: There is nothing really to prove. I just stated a theoretical fact about java that references go to Stack. Is it misleading? Not sure. Check this link :http://stackoverflow.com/questions/873792/where-is-allocated-variable-reference-in-stack-or-in-the-heap – Lokesh Apr 17 '13 at 05:18
3

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.

Ankit
  • 6,554
  • 6
  • 49
  • 71
  • here the outletDao.getOutlets() just returns a list of outlets without taking parameters, so I don't think there will be any exceptions from this code.! – Imesh Chandrasiri Apr 17 '13 at 05:12
  • 1
    what if outletDao is null or if getOutlets throws any uncaught exception? – Ankit Apr 17 '13 at 05:13
  • Agreed. But if you start creating lots of local variables inside the method then it can degrade performace as more time will be spent on garbage collection and stack memory will be occupied more. – Lokesh Apr 17 '13 at 05:14
  • outletDao is initialized prior to calling it in class level so will there be a possibility it can be null? Furthermore getOultets is method which uses hibernate to communicate with the database. Exceptions are handled inside the method! – Imesh Chandrasiri Apr 17 '13 at 05:16
  • 1
    loki: 1st of all GC is more applicable term for object, since memory occupied by a variable is freed as soon as it goes out of scope. @DimalChandrasiri i haven't seen the whole code, i just pointed the possibility, if you are sure it wont throw any exception, it shouldn't. – Ankit Apr 17 '13 at 05:17
  • @loki that's the job of JVM. What you states is part of micro optimization, and as a Java programmer you should know that [premature optimization is the root of all evil](http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html) – Luiggi Mendoza Apr 17 '13 at 05:20
  • @Luiggi: I am just answering in context of the question while comparing 2 approaches mentioned. Yes i agree in normal cases you wont achieve much optimization. But questions talks about comparison thts it. – Lokesh Apr 17 '13 at 05:24
1

I think the first one (returning directly without creating variable) is better because we should always avoid creating variables if they are not useful.

There is not much performance hit but still make it a practice not to create useless variables.

Someone said that it will be GCed as soon as it goes out of scope but according to my understanding there is no fixed time when GC runs and you cannot force it to run. So it will stay in memory till the next time GC runs.

Popeye
  • 1,548
  • 3
  • 25
  • 39
0

There's no performance penalty for #2. If it's executed frequent enough, VM will take care of optimizing it.

The #2 style is redundant and unnecessary, but no big deal.

ZhongYu
  • 19,446
  • 5
  • 33
  • 61