相同变量作为不同类型的命名约定?
许多人认为匈牙利表示法不好。那么如何命名代表相同值并转换为不同类型的变量呢?
我有一个名为 value
的变量,它可能是一个 string
或一个 decimal
。您如何称呼不同的格式? strValue
、decValue
? valueAsString
?
Many people believe Hungarian notation is bad. How then do you name a variables that represent the same value casted to different types?
I've got a variable called value
, that might be a string
, or a decimal
. What would you call the different formats? strValue
, decValue
? valueAsString
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这很大程度上取决于上下文。例如,如果字符串值被命名为age,并且小数点是解析的值,那么可能是parsedAge 或类似的东西。实际上,这取决于您正在做什么以及该变量的生命周期,什么才是有意义的。如果它存在的时间足够长,足以实际收集和解析该值,那么我会给解析的变量提供更好的名称,或者不用担心中介的命名。
如果您实际上需要保留这两个值,那么我可能会考虑创建一个结构或一些类似的数据结构来表示该数据值的各种形式,以防止需要在字符串和十进制格式等之间转换。
I think it would largely depend on the context. For instance if the string value was named age, and the decimal was the parsed value then perhaps parsedAge or something along those lines. Really it comes down to what makes sense given what you are doing and the lifetime of that variable. If it only exists long enough to actually collect and parse the value, then I would give the better name to the parsed variable or worry less about the naming of the intermediary.
If you actually need to hold on to both values, then I might consider creating a struct or some similar data structure that represents the various forms for that data value to prevents the need to shift between string and decimal formats etc.