简体   繁体   English

在Java中这是更好的做法

[英]Which is a better practice in java

I have a method which returns me some object let say a String object. 我有一个方法,它返回一些对象,让我说一个String对象。

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. +1为第二个解决方案。 The less you write, the better you develop! 写得越少,开发就越好!

The first one: first一个:

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. 在编写简洁且易于理解的代码方面很好,但是在初始化Sring对象时会使用更多的memory 。无论如何,编译器将切换到第first ,然后,当任何其他开发人员想要编辑时,他可以在第一个初始化下直接添加代码,如果他需要debug那就更好了。
Good for code management. 适用于代码管理。

The second one: second个:

Is better in performance where there is no additional object initialization, but just outputing the needed return data. 在没有其他对象初始化的情况下performance更好,而仅输出所需的返回数据。 But it is not a good practice in terms of code writing, but it is in terms of performance and quickness in writing code. 但这不是写代码的好习惯,而是写代码的性能和快速性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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