toString():用于调试还是用于人类?
class Address
{
private enum Component
{
NUMBER,
STREET,
STATE,
COUNTRY
}
private Map<Component, String> componentToValue = ...;
}
我希望我的类包含两种方法:
- 一种用于指示每个地址组件的值(这样我可以在出现问题时进行调试)。
- 以人类期望的形式返回地址:“1600 Amphitheatre Parkway Mountain View, CA 94043”。
Object.toString() 的最佳实践是什么?它主要是针对#1还是#2?这些方法的命名是否有最佳实践?
class Address
{
private enum Component
{
NUMBER,
STREET,
STATE,
COUNTRY
}
private Map<Component, String> componentToValue = ...;
}
I'd like my class to contain two methods:
- One to indicate the value of each address component (so I can debug if anything goes wrong).
- One to return the address in a form expected by humans: "1600 Amphitheatre Parkway Mountain View, CA 94043".
What is the best-practice for Object.toString()? Is it primary meant for #1 or #2? Is there a best-practice for the naming of these methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您是否会在 SMS 消息和 HTML 页面中以相同的方式设置地址格式?英语、法语和日语的格式是否相同?
如果不是,那么您就有了答案:表示不属于该对象,而是属于显示该对象的表示层。除非该对象是专门为表示层制作的,例如如果它是 HtmlI18nedAddress,则使用 toString 进行调试。
考虑
Date
与SimpleDateFormat
。Date
包含状态,SimpleDateFormat
返回多种表示形式。Would you format an address the same way in a SMS message and in an HTML page? Would you format it the same way in English, French and Japanese?
If no, then you have your answer : the presentation does not belong to the object, but to the presentation layer displaying the object. Unless the object is specifically made up for the presentation layer, for example if it is a HtmlI18nedAddress, use toString for debugging.
Consider
Date
vsSimpleDateFormat
.Date
contains the state andSimpleDateFormat
returns multiple representations.我想说的是第一个。数据格式化不应硬编码到对象的 ToString() 函数中。
我这样看:我尝试使 ToString() 输出数据可由匹配的 Parse(string data) 函数读取(该函数是否实际存在并不重要)。因此,在这种情况下,如果您想要特定的格式,请编写特定的函数,并将通用数据转储例程留给 ToString()。
I would say the first. Data formatting should not be hard coded into the ToString() function of the object.
I look at it this way: I try to make my ToString() output data that is readable by a matching Parse(string data) function (if that function actually exists or not is not important). So in this case, if you want a specific formatting, write a specific function, and leave the generic data dump routines to ToString().
我通常使用 Apache Commons ToStringBuilder http: //commons.apache.org/lang/api-2.5/org/apache/commons/lang/builder/ToStringBuilder.html 仅包含我认为调试绝对必要的部分。
I normally use the Apache Commons ToStringBuilder http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/builder/ToStringBuilder.html with only the parts that I think are absolutely necessary for debugging.
根据 Effective Java 第 12 项:“始终重写 toString “
toString()
的约定是:因此,它是为了调试。
关于
toString()
的更多注释:我强烈推荐投资《Effective Java》这本书。这是一本非常好的读物。一个项目只需五到十分钟,但您的 Java 生活将永远改变!
According to Effective Java Item 12: "Always override toString" the contract for
toString()
is:Thus, it is for debugging.
More notes on
toString()
:I highly recommend investing in the book "Effective Java". It is a very nice read. Just five to ten minutes for an item, but your Java live will change forever!
您可以从动态配置的某些调试属性中读取:
You could read from some debug property that you configure dynamically.:
ToString 通常应该仅用于调试信息。请记住,您正在重写
Object
上的方法;对Object
引用进行方法调用返回人类可读的地址在概念上准确吗?在某些情况下,根据项目的不同,这实际上可能是有意义的,但对我来说听起来有点奇怪。我会实施一种新方法。另一件需要注意的事情是,大多数现代 IDE 在检查对象时使用 ToString 方法来打印有关对象的调试信息。
ToString should generally only be used for debug information. Keep in mind that you're overriding a method on
Object
; is it conceptually accurate to have a method call on anObject
reference return a human readable address? In some cases, depending on the project, this may actually make sense, but it sounds a bit odd to me. I would implement a new method.Another thing to note is that most modern IDEs use the ToString method to print debug information about objects when inspecting them.
您可以做的一件事是使用调试标志来根据需要更改此设置:
当然,这假设您有一个带有调试标志的实用程序类。
One thing you can do is use a Debug flag to change this as you like:
Of course this assumes that you have a utility class suet up with a debug flag in it.