Do While Once in a While

The other day, a programmer I follow on Twitter was pleased because he had found a good use for a do/while loop.

When a programmer gets excited by a loop construct, you know something is wrong. The truth is that do/while is underutilized, not just in JavaScript but in all C-based languages.

The rule of thumb is that you should use always use a do/while loop when the body of the loop must execute at least once. Use a while loop when the body of the loop may never execute.

Maybe it’s true that a while loop is indicated more often than a do/while loop, but I’ve often seen programmers do a little jury-rigging to patch up the while loop to do their bidding when a do/while would have meant less typing and have made more sense.

Why? Well, programmers get into habits. If you get to know while very well, you may be shy about using do/while.

But I think there’s some clumsiness inherent in do/while that also plays a part in its unpopularity. It’s a result of JavaScript getting its syntax genes from C rather than the Wirth family of languages. Pascal and the others (Modula-2 and Oberon) used repeat/until

do {
  a = a + 1;
} while (a!==10);

  a := a + 1
until a = 10;

I think repeat/until is superior to do/while for these reasons:

  • In JavaScript, the reuse of the identifier “while” is awkward. When you see it, you tend to expect the loop body to come after it. See how it just sits there AFTER the close brace? Like a teenage boy avoiding a girl at a middle school dance.
  • do/while‘s ending condition is the same as while‘s starting condition. Pascal’s until condition is logically negated, which I think is more natural at the end of a loop. Do something over and over UNTIL something is true, rather than do something over and over WHILE something is true. REPEAT UNTIL i hits 5, rather than DO WHILE i is less than 5. A possible solution would be to allow do/until as well as do/while. This would allow for backward compatibility. Someone remind me I said that the next time I invent my own computer language.

In short, the do/while construct just doesn’t sit quite right in C, encouraging the overuse of the while loop, which sits just fine. In any case, repeat/until seems to get more use in Pascal than do/while gets in C.

Don’t forget do/while is there, waiting for you to be excited to use it. It’s the right choice when you must execute the body at least once.


The Object Slinger

The more JavaScript code I write, the less secure I am in thinking I have a handle on the language. Just when I settle on a way of doing something, a startlingly better way occurs to me.

JavaScript as Sexy Forest Nymph

JavaScript is a language that seems to have grown up without the sun shining on it. While Java is the bleach blonde fake-boob lifeguard chick surrounded by gawkers, JavaScript is a hauntingly mysterious gal in the forest that you can nearly catch a glimpse of as you scramble through blackberries and brambles.

Everyone seems to know the proper way to program in Java. It’s the language you learn in college. It’s formal. It’s strict. It’s how your mother would want you to code.

No one really seems to be sure of the “right way” to code in JavaScript. Or, rather, there are a lot of people who are sue how to program in JavaScript, but they don’t all agree with each other. It’s still the wild west.

When people start coding in JavaScript, many complain that it’s not like Java. Looking for Java in JavaScript is looking for heartbreak. Instead, treat JavaScript as a magical version of C that allows you to stuff objects and functions into variables. Sometimes you don’t even have to stuff them into variables–-you can create them on the fly and pass them around.

Maybe if C had been more like JavaScript we never would have moved to C++.


I remember that when I programmed in C, I’d often get into a situation where I had a function that had been returning one value and now I wanted it to return two values. I felt painted into a corner.

Sometimes I’d hack it so both values got returned together. If I knew x was in the range 0-15, and y was in the range 0-15, I’d return x*16+y and the calling function would do the math to break the values out into two pieces. In more difficult situations, I’d have to make a struct and pass a pointer to that around. Doesn’t sound like a big deal, really, but it broke the flow and made everything ugly.

In C++ or Java, you’re typically thinking about classes the whole way through. It’s a mindset where you’re not mad at having to put together a struct to hold structured data, you’re resigned to thinking in classes. OK, stop staring at the nymph and pay attention. I’m going to show you some code.

It’s Different

JavaScript is different. I don’t have to hack around like I did in C, and I don’t have to think in classes. I just return an object, and I can sling that object into existence right at the last minute, right in the return statement.

Here’s a ridiculous function that always returns an object that holds the numbers 10 and 12. A better example (and a rather common one) would be a library that does complex math. For illustration, this’ll do.

function giveMeX10Y12() {
    return {x:10,y:12};

The caller can easily pull the pieces apart:

console.log (obj.x,obj.y);

To me, this is what JavaScript programming is all about. It’s not about classes-–it’s about objects. It’s lightweight and comfortable, once you unlearn other languages and gain JavaScript chops.

Spaghetti Western

When you start programming JavaScript, you’re going to write some big plates of spaghetti. You’ll know you’re doing it badly. But learn to think in JavaScript and your code will be clean and easy to debug.