by _yosefk · 11y ago · 0 comments
for i in range(10):
...with Go's:
for i:=0; i<10; i++ { }
An experienced programmer might have an opinion about these syntaxes but he can certainly handle both effortlessly. However, when I first saw for loops using syntax similar to Go's, I could not, for a long while, remember exactly what I have to type, and of course the compiler errors along the lines of "expecting some gibberish after some other gibberish" didn't help at all - and not knowing that they aren't supposed to help hurt, because I stared at them, trying to decipher them, instead of guessing that I got it wrong and looking up the loop syntax.
Now if you think about it, Python's incantation is very close to English ("for i in range 10" instead of "for every in the range of numbers up to 10" - you omit some words and you add 3 punctuation characters but it's close.) Go's incantation, if you try to read it, makes little sense ("for i assigned to equal 0; i less than 10; i plus plus [or i equals i plus 1]".) You mention i 3 times which is weird, because beginners actually intuitively expect DRY code (hence Java's AssWiper bill = new AssWiper [and I'm quoting from a toilet door in a CS lab] felt very weird to me for a long while.) You have those semicolons in the middle, and you're supposed to remember the order of these 3 parts - init, compare, increment.
I mean, it wasn't a huge trauma but I felt dumb for quite some hours until I got it.
(Note that i:=0 and then a while loop incrementing i are not nearly as arbitrary because you compose it out of sensible primitives. A Cish for loop on the other hand feels like one big blob of syntax which came out of nowhere. Of course once you memorize it, it's vastly more readable than a while loop because you pattern-match it and you translate it to "for every i from 0 to 10", and you assume that nobody fiddles with i midway through the loop; whereas if you see a while loop, you need to scan all of it because you will suspect that i is being fiddled with in some non-trivial way.)
Also - omitting some parts of the for loop results in syntax which is fairly crazy actually, if you read it out loud. Like for ; i<10; i++ - what's a semicolon doing after the word for?!
for idx, elem := range someSlice {
}
Sometimes with idx replaced by _.(Or at least, that's the modal for loop if you're doing it right.)
I wish I could make
for x := range someSlice {
default to iterating on values rather than indexes... I do have rather a lot of for _, val := range someSlice {
but, meh.Personally I'd unhesitatingly introduce that version and pull out the three-element version once someone needs it. People should learn value-based iteration as the default. I have to admit when I'm doing an interview and I see someone pull out the 3-element for loop in a language that does value-based iteration, I mentally dock a point for choosing a dangerous construct when an easier, safer construct is readily available.
(Before someone jumps up and yells, I readily admit... no, advocate for... Python's
for idx, value in enumerate(something):
when available. I really don't like the 3-element for loop.)All I'm saying is, it's a lot of syntax to inhale, there are plenty of kinds of for loop. I certainly don't mind, and I'd be happy enough with "the modal for loop" being DRY and compact. I just recall that my former self had trouble memorizing grammar cases. Python's for loop always does for value in sequence, so there's less syntax.
func it(n int) []struct{} {
return make([]struct{}, n)
}
// ex. 1:
for range it(10) {
fmt.Println(".")
}
// ex. 2:
for i := range it(10) {
fmt.Println(i)
}