Deva
Deva

Reputation: 3949

Which is a better practice in java

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.

Upvotes: 1

Views: 146

Answers (9)

GingerHead
GingerHead

Reputation: 8230

The first one:

Is good on the side of writing neat and more understandable code, but it uses more memory in initializing a Sring object The compiler will switch to the first anyway, secondly when any other developer wants to edit, he can add code directly under the first initialization, if he needs to debug it's also better.
Good for code management.

The second one:

Is better in performance where there is no additional object initialization, but just outputing the needed return data. But it is not a good practice in terms of code writing, but it is in terms of performance and quickness in writing code.

Upvotes: 0

Whimusical
Whimusical

Reputation: 6629

The first one can be more intuitive (in the college they show it that why and I think that´s why you doubt about this) just for the purpose of learning and debugging any possible exception but in general the second is preferred and nicer.

Upvotes: 0

sp00m
sp00m

Reputation: 48807

+1 for the second solution. The less you write, the better you develop!

Upvotes: 0

Chris Thompson
Chris Thompson

Reputation: 35598

This is going to come down almost entirely to style (and the first one is crappy style -- don't create things you aren't going to use). Any good compiler would notice that there are some optimizations to be had here by eliminating the unused local variable. It's really easy to get caught up in making minor adjustments that you view as "optimizations" but are really just an attempt for the developer to feel clever and waste time. Even if the compiler didn't optimize the first code segment into the second code segment (and actually it could potentially be optimized even further).

When it comes to performance, the larger-picture, architecture decisions are what's really going to make a difference, not one unused allocation/assignment operation. Don't get bogged down in line-by-line optimization. Chances are those optimizations are better left to the compiler.

Upvotes: 1

Andy
Andy

Reputation: 10830

I believe the compiler would turn the second into the first, hence optimizing it for you. It won't do that for all things, but in most instances it would. But it all depends on readability. Whatever you will be able to catch quicker, I say stick with that. But they are both the same. Only difference in the first one you are making a variable for the pointer to that string, hence "wasting" unnecessary memory. But again, the compiler will probably optimize that in that case.

Upvotes: 1

eabraham
eabraham

Reputation: 4164

The second is a better option because it requires less typing and reduces unnecessary code. Always try to make you code as readable as possible.

Upvotes: 0

stevevls
stevevls

Reputation: 10843

As with any programming language, you want to express it as concisely and clearly as possible. They'll both compile down to the exact same thing, but the latter is much more readable and omits un-needed lines of code.

Upvotes: 1

Chaos
Chaos

Reputation: 11721

The 2nd one is the better option. In the 1st one, your variable data serves no actual purpose. Anyway, I think if this thing is compiled, the compiler will change the 1st one to 2nd one anyway.

Upvotes: 0

twain249
twain249

Reputation: 5706

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.

Upvotes: 4

Related Questions