简体   繁体   English

如何在Java中正确使用方法结果

[英]How to use method result properly in Java

I will use result of a method call in some calculation. 我将在一些计算中使用方法调用的结果。 I have two ways: 我有两种方法:

  1. Invoke method once and store the return into a local value, then use the local value in some calculation. 调用一次方法,将返回值存储为局部值,然后在某些计算中使用局部值。

  2. Use call method many times. 多次使用调用方法。

Please see my sample code: 请查看我的示例代码:

public class TestMethod {

    public void doSomething_way1() {

        String prop1 = this.getProp1();
        if (prop1 != null) {
            String value = prop1 + " - another value";
            System.out.println(value);
        }
    }

    public void doSomething_way2() {

        if (this.getProp1() != null) {
            String value = this.getProp1() + " - another value";
            System.out.println(value);
        }   
    }

    public String getProp1() {
        return "return the same value";
    }
}

NOTE that, the method doSomething will be invoked a lots at a time ( In web environment ) 注意,方法doSomething将一次调用很多(在Web环境中)

Can someone show me which way I should use in the case the result of method will be used at least 3 times? 有人可以告诉我在方法结果至少要使用3次的情况下应该使用哪种方式吗?

我相信多次使用方法调用会更直观,并使代码更具可读性。

In your case it wont matter even if you give call to the getProp1() method multiple times. 在您的情况下,即使多次调用getProp1()方法也没有关系。 Because it does not perform any computation, object creation or I/O operation which may cause performance issues. 因为它不执行任何计算,所以可能会导致性能问题的对象创建或I / O操作。

You could go a step further: 您可以进一步:

public void doSomething_way2() {

    if (this.getProp1() != null) {
        System.out.println(this.getProp1() + " - another value");
    }

}

If the method is getting called a lot (I mean many, many times a second), creating the extra variable could change performance a tiny bit with respect to garbage collection and what not... I think its trivial. 如果该方法被调用很多(我的意思是每秒很多次),那么创建额外的变量可能会在垃圾收集方面改变性能,而对于其他方面则不是如此……我认为这很简单。

In some cases, getting the value more than once could raise thread-safety issues, if the value weren't final , whereas if you fetch the value once, at least the entire operation of way1 will be consistent with a single value for prop1. 在某些情况下,如果该值不是final ,则多次获取该值可能会引发线程安全问题,而如果一次获取该值,则至少way1的整个操作将与prop1的单个值一致。

But even if threading weren't an issue, I still think it's better, stylistically, to 'cache' the value in a local variable which is well named. 但是,即使线程不是问题,但从风格上讲,我仍然认为将值“缓存”在一个命名良好的局部变量中会更好。

(I'm assuming that your real code does something more significant than return the fixed String "something") - the getProp1 method as written is pretty thread-safe. (我假设您的实际代码比返回固定的字符串“ something”更有意义)- 编写getProp1方法是线程安全的。 :) :)

From a performance standpoint, at least from the examples given, it does not appear to be any fundamental difference doing it one way or another. 从性能的角度来看,至少从给出的示例来看,以一种方式或另一种方式进行操作似乎没有任何根本区别。 Object allocations for small numbers of iterations (unless they are heavyweight objects) should be minimal. 少量迭代(除非它们是重量级对象)的对象分配应该最小。

However, from a programming design and implementation standpoint, it may be helpful to keep the program 'cohesive', ie have classes more closely represent things. 但是,从编程设计和实现的角度来看,使程序保持“内聚”(即使类更紧密地表示事物)可能会有所帮助。

In which case the local variable from the return of the method (as it is a different 'thing') and subsequent calculation. 在这种情况下,方法返回的局部变量(因为它是不同的“事物”)和后续计算。

eg: 例如:

interface Dog{
    void wagTail();
    Dung eat(DogFood f);
}

interface Human{
    void pickUpDung(Dung d);
}

codeLoop(Human m, Dog d, DogFood f){
      d.wagTail();
      Dung poo = d.eat(f);
      m.pickUpDung(poo);
}

whereas a less cohesive example would be 凝聚力较小的例子是

interface Dog{
    void wagTail();
    void eatAndHaveHumanPickUp(DogFood f);
}
// you can fill in the rest...

it wouldn't follow the principle of cohesion, because you wouldn't normally expect a dog call to have this kind of method... 它不会遵循凝聚力原理,因为您通常不会期望狗叫具有这种方法...

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

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