Clojure 的命名约定是什么?

发布于 2024-11-24 07:51:45 字数 110 浏览 0 评论 0原文

任何人都可以解释或指出我在哪里可以找到 clojure 的命名约定:

  1. 文件名
  2. 函数(据我所知,函数名称只是破折号分隔的值)
  3. 变量

Can anyone explain or point me to where I can find clojure's naming conventions for:

  1. File names
  2. Functions (From what I understand, function names are simply dash separated values)
  3. Variables

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

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

发布评论

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

评论(5

泪痕残 2024-12-01 07:51:45

您可能需要查看开发人员的 Clojure 库编码标准维基 - 这可能是我见过的最全面的列表。

更新:上面的链接似乎已失效,请考虑:https://clojure .org/dev/contrib_howto#_coding_guidelines

针对您的具体观点:

  1. 文件名小写,并存储在目录结构中以匹配命名空间,并以 .clj 结尾,例如“my/special/namespace.clj
  2. 函数是用破折号分隔的小写单词,理想情况下是经过描述性选择的,以便您的代码清晰且自记录。不要害怕在不同的命名空间中重复使用好的函数名称(即命名空间的用途是什么!)。
  3. 变量(我假设你指的是参数、let 绑定变量等)通常也是用破折号分隔的小写单词,因为代码就是数据,所以我认为这是合适的。函数和数据具有相同的命名约定:-)

You might want to look at the Clojure library coding standards on the developer Wiki - this is probably the most comprehensive list that I've seen.

Update: link above seems to be dead, consider instead: https://clojure.org/dev/contrib_howto#_coding_guidelines

To your specific points:

  1. File names are lowercase, and stored in a directory structure to match the namespace, and end in .clj e.g. "my/special/namespace.clj
  2. Functions are dash-separated-lowercase-words, ideally descriptively chosen so that your code is clear and self-documenting. Don't be afraid to re-use good function names in different namespaces (that is what namespaces are for!).
  3. Variables (by which I assume you mean parameters, let-bound variables etc.) are also usually dash-separated-lowercase-words. Since code-is-data, I think it is appropriate that functions and data have the same naming convention :-)
勿忘初心 2024-12-01 07:51:45

您可能需要查看这份非官方风格指南

You might want to take a look at this non official style guide.

情绪操控生活 2024-12-01 07:51:45

有一些有趣的Stuart Sierra 编写的命名指南这表明:

  • 纯函数应该是描述返回值的名词(age 而不是 calculate-age
  • 副作用函数应该是描述操作的动词(create- 用于构造,get- 用于检索),保留对可变引用的 bang swap! 更改。
  • 也可以是名词的动词应区分为动词短语(send-message 而不是 message
  • 强制转换应命名不带箭头前缀的输出类型(connection< /code> 而不是 ->connection),除非输入类型必须是显式的 (input-type->output-type)
  • 命名空间别名可以节省重复(products/price 而不是 products/product-price)并防止 let 绑定
  • 函数中的本地冲突,返回函数应具有 -fn 后缀

There are some interesting guidelines on naming written by Stuart Sierra which suggest that:

  • pure functions should be nouns describing the return value (age instead of calculate-age)
  • side-effecting functions should be verbs describing the action (create- for constructing and get- for retrieving), reserving the bang swap! changes to mutable references.
  • verbs that can also be nouns should be distinguished as verb phrases (send-message instead of message)
  • coercions should name the output type without an arrow prefix (connection instead of ->connection) except when the input type must be explicit (input-type->output-type)
  • namespace aliases can save on repetition (products/price instead of products/product-price) and prevent local clashes in let bindings
  • functions returning functions should have the -fn suffix
小苏打饼 2024-12-01 07:51:45

在评论中记录了一组有趣的命名约定
陶恩索在他的
Encore 库

他建议使用 ! 来表示副作用,使用 ? 来表示布尔值,
$ 用于昂贵的操作,_ 作为可取消的操作,
* 用于宏;加上一些其他组合。

There is an interesting set of naming conventions documented in a comment by
Taoensso in his
Encore library.

He proposes names using ! for side-effects, ? for booleans,
$ for expensive operations, _ as dereffable,
* for macros; plus a few other combos.

柒夜笙歌凉 2024-12-01 07:51:45

即使您没有明确要求,我也会解释我所看到的协议命名约定。

通常,名称以大写“I”开头,然后其余部分为驼峰式大小写,其中每个单词的第一个字母大写,其余部分小写。例如,我想为火箭飞船定义一个协议,我会使用名称 IRocketShip

我还看到使用“A”而不是“I”,可能代表“抽象”一词。

Even though you didn't ask for it explicitly, I'll explain what I've seen for protocol naming conventions.

Typically, the name starts with an uppercase "I" and then the rest is camel case, where the first letter of each word is capitalized, and the rest is lower case. For example, I want to define a protocol for rocket ships, I'd use the name IRocketShip

I've also seen 'A' instead of 'I' used, probably to represent the word 'abstract'.

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