选择一个合适的名字

发布于 2024-10-19 19:17:14 字数 714 浏览 4 评论 0原文

我了解尽可能选择一个能够反映您的意图的适当名称是多么重要,以及它如何影响您的代码质量。

例如,下面是 java.math.BigDecimal 中的一些方法,我认为它们有一个有趣的参数名称:

BigDecimal add(BigDecimal augend);
BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode);
BigDecimal multiply(BigDecimal multiplicand);
BigDecimal subtract(BigDecimal subtrahend);

这些方法总是让我意识到了解一些特定的符号和术语对于改进代码有很大帮助,因此我认为它会如果我能更了解他们就好了。

最近我发现一个非常好的网站,它为您提供了常用术语的列表数学,我想知道是否有任何类似的资源(无论是在网络、物理还是与编程相关的任何其他领域)可以提高您作为程序员的词汇量?

请记住,我在这里的意思不是编码标准,例如 Zend 命名约定

I understand how important it is to choose an appropriate name that reflects your intentions whenever it is possible, and how it can impact your code quality.

For example, below are some methods in java.math.BigDecimal that I consider having an interesting parameter names :

BigDecimal add(BigDecimal augend);
BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode);
BigDecimal multiply(BigDecimal multiplicand);
BigDecimal subtract(BigDecimal subtrahend);

Such methods are always made me realize that knowing some specific notations and terms is a great help for improving your code, hence I think it would be good if I know them better.

Recently I've found a really good website that gives you a list of commonly used terms in mathematics, and I wonder is there any similar resources (whether in networking, physics, or any other fields related to programming) that can improve your vocabulary as a programmer?

Please keep in mind that what I meant here is not a coding standard, such as Zend Naming Conventions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是我的挚爱i 2024-10-26 19:17:14

我发现 维基百科 是一个很好的资源。

例如,当我需要一个变量来存储 ampm 时,我之前将其称为 ampm

不久前,我研究了它,现在将其命名为meridiem.

I find Wikipedia to be a good resource for this.

For example, when I needed a variable to store am or pm, I previously called it ampm.

A short while back, I looked into it and now name it meridiem.

揪着可爱 2024-10-26 19:17:14

我想说,多个领域的术语表可能有助于找到好的术语,但通常这也是您的团队为自己设定的经验和惯例的问题。

命名的一个问题是其他人阅读名称可能不知道如何解释它,因此您至少应该添加一条注释来解释类/字段/方法的含义。

除此之外,我建议根据方法的用途或使用的算法来命名,例如 sortquicksort。如果您从特定领域实现算法,您应该熟悉该领域或至少熟悉该算法,从而减少命名问题。 (例如:如果我实现一种方法来计算两个字符串的 levenshtein 距离,我会将方法类似于 int calcLevenshtein(String arg1, String arg2) )。

I'd say that there are glossaries for multiple fields which might help in finding good terms but generally it's also a matter of experience and conventions that your team sets for itself.

A problem with naming is that others reading a name might not know how to interpret it and thus you should at least add a comment explaing the meaning of the class/field/method.

Other than that, I'd suggest to name the methods by their purpose or the algorithm they use, e.g. sort or quicksort. If you implement an algorithm from a specific area, you should be familiar with that area or at least that algorithm, result in less problems with naming. (Example: if I implement a method to calculate the levenshtein distance of two strings I'd name the method something like int calcLevenshtein(String arg1, String arg2) ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文