domluna / JuliaFormatter.jl

An opinionated code formatter for Julia. Plot twist - the opinion is your own.
https://domluna.github.io/JuliaFormatter.jl/dev/
MIT License
579 stars 69 forks source link

for = vs for in normalization #34

Closed StefanKarpinski closed 5 years ago

StefanKarpinski commented 5 years ago

The rule of thumb that I use for for loops is that if the thing you're iterating over is a literal range then you use = whereas if it's anything else (e.g. an object) then I use in. So, for example, these would be "correct":

for i = 1:n
    println(i)
end

for i in itr
    println(i)
end

These, on the other hand would be "incorrect":

for i in 1:n
    println(i)
end

for i = itr
    println(i)
end

Part of the reasoning is that for i = 1:n looks like it assigns i to 1, then 2, then ... n — at least in common mathematical / pseudocode notation, whereas for i = itr looks like it assigns i = itr which is not what it does. So one could argue "why use/allow for i = 1:n at all? Why not just use in uniformly and write for i in 1:n and for i in itr? Some people surely would prefer this. However, this superficial syntax difference can help catch a common error, which is writing for i = n when you meant to write for i = 1:n. If we require in there then the programmer will notice that for i in n is not what they meant to write and catch the bug.

So I would propose normalizing for = versus for in based on this rule. Thoughts?

domluna commented 5 years ago

I'm in favour of this.

Throwback https://github.com/julia-vscode/DocumentFormat.jl/pull/26/files#diff-8f8b649677f922163282f3d951be629aR89-R104.

Additionally this is a best practice for the prevalent style guides:

KwatMDPhD commented 5 years ago

Good idea.

diegozea commented 5 years ago

why use/allow for i = 1:n at all? Why not just use in uniformly and write for i in 1:n and for i in itr? Some people surely would prefer this.

I'm "some people" ;) I've found for = difficult to read, so I always use for in. I would prefer for ∈ instead of for = if a difference should be made.

KwatMDPhD commented 5 years ago

It's more difficult to type ∈. Also, ∈ is rarely found in other languages.

singularitti commented 5 years ago

I am "some people" also. In fact, some nonofficial guide has suggested not to use for ... = ....

KwatMDPhD commented 5 years ago

I'm for always using in.

odow commented 5 years ago

Going to chime in on this dead issue that

Additionally this is a best practice for the prevalent style guides:

Is incorrect. JuMP's rule is to always prefer in over =.

Part of the reasoning is that for i = 1:n looks like it assigns i to 1, then 2, then ... n — at least in common mathematical / pseudocode notation, whereas for i = itr looks like it assigns i = itr which is not what it does.

You could (and I will) argume that i = 1:n looks like it assigns i to the iterable 1:n.

KwatMDPhD commented 5 years ago

https://github.com/jrevels/YASGuide also recommends using in over =. @domluna , is it difficult to update the code to use in instead of =?

tk3369 commented 5 years ago

I also opt for in over = in all circumstances although my visual cortex interprets them the same anyways. I love because it's cool but often it's just too much to type and so in wins.

Roger-luo commented 5 years ago

Is there a way to choose different syntax guide? like this one. I also opt for in over = in all circumstances...

StefanKarpinski commented 5 years ago

No, there are currently no formatting options.