What are Clojure's Naming Conventions?

Deadly 提交于 2019-12-18 11:40:25

问题


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

回答1:


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.

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 :-)



回答2:


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




回答3:


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



回答4:


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.




回答5:


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'.



来源:https://stackoverflow.com/questions/6709131/what-are-clojures-naming-conventions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!