简体   繁体   中英

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.

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.

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.

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.

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.

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.

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.

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

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.

第一个可以更直观(在大学中,他们表明为什么,我认为那是您对此表示怀疑的原因)只是出于学习和调试任何可能的异常的目的,但总的来说,第二个是更好的和更好的选择。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM