将信息存储在对象上作为该对象的属性是不好还是好的做法? [英] Is it bad or good practice to store info on an object as an attribute of that object?

查看:50
本文介绍了将信息存储在对象上作为该对象的属性是不好还是好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了包含许多数据表和其他对象的代码.当其他人需要查看我的代码时,他们通常不知道我创建的不同数据表之间的区别是什么.因此,我想知道是否建议仅将特定数据表的描述存储为该数据表的属性.

I have written code with many data tables and other objects. When other people need to look into my code, they often have no idea what the difference is between the different data tables I have created. I was therefore wondering whether it is advised to simply store a description of a particular data table as an attribute of that data table.

例如:

animal = c('rabbit', 'dog', 'cat')
food   = c('carrot', 'bone', 'fish')
DT     = data.table(animal, food)
attr(DT, 'information') <- 'table that holds info on what different animals eat'
attributes(DT)$information

推荐答案

我想您可能会争辩说这个问题正在要求选择,但是在任何人介入之前,让我们先看看已经认可使用属性的代码以此目的. comment 函数用于设置机器人并获取此类属性. comment 功能帮助页面的标题为:查询或设置"comment"属性".

I suppose you could argue that this question is asking for opintion, but before anyone leaps in with that, let's look at the code that already endorses the use of attributes for this purpose. The comment function exist for bot setting and getting such an attribute. The comment functions help page is entitled: "Query or Set a "comment" Attribute".

弗兰克·哈雷尔(Frank Harrell)的Hmisc软件包还具有 label 函数,该函数将信息性字符串附加到数据框列,还有一个标签"函数,其功能与数据框本身相同.

Frank Harrell's Hmisc package also has a label function that attaches informative strings to dataframe columns, as well as a `Label" function that does hte same to the dataframe itself.

有证据支持的观点:遵循弗兰克在编程实践中的领导是好的".

Opinion supported by evidence: following Frank's lead in programming practice is "good".

这篇关于将信息存储在对象上作为该对象的属性是不好还是好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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