使用变量存储值还是直接从对象中获取值? [英] Using variables to store values or directly get values from the objects?

查看:74
本文介绍了使用变量存储值还是直接从对象中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种情况是,我需要至少两次访问一个值. IE.我正在使用记录器来跟踪应用程序中正在发生的事情.我想记录该函数正在运行的对象的名称,然后再执行具有相同名称的操作(即检查它是否包含一些字符串或将其放入数组中).

The scenario is that I need to access a value at least twice. I.e. I am using logger to keep track of what is happening in the application. I want to log the name of the object, that function is working on, and later on do something with the very same name(i.e check if it contains some string or put it into an array).

将名称存储在变量中:

foo(Bar bar){
    String name = bar.getName();
    logger.info("I am working with "+name);
    this.doSomethingWith(name);
}

或两次调用getName():

Or calling getName() twice:

foo(Bar bar){
    logger.info("I am working with "+bar.getName());
    this.doSomethingWith(bar.getName());
}

我了解,在第一种情况下,我将创建一个新的String,为其分配一个值,然后两次检索该值.这样我可以使用更多的内存资源,对吗?

I understand, that in the first scenario, I will create a new String, assign a value to it and then retrieve this value twice. This way I am using more memory resources, correct?

在第二种情况下,我是否两次访问对象栏,然后两次访问其名称.我想这不是DRY方法.但另一方面,我不是在记忆中重复自己,对吗?

In second scenario, am I accessing object bar twice and then accessing it's name twice. I suppose this is not a DRY approach. But on the other hand I am not repeating myself in the memory, correct?

哪种方法更好?

推荐答案

我个人更喜欢第二种方法. 确实,我通常只在必要时创建临时变量.

Personally, I prefer the second approach. Indeed, I usually create temporary variable solely when necessary.

马丁·福勒( http://en.wikipedia.org/wiki/Martin_Fowler )遵循本指南.他在我读过的书中提到了它:

Martin Fowler ( http://en.wikipedia.org/wiki/Martin_Fowler ) also follows this guideline. He relates it within his book that I've read:

http://www.amazon.fr/Refactoring-改善设计现有代码/dp/0201485672

有关此主题的书的免费摘录在这里:

A free extract of its book concerning this topic is here:

http://sourcemaking.com/refactoring/replace-temp-with-query

有人会争辩说删除临时变量可能会导致性能问题.

Some people would argue removing temporary variables could lead to performance issue.

正如马丁·福勒(Martin Fowler)所说:

As Martin Fowler says:

在这种情况下,您可能会担心性能.和其他一样 性能问题,暂时让它滑下来.九次 十,没关系.当这很重要时,您将解决问题 在优化过程中.更好地考虑代码因素后,您通常会 找到更强大的优化,而如果没有这些优化,您可能会错过 重构.如果情况变得更糟,就很容易 回来.

You may be concerned about performance in this case. As with other performance issues, let it slide for the moment. Nine times out of ten, it won’t matter. When it does matter, you will fix the problem during optimization. With your code better factored, you will often find more powerful optimizations, which you would have missed without refactoring. If worse comes to worse, it’s very easy to put the temp back.

但是无论如何,这是一个品味问题.有些人发现第一种方法更具可读性,其他人则发现第二种.我真的更喜欢第二个,因为我讨厌为临时变量添加没有实际值的行:)

But anyway, it's a matter of taste. Some people find more readable the first approach, others the second. I really prefer the second because I hate temporary variables adding lines for no real values :)

这篇关于使用变量存储值还是直接从对象中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆