您是否使用特定约定来命名互补变量?

时间:2022-03-28 05:36:56

I often find myself trying to come up with good names for complementary pairs of variables; where two variables denote opposing concepts, two participants in some sort of duologue, and so on.

我经常发现自己试图为互补的变量对提出好名字;其中两个变量表示相反的概念,两个参与者表示某种形式,等等。

This might be better explained by a counter-example - I maintain an app that prints two graphics as part of a print advertisement. They're stored in the database as TopLogo and LowerLogo, which I have to stop and double-check every time I use them because I'm expecting top to complement bottom, and lower should complement upper.

这可以通过反例来更好地解释 - 我维护一个应用程序,它打印两个图形作为打印广告的一部分。它们作为TopLogo和LowerLogo存储在数据库中,我每次使用它时都必须停止并仔细检查,因为我期望顶部补充底部,而较低部分应补充上部。

There's some obvious examples that I think work well:

我认为有一些明显的例子可以很好地运作:

client / server
source / target for copying/moving data or files from one variable to another
minimum / maximum

用于将数据或文件从一个变量复制/移动到另一个最小值/最大值的客户端/服务器源/目标

but there's some concepts that just don't lend themselves to such neat naming schemes. For example, when paging through records, does 'last' mean 'final' or 'previous' ? I recently saw some code that used firstPage, previousPage, nextPage and finalPage to avoid the ambiuous lastPage completely, which I thought was very beat, hence this question.

但是有一些概念不适合这种简洁的命名方案。例如,在翻阅记录时,“最后”是指“最终”还是“前一个”?我最近看到一些使用firstPage,previousPage,nextPage和finalPage的代码,以避免完整的ambPageous,我认为这是非常好的,因此这个问题。

Do you have any particularly neat variable name pairs you'd care to share with us? (Bonus points if they're the same length, which makes the code so much neater in monospaced fonts.)

你有什么特别整洁的变量名对你要跟我们分享吗? (如果它们的长度相同,则会获得奖励,这使得代码在等宽字体中更加整洁。)

4 个解决方案

#1


3  

Like with all kinds of code style conventions, consistency is what you should strive for.

I would have the development team agree on "standard" pairs of prefixes for common scenarios like "source/destination" or "from/to" and then stick with them for the whole project. As long as every developer is aware of what is meant with a particular prefix in the codebase, it is easier to avoid misunderstandings.

Exceptions to the rule should be clarified in the documentation if the variable is part of a public API, or in comments within the code, if it's visibility is restricted to a single class or method.

与各种代码样式约定一样,一致性是您应该努力的方向。我希望开发团队就“源/目的地”或“从/到”等常见场景的“标准”前缀对达成一致,然后在整个项目中坚持使用它们。只要每个开发人员都知道代码库中特定前缀的含义,就可以更容易地避免误解。如果变量是公共API的一部分,或者在代码中的注释中,如果可见性仅限于单个类或方法,则应在文档中阐明规则的例外情况。

#2


1  

In my databases you'll find many valid-state temporal ("history") tables containing a pair of columns named start_date and end_date. No bonus points for me, then, because I'd rather use the commonly used 'end' than try to come up with an intuitive alternative with the same number of characters as the word 'start'.

在我的数据库中,您将找到许多有效状态的temporal(“history”)表,其中包含一对名为start_date和end_date的列。那么,对我来说没有奖励积分,因为我宁愿使用常用的“结束”而不是尝试用与“开始”这个词相同数量的字符来提出一个直观的替代品。

I tend to prefer these generic terms even when more context-specific terms may be viable e.g. preferring employee_start_date over employee_hire_date (what if their employment started for a reason other than being formally hiring e.g. their company was the subject of an acquisition). That said, I'd prefer person_birth_date over person_start_date :)

我倾向于更喜欢这些通用术语,即使更多特定于上下文的术语可能是可行的,例如更喜欢employee_start_date而不是employee_hire_date(如果他们的工作开始的原因不是正式招聘,例如他们的公司就是收购的主题)。那就是说,我更喜欢person_birth_date而不是person_start_date :)

#3


1  

While one does try to be semantically coherent in obvious cases -- e.g., maximum goes with minimum, and not "lowest" -- in well-structured OO code (which isn't all code, I know) the problem disappears with a good IDE. Classes are short, methods are short, and variables are few in each method. So it doesn't matter what you call the variable pairs so long as they're clear. Your code might not look professional, but real quality is in the code, not in the look of your code.

虽然人们确实试图在明显的情况下在语义上保持一致 - 例如,最大值是最小的,而不是“最低” - 在结构良好的OO代码中(这不是所有代码,我知道)问题随着一个好的消失而消失IDE。类很短,方法很短,每种方法中的变量都很少。因此,只要它们清晰,你所谓的变量对并不重要。您的代码可能看起来不专业,但代码中的真实质量,而不是代码的外观。

The problem further disappears if there is good JavaDoc or whatever the documentation system is, and if have good Class names that go with them. For instance, if you have an instance of a Connection class and it has a method a method called setDestination, that's okay, but if you know that setDestination takes one parameter called destination and it's of the Server class, you're cool... even though you might prefer to call it target, aimHere, placeToSendTheData, or whatever (and the corresponding names, source, comingFromHere, and placeToGetTheDataFrom). Plus the doc system says what the thing is for, and that is priceless.

如果存在良好的JavaDoc或任何文档系统,并且具有与它们一起使用的良好类名,则问题会进一步消失。例如,如果你有一个Connection类的实例,并且它有一个名为setDestination的方法,那没关系,但是如果你知道setDestination接受一个名为destination的参数而且它是Server类的参数,你很酷......即使您可能更喜欢将其称为target,aimHere,placeToSendTheData或其他(以及相应的名称,source,comingFromHere和placeToGetTheDataFrom)。另外,doc系统说的是什么,这是无价之宝。

This next thing might sound stupid and I'm sure I'll get voted down here on *, but unique non-professional sounding variable names have a great advantage: I know that my variables have names like placeWeWantTheDataToGo (and the IDE takes care of typing it), but the "serious" guys who do the JDK would never use such silly names. So I know immediately that the variable is one of mine. Incidentally, when I worked with developers in Spain and Italy, they write code with Spanish variable names (not always, but usually). This causes the same effect: we can quickly see that the Conexion class is ours, but the Connection class is not.

接下来的事情可能听起来很愚蠢,我确信我会在*上投票,但是独特的非专业声音变量名称有很大的优势:我知道我的变量的名称就像placeWeWantTheDataToGo(并且IDE负责输入它,但是那些做JDK的“严肃”的人永远不会使用这些愚蠢的名字。所以我立即知道变量是我的变量之一。顺便说一句,当我与西班牙和意大利的开发人员合作时,他们用西班牙语变量名称编写代码(并非总是,但通常)。这会产生同样的效果:我们可以很快看到Conexion类是我们的,但Connection类不是。

[Also, instead of typing your variable names, assign them a constant String somewhere in your code and use that, so if they called it lower or downer instead of low, you're still okay.]

[另外,不是输入你的变量名,而是在你的代码中的某个位置为它们分配一个常量字符串并使用它,所以如果它们将它调低或降低而不是低,你仍然可以。

#4


0  

Yes, I do try to name complementary sets of variables systematically so that the symmetry is clear. It is not always easy; sometimes, not even possible. Well, not possible using the rules I lay down for myself - which means I usually try to have the names the same length. The 'top' and 'lower' example would drive me batty (assuming I'm not batty already, which is far from certain); I'd probably use 'upper' and 'lower' because those are the same length; 'top' and 'bottom' would frustrate me too because of the difference in length.

是的,我确实试图系统地命名互补的变量集,以便明确对称性。这并不容易;有时,甚至不可能。好吧,不可能使用我为自己制定的规则 - 这意味着我通常会尝试使用相同长度的名称。 “顶部”和“较低”的例子会让我感到沮丧(假设我已经不是很邋,,这远非确定);我可能会使用'upper'和'lower'因为它们长度相同;由于长度的不同,'top'和'bottom'也会让我感到沮丧。

#1


3  

Like with all kinds of code style conventions, consistency is what you should strive for.

I would have the development team agree on "standard" pairs of prefixes for common scenarios like "source/destination" or "from/to" and then stick with them for the whole project. As long as every developer is aware of what is meant with a particular prefix in the codebase, it is easier to avoid misunderstandings.

Exceptions to the rule should be clarified in the documentation if the variable is part of a public API, or in comments within the code, if it's visibility is restricted to a single class or method.

与各种代码样式约定一样,一致性是您应该努力的方向。我希望开发团队就“源/目的地”或“从/到”等常见场景的“标准”前缀对达成一致,然后在整个项目中坚持使用它们。只要每个开发人员都知道代码库中特定前缀的含义,就可以更容易地避免误解。如果变量是公共API的一部分,或者在代码中的注释中,如果可见性仅限于单个类或方法,则应在文档中阐明规则的例外情况。

#2


1  

In my databases you'll find many valid-state temporal ("history") tables containing a pair of columns named start_date and end_date. No bonus points for me, then, because I'd rather use the commonly used 'end' than try to come up with an intuitive alternative with the same number of characters as the word 'start'.

在我的数据库中,您将找到许多有效状态的temporal(“history”)表,其中包含一对名为start_date和end_date的列。那么,对我来说没有奖励积分,因为我宁愿使用常用的“结束”而不是尝试用与“开始”这个词相同数量的字符来提出一个直观的替代品。

I tend to prefer these generic terms even when more context-specific terms may be viable e.g. preferring employee_start_date over employee_hire_date (what if their employment started for a reason other than being formally hiring e.g. their company was the subject of an acquisition). That said, I'd prefer person_birth_date over person_start_date :)

我倾向于更喜欢这些通用术语,即使更多特定于上下文的术语可能是可行的,例如更喜欢employee_start_date而不是employee_hire_date(如果他们的工作开始的原因不是正式招聘,例如他们的公司就是收购的主题)。那就是说,我更喜欢person_birth_date而不是person_start_date :)

#3


1  

While one does try to be semantically coherent in obvious cases -- e.g., maximum goes with minimum, and not "lowest" -- in well-structured OO code (which isn't all code, I know) the problem disappears with a good IDE. Classes are short, methods are short, and variables are few in each method. So it doesn't matter what you call the variable pairs so long as they're clear. Your code might not look professional, but real quality is in the code, not in the look of your code.

虽然人们确实试图在明显的情况下在语义上保持一致 - 例如,最大值是最小的,而不是“最低” - 在结构良好的OO代码中(这不是所有代码,我知道)问题随着一个好的消失而消失IDE。类很短,方法很短,每种方法中的变量都很少。因此,只要它们清晰,你所谓的变量对并不重要。您的代码可能看起来不专业,但代码中的真实质量,而不是代码的外观。

The problem further disappears if there is good JavaDoc or whatever the documentation system is, and if have good Class names that go with them. For instance, if you have an instance of a Connection class and it has a method a method called setDestination, that's okay, but if you know that setDestination takes one parameter called destination and it's of the Server class, you're cool... even though you might prefer to call it target, aimHere, placeToSendTheData, or whatever (and the corresponding names, source, comingFromHere, and placeToGetTheDataFrom). Plus the doc system says what the thing is for, and that is priceless.

如果存在良好的JavaDoc或任何文档系统,并且具有与它们一起使用的良好类名,则问题会进一步消失。例如,如果你有一个Connection类的实例,并且它有一个名为setDestination的方法,那没关系,但是如果你知道setDestination接受一个名为destination的参数而且它是Server类的参数,你很酷......即使您可能更喜欢将其称为target,aimHere,placeToSendTheData或其他(以及相应的名称,source,comingFromHere和placeToGetTheDataFrom)。另外,doc系统说的是什么,这是无价之宝。

This next thing might sound stupid and I'm sure I'll get voted down here on *, but unique non-professional sounding variable names have a great advantage: I know that my variables have names like placeWeWantTheDataToGo (and the IDE takes care of typing it), but the "serious" guys who do the JDK would never use such silly names. So I know immediately that the variable is one of mine. Incidentally, when I worked with developers in Spain and Italy, they write code with Spanish variable names (not always, but usually). This causes the same effect: we can quickly see that the Conexion class is ours, but the Connection class is not.

接下来的事情可能听起来很愚蠢,我确信我会在*上投票,但是独特的非专业声音变量名称有很大的优势:我知道我的变量的名称就像placeWeWantTheDataToGo(并且IDE负责输入它,但是那些做JDK的“严肃”的人永远不会使用这些愚蠢的名字。所以我立即知道变量是我的变量之一。顺便说一句,当我与西班牙和意大利的开发人员合作时,他们用西班牙语变量名称编写代码(并非总是,但通常)。这会产生同样的效果:我们可以很快看到Conexion类是我们的,但Connection类不是。

[Also, instead of typing your variable names, assign them a constant String somewhere in your code and use that, so if they called it lower or downer instead of low, you're still okay.]

[另外,不是输入你的变量名,而是在你的代码中的某个位置为它们分配一个常量字符串并使用它,所以如果它们将它调低或降低而不是低,你仍然可以。

#4


0  

Yes, I do try to name complementary sets of variables systematically so that the symmetry is clear. It is not always easy; sometimes, not even possible. Well, not possible using the rules I lay down for myself - which means I usually try to have the names the same length. The 'top' and 'lower' example would drive me batty (assuming I'm not batty already, which is far from certain); I'd probably use 'upper' and 'lower' because those are the same length; 'top' and 'bottom' would frustrate me too because of the difference in length.

是的,我确实试图系统地命名互补的变量集,以便明确对称性。这并不容易;有时,甚至不可能。好吧,不可能使用我为自己制定的规则 - 这意味着我通常会尝试使用相同长度的名称。 “顶部”和“较低”的例子会让我感到沮丧(假设我已经不是很邋,,这远非确定);我可能会使用'upper'和'lower'因为它们长度相同;由于长度的不同,'top'和'bottom'也会让我感到沮丧。