|
|
|
|
|
by Someone
4401 days ago
|
|
"I use `var` everywhere and I'm pretty sure I should be using `let` a lot more. I haven't worked with Swift enough to have a strong intuition about when to use either." I think you should type let everywhere, except when you cannot get away with it. So, it is var when declaring a loop variable. Elsewhere you almost always can introduce a new name. So, instead of X += 1
do let newX = x + 1
Those rules are too strict, but not much so, and you will learn the difference faster if you overcompensate.If you find yourself creating a newerX, a newestX, etc. you may be better of with a var, but chances also are that you are better of writing your code differently (for example can you iterate over a constant array of values instead of doing x+=1 a few times? If your function needs to create 10+ Local variables, can you factor out a function?) |
|