schemedoc / cookbook

New Scheme Cookbook
https://cookbook.scheme.org
30 stars 3 forks source link

Naming conventions #16

Open lassik opened 3 years ago

lassik commented 3 years ago

We should probably decide on common names to use for variables in similar situations. For example:

Haskell has a convention of using x for a list element, and xs for the list. xs is meant as the plural of x: "multiple x's". They also use y and ys.

jcubic commented 3 years ago

We can create CONTRIBUTING.md file that will list the common names. The file name is GitHub convention.

lassik commented 3 years ago

(let loop ...) is probably the most common name to call it. I haven't seen iter in other code bases so far.

soegaard commented 3 years ago

I am not in the target demography, but I really dislike the lst and lis abbreviations. Even just l is better. The loop over x in xs makes much more sense to me.

lassik commented 3 years ago

Agreed on lst, it could be an abbreviation for either last or list. Now we're really getting into the bikeshedding :)

jcubic commented 3 years ago

For last we can just use last, and leave lst for list so we don't use list name.

lassik commented 3 years ago

OK, since nobody else has commented, let's use lst.

I'm fine with first and rest.

Are inner/outer loop/recur reasonable compromises. recur is in Clojure, and I'm told recurse sounds like "cursing again", so recur is decent (no pun intended).

lassik commented 3 years ago

D'oh... Clojure's recur does not allow general recursion, only tail recursion, i.e. iteration.

jcubic commented 3 years ago

I think that loop / recur is not a good idea because the names don't match (if they would use both in nested let's), if they were functions/macros they would do completely different things. An alternative is to always use inner / outer if there are two loops (if there are more they should be extracted into functions). And if there is one loop I think that it can be anything I'm fine with both recur / loop. You can pick the one you think is best.

Also need to add that the code needs to have spaces instead of tabs it may look nice in one editor but be broken on the website or in another editor. One person added a code snippet with tabs and looked like broken indentation but he only use 4 stops in the editor and I think that the browser uses 8, because replacing the tabs with 4 space fixed the indentation issue,

lassik commented 3 years ago

I think that loop / recur is not a good idea because the names don't match (if they would use both in nested let's), if they were functions/macros they would do completely different things.

The idea is that looping and recursion are different things; looping is O(1) tail recursion, general recursion is O(n). But Clojure has made this confusion because its recur is not real recursion, so I agree that it's best to avoid in a cookbook.

An alternative is to always use inner / outer if there are two loops

Works for me. Or name them according to what they do.

(if there are more they should be extracted into functions).

Agreed.

And if there is one loop I think that it can be anything I'm fine with both recur / loop. You can pick the one you think is best.

(let loop ...) is probably the most standard. There's even http://letloop.xyz/ :)

Also need to add that the code needs to have spaces instead of tabs it may look nice in one editor but be broken on the website or in another editor. One person added a code snippet with tabs and looked like broken indentation but he only use 4 stops in the editor and I think that the browser uses 8, because replacing the tabs with 4 space fixed the indentation issue,

Agreed. It makes no sense to use tabs in Lisp code, since the 2-space indent is universally accepted.