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.
There are probably analogies to be made here to both https://css-tricks.com/blue-box/ and https://css-tricks.com/interviewing-front-end-engineer-san-francisco/
Well said, Chris.
I spend a ton of my time in WordPress and PHP, but I still find myself having to go back to Google for certain constructs, functions and such that I don’t use on a regular basis.
It gets even worse when I’m jumping between languages. For some reason, PHP doesn’t like it when I try to print something out with the command:
alert $myvar;
Go figure…
I think the best solution is to use something like Sublime Text 3 with good snippets and than you can do all kind of stuff much faster and easier in any language.
Do you know Ralph, when ST3 will be released?
It’s already released http://www.sublimetext.com/3
Actually version 3 is in beta.
It’s available to the public, it’s released.
just to keep the spirit: a loop in haxe is different too :)
for (i in 0…5)
{
trace(i);
}
I always say to all who wants to learn programming, the important thing is spirit of code not a language syntax.
I want to translate this article to persian
In JavaScript, my for loops mostly look like:
Isn’t that a little overkill? And not recommended for performance intensive tasks. Or longer iterations.
Unfortunately this isn’t always supported. It won’t work pre-IE9 without creating a polyfill. Not a deal-breaker though.
It will only work on Arrays, since it is a prototype fn of Array.
Another con is you can’t actually break a forEach loop.
So it certainly isn’t the most flexible for loop to go for in JavaScript.
Haha, glad to know I’m not the only one in the same boat. I am with you on this one Chris. Knowing when to use something is much more valuable than remembering all the syntaxes and not knowing when to use which.
We have just so many brain cells, and we should use it for innovation and not storing stuff which google best handles.
I feel like I’ve been “outed”
Thanks Chris! I’ll bookmark this page for the next time I need one!
you shouldn’t like that either. Use console.log instead.
Very true. This is similar to how I recommend people to pick up new languages: it would take years to fully memorize every command you need to use in every syntax, but as long as you know the capabailities of a launch, and the building blocks of programming in general, it’s not about remembering formulas. It’s about knowing what to use and how to find it.
This is why we have documentation :)
I seem to always forget the “case..when…otherwise”, especially in Javascript. I use it in C#, XSLT and javascript but every time (even now) I ask my friend, Google :-)
Often I land on good old w3schools.com, like: Javascript Switch.
– Oh, and I have been a programmer since 1986 (!)
Nooooo, you should read this: http://www.w3fools.com/
Reading things like this is always a relief to me. I recently have gotten into JQuery and pure JS and sometimes feel like I messed up in my studying when I get to a problem and can’t remember how to structure the syntax. It’s nice to read that even at more advanced levels, sometimes you draw a blank and need to use some external resource.
Ha, I run into a similar thing with comment syntax…
Not that I forget what the syntax is, but when jumping between files I occasionally use the wrong one by accident. Always thrown off for a second when I simply add a comment and suddenly everything breaks… :-)
Coffeescript
Gets even more interesting when you work in a couple of different environments ;)
Yeah, Coffeescript was dropped somehow:
That is why its a good idea to have an IDE that allows you to highlight a block of text and press a key combination to comment it out.
Not remembering the syntax of a for-loop is not a problem at all. The problem is not to know how/when to use it.
Same happens to me as I change from JS to PHP or to other languages. But oddly, I always remember how to write it in Basic!
I completely agree. It’s not about who can remember the right syntax, rather who knows the right tools/constructs for each situation.
Yeah, it’s like playing different instruments. If you can write good music, it’s just syntactical differences. Doubly true within the same family of instruments (strings, brass, woodwind, percussion, etc.). Good post.
Curious to know why Haml and CoffeeScript got to loop 16 times and the rest only 5? ;)
I love this article. I forget random things too and I’m a good developer! I think it is like you said, when you’re out of so many languages that happens sometimes. So refreshing to hear from someone who I consider a complete ninja. I also really love the loop rundown in all these languages – would make a nice poster!
For loop’s sake, here’s the loop in AMPscript:
%%[for @i = 1 to 3 do ]%%
[variables, fields and output]
%%[next @i]%%
What is “Haml”?
HAML is html template parsing engine. In other words, it is a framework to optimize coding in html…
Please google it for More…
Forgetting the exact syntax is something that very often happens to me when writing code.
But I don’t code every day. Sometimes I might not write any code for weeks because I do other things (design in Photoshop, SEO, writing content for my websites etc). I believe Chris is in a similar situation.
Somebody that does not much else than write code every day – the whole day, is highly unlikely that he will forget how to write a for loop in any of the languages he regularly uses.
I realise this isn’t in the ‘spirit’, but it may help those not familiar with JavaScript; In your JS loop you aren’t declaring
i
so it will be in the global scope, which may cause issues with other uses ofi
in your code. It should be:That’s why I have always Dash opened and ready to help when I work!
If you don’t know already Dash, please consult: http://kapeli.com/dash
Nota bene: I have no intrest in common with the Dash developers, I just find it’s a great solution to help in this kind of situation… ;-)
You may want to add these:
Python:
Java:
The example in Ruby is considered bad practice according to the the Ruby Styleguide:
Personally, the only
for
loop i can properly remember are the ones witheach
from ruby and haml because they seem to resemble to natural language the most. In other languages the ones i dislike the most are(i = 0; i < 5; i++)
because they are way to configurable than needed in 99% of their usage. Most of the times i just want to loop over the whole list.Exactly!!
Many a times, I too get confused, or should I say, forget!
Erm… Haml’s for loop is Ruby’s for loop…
Exactly!
A good programmer isn’t someone who remembers syntax perfectly. You can look up any syntax or functions you need in just a few seconds. It’s about being creative and thoughtfull.
I suggest anyone that codes to use something like AutoHotkey – a cool text auto-expander.
Example: I type “fori” and it expands into my most common php for loop. (dang it, yes it auto expanded as I typed it here too haha :)
I setup all my scripts in one text file, that file is in dropbox, so it’s same on any computer I use.
As Chris mentioned, there are many languages, so one may choose to setup; pfor, jfor, sfor… etc.
I believe we are getting to a time where we don’t need remember spelling, nor exact syntax for some stuff – computers are at our aid : )
Confirmed any topic is to discuss, even something as silly as this
That’s what I say in all my seminars. Now I have someone to support me. :P
I love this site. Somehow, I wind up here either if I want it or not. Chris’ articles don’t have any ego in them or any other BS. Gives me confidence as a web developer, that these sort of “easy” things, that I also forget / sometimes have problems with, also exist at all sorts of levels. Thanks.
How quickly you can Google your little code problems counts too. **It’s the spirit! **
From all languages the most easiest loop to remember is ASP classic loop:
Nice article , thank you .
Great post, I’m a front ender who invariably spins cycles fine tuning my errors from language to language. My looping has become somewhat like Spanglish always close to the native syntax, but flavored with a little sabor from JS or SCSS.
Funny… this is just one of those things that comes with coding in many languages. I can’t count the number of times I’ve used the wrong syntax for simple functions (such as this) because I’m typing in one language, and thinking in another. Haha!
for ($i = 1; $i < 5; $i++) {
echo $i;
}
Every once in a while I slip up and use commas instead of semicolons. Really throws me for a loop (no pun intended).
This site has many common algorithms for several popular programming languages. You can view by algorithm, or by language.
http://langref.org/all-languages/program-structure/loops
I cannot remember the various syntaxes either. But my editor can. vim lets me type fl. Out pops a for loop in the correct syntax for the language. cl for a class, wl for a while loop, etc.
I assume many editors can do this, or can with appropriate plug-ins.
-sq
Good point. phpStorm has this capability too, helps a lot for zen coding. Can specify short codes per file type. So, “fl” can be used multiple times and the IDE knows which for loop to generate depending on file type. Clever helpful stuff : )
Amen.
Glad to hear I’m not the only one.