---
title: While-loops Considered Harmful
description: Why you should never use a while-loop when writing software.
---

## While-loops Considered Harmful

Your phone buzzes with the server alert ring tone. Your product manager taps you on the shoulder: "Um, it looks like we're down." Not a single screen in your web app loads. You attempt to SSH into your server but connecting takes for...ev...er. Finally you connect to the shell and see the CPU pegged at 100%. You tail the access logs, but nothing looks fishy. Site traffic is normal. Frantic and desperate, you reboot the server. The server comes back up ... but immediately the CPU spikes back to 100%.

An hour later you find the problem. That hour seemed like an eternity as you tore your hair, fended off account managers, grepped every line of source you could find, and wondered what it feels like to get fired. And what was the error? It was a while loop that processed some unexpected input and spun forever.

Infinite loop bugs are among the most insidious and - outside of wiping out customer data - most destructive bugs. In web application development, most bugs only affect the particular code path where the buggy code resides. Most of the time you know what paths are most critical and can give those paths an extra thorough review. But a while-loop bug in some unimportant, admin-only screen can disable the entire application for everyone. When the problem strikes, it is very difficult to figure out what exactly is causing the app to freak out and where the buggy code lives.

Fortunately, there is a solution to avoid this class of bug completely: never write a while-loop.

Anywhere in your code where the need arises for a while loop, instead write a "for-loop" with a sensible maximum.

For example, let us say that I am doing a standard chunked-file read in python. In my younger days I would write:

```
f = open('file.mp3', 'r')
while True:
    chunk = f.read(4000)
    if not chunk:
        break
    output.write(chunk)
```

A better pattern is:

```
# pick a number comfortably higher than the logic of the function
# needs to support.
EMERGENCY_BRAKE = 10000000 
for x in xrange(0, EMERGENCY_BRAKE):
    chunk = f.read(4000)
    if not chunk:
        break
    output.write(chunk) 
if (x+1) >= EMERGENCY_BRAKE:
    raise Exception("File reading loop hit the emergency brake")
```

Even better, you can write a helper method:

```
def iter_with_brake(max_times=1000000):
    for x in xrange(0, max_times + 1):
        if x == max_times:
            raise Exception("Iteration exceeded maximum")
        yield x
```

And now you can use this helper any time you would have previously used a while-loop:

```
for x in iter_with_brake():
    chunk = f.read(4000)
    if not chunk:
        break
    output.write(chunk)     
```

In the trivial case of reading a file, the lack of an emergency brake is unlikely to cause a problem. But when you start creating more complicated while-loops, such as for tree traversal, streaming parsers, etc., while-loops become very dangerous. I therefore highly recommend eliminating them entirely.

(Note for the pedantic: this blog post targets web application programmers. There are of course cases in software development where while loops are still fine. If you are writing the main event loop for a sensor on the next Pioneer space probe, your while-loop should happily keep plugging away until the end of the universe).

[![By the way, my company, HubSpot, is hiring software engineers. We're one of the  fastest growing startups in the greater Boston area.Check out our jobs page to  learn more about our culture and the type of work available.](https://no-cache.hubspot.com/cta/default/498/7a67aaa5-0890-464f-b290-21d408a73628.png)](https://cta-redirect.hubspot.com/cta/redirect/498/7a67aaa5-0890-464f-b290-21d408a73628)

posted by Patrick Fitzsimmons on February 20, 2013

topics: [programming](https://pfitz-old.hs-sites.com/topic/programming)

[« back to the blog front page](https://pfitz-old.hs-sites.com/)

#### [Add a comment ＋](https://pfitz-old.hs-sites.com/while-loops-considered-harmful#blog-comment-form)

Name

Email

Website

Comment

 Subscribe to follow up comments for this post

[![patrick-fitzsimmons-headshot](https://pfitz-old.hs-sites.com/hs-fs/assets/img/patfitz-face-circle-vintage.png)](https://pfitz-old.hs-sites.com/)

#### [Patrick Fitzsimmons](https://pfitz-old.hs-sites.com/)

I am a startup guy and software engineer based in Cambridge, MA. Current I am co-founder of [Grokky](http://www.grokky.com). Before that, I spent seven years at HubSpot. I joined HubSpot as the first employee. During the early days I cranked out the first prototypes, during later days I helped scale our software and team to serve ten thousand customers.

[Read more about me and my work](https://pfitz-old.hs-sites.com/about)

[![](https://pfitz-old.hs-sites.com/hs-fs/mail.png)](https://pfitz-old.hs-sites.com/while-loops-considered-harmful#) [![](https://pfitz-old.hs-sites.com/hs-fs/linkedin.png)](http://www.linkedin.com/pub/patrick-fitzsimmons/1/373/7b) [![](https://pfitz-old.hs-sites.com/hs-fs/assets/img/twitter.png)](https://twitter.com/pfitzsimmons) [![](https://pfitz-old.hs-sites.com/hs-fs/assets/img/octocat.png)](https://github.com/pfitzsimmons) [![](https://pfitz-old.hs-sites.com/hs-fs/assets/img/rss.png)](http://www.patfitzsimmons.com/rss.xml)

### Get new blog posts via email

### Posts by topic

- [programming (3)](https://pfitz-old.hs-sites.com/topic/programming)
- [about (1)](https://pfitz-old.hs-sites.com/topic/about)