如何“自我记录”可以代码不用烦人吗? [英] How "self-documenting" can code be without being annoying?

查看:177
本文介绍了如何“自我记录”可以代码不用烦人吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这里是最好的做法,但是我经常看到缩写变量名,特别是当范围很小的时候。所以(使用简单的Ruby示例)而不是 def add_location(name,coordinates),我看到像 def add_loc(name,coord) code> - 甚至可能会看到像$ code> def add_loc(n,x,y)

I am not sure what the best practices are here, but I often see abbreviated variable names especially when the scope is small. So (to use simple Ruby examples) instead of def add_location(name, coordinates), I see things like def add_loc(name, coord)—and I might even see something like def add_loc(n, x, y). I imagine that longer names might tire a person out whose used to seeing abbreviations.

详细程度有助于可读性,还是伤害每个人的眼睛? - 人们喜欢缩写缩写,缩短姓名吗?

Does verbosity help readability, or does it just hurt everyone's eyes?—Do people prefer abbreviations and shortened names over longer names?

推荐答案

个人而言,我宁愿看到更长的名字,没有必要首先确定上下文的东西。当然,没有实际意义的变量,如计数器,我仍然使用小的无意义的变量名(如 i x ),但大多数情况下,详细程度是清晰度。这对于公共API尤其如此。

Personally, I would MUCH rather see longer names that actually mean something without having to determine the context first. Of course, variables that don't lend real meaning, such as counters, I still use small meaningless variable names (such as i or x), but otherwise verbosity is clarity most of the time. This is especially true with public APIs.

然而,这可能要太远了。我曾经看过过一些VB代码,这样可笑。像其他一切一样适应!

This can be taken too far, however. I've seen some VB code in the past that way ridiculous. Moderation like everything else!

这篇关于如何“自我记录”可以代码不用烦人吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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