Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for = vs for in normalization #34

Closed
StefanKarpinski opened this issue Aug 14, 2019 · 11 comments
Closed

for = vs for in normalization #34

StefanKarpinski opened this issue Aug 14, 2019 · 11 comments

Comments

@StefanKarpinski
Copy link
Contributor

StefanKarpinski commented Aug 14, 2019

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?

@StefanKarpinski StefanKarpinski changed the title for = vs in normalization for = vs for in normalization Aug 14, 2019
@domluna
Copy link
Owner

domluna commented Aug 14, 2019

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
Copy link
Contributor

Good idea.

@diegozea
Copy link

diegozea commented Sep 3, 2019

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
Copy link
Contributor

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

singularitti added a commit to MineralsCloud/QuantumESPRESSOBase.jl that referenced this issue Sep 10, 2019
about why use `i = 1:nfields(data)` see:
domluna/JuliaFormatter.jl#34
@singularitti
Copy link

singularitti commented Sep 10, 2019

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

@KwatMDPhD
Copy link
Contributor

I'm for always using in.

PallHaraldsson added a commit to PallHaraldsson/YASGuide that referenced this issue Sep 11, 2019
Yes, you're more strict than Julia's style guide, that has this line:

for i = firstindex(a):lastindex(a)
uur guide
and Karpinski's reasoning here: domluna/JuliaFormatter.jl#34

I'm not sure I agree, but it's not good if the formatter generates style incompatible with guide(s). Your guide was updated in 2018 to reflect JuMP's but I at least didn't locate wording on this (yet) there.
@odow
Copy link
Contributor

odow commented Sep 11, 2019

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
Copy link
Contributor

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

@tk3369
Copy link

tk3369 commented Sep 15, 2019

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
Copy link

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

@StefanKarpinski
Copy link
Contributor Author

No, there are currently no formatting options.

@domluna domluna mentioned this issue Oct 5, 2019
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants