I have a method which returns me some object let say a String object.
Now i have two possiblity one
public String someMethod(){
String data=getData(); //This method returns string
return data;
}
or
public String someMethod(){
return getData();//This method returns string
}
which of the above is a better practice and also a better thing considering performace/scalabilty. Please give your views supporting the answer. Thanks for your time.
If you aren’t going to be modifying the variable then there is no reason to create a local copy of it just to return it.
The second version of the code is cleaner and more concise which is usually preferred.
An optimized compiler will most likely do the transformation for you if you don’t so performance wise they’re equivalent.