The Syntax vs The Spirit

Chris Coyier //

Sometimes I literally can't remember how to write a for loop. Not like funny ha-ha I can't remember how to spell "psychology" but really I can figure it out in a few seconds. Like, go to Google, type in "for loop PHP", click on a promising result, copy and paste, then alter it.

I'm mostly a front-end guy, but I'm essentially a professional programmer. Isn't that ridiculous and embarrassing? Meh.

I'm in and out of a lot of different languages in any given day. A for loop is different in all of them. Here they are:

for i in 1..5
  puts i
end
for (i = 0; i < 5; i++) {
  console.log(i);
}
for ($i = 1; $i < 5; $i++) {
  echo $i;
}
@for $i from 1 through 5 {
  .n-#{$i} { color: red; }
}
- (1..16).each do |i|
  %div #{i}
for i in [1...16]
  console.log(i)

They are all different. None are very difficult to understand, but the for loop is probably the simplest construct you'll need to write in a language, so you can imagine it gets worse.

So what.

All those languages have perfectly good reasons for why they do that the way they do. It is a minor inconvenience to maneuver the correct characters into place to get them to do the job.

What matters is:

  • I know what a for loop is
  • I know when to reach for a for loop
  • I know the difference between a for loop and other loops
  • I know the potential dangers of a for loop

Nevermind the syntax, it's the spirit that counts.