-
Notifications
You must be signed in to change notification settings - Fork 41
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
Allow cuddle assignments if used first in sub-blocks #50
Comments
@dionysius Thank you for this issue! I think it's always worth discussing things like this and to get as much input as possible. After all, this linter is about opinions regarding code style and thus should be discussed! Regarding your first example I think one idea could be to configure "maximum first block statements" for cuddled variable usages with your example requiring the depth to be 2 while the default value is 1. With 3 this would be allowed: r := map[string]string{}
for k, v := range m {
if true {
if false {
r[k] = v
}
}
} Regarding the last example I think it kind of proves the case that the usage is so far away that it would be easier to not have this as a part of this rule. However, if/when I were to implement #24 my idea is to make that recursive which means that you could cuddle one variable with a block as long as it's used somewhere in said block. I'll leave this issue open for further discussion and thoughts and maybe a first-block-depth-option would be a good combination with cuddle for entire blocks if you just want this part of it. Of course PRs are welcome! |
Thank you @bombsimon for the insights! I couldn't describe it better. For me, this definitely sounds going into the right direction and am happy to hear what others think of that. |
TL;DR: In my opinion, Sometimes we need nested loops to fill a variable: n := 0
for i := 0; i < 3; i++ { // for statements should only be cuddled with assignments used in the iteration
for j := 0; j < 4; j++ {
n += i*j
}
}
params := []myParams{}
for _, f := range feeds { // for statements should only be cuddled with assignments used in the iteration
for _, v := range f.vitamins {
if v.isValid() {
params = append(params, myParams{f.name, v.id})
}
}
} Disconnecting n := computeN()
params := fillWithVitamins(feeds) In my opinion a rule based on first-block-depth-option and/or loop(s) size shifts the problem within the configuration. A good linter should not (in my opinion) require a customized configuration. So I think about how to keep So I would say the rule " And if the loop is too large, or too deep, |
if
s or switch
for "ranges should only be cuddled with assignments used in the iteration"
@olibre I would go even further and say that any assignments before a block of code should be allowed without a newline if it is used then somewhere (anywhere) inside the block. For for loops this is very common, but also other blocks have variables you initialize first and then manipulate inside those blocks. |
Just a suggestion, I can also see it as intentional, since it would allow too complicated blocks maybe. I stumble a lot into that in my code, so I have some examples and can tackle down if and how we can improve this. I'm also very critic to myself that this suggestion is wrong and I have to overthink my code :)
what this rule wants:
I often have a
range
with a little ifif !thisorthat()
Or a switch-case:
The text was updated successfully, but these errors were encountered: