A legitimate use for “with” statement

Non-programmers: This is esoteric. You can safely skip this post. 🙂

Now, as a programmer, I hate the with statement. I never use them. Back when I did they were the source of many ridiculous bugs. Saving a few characters in the source while obscuring the meaning of the code. Not a good tradeoff.

However, there is a legitimate use for a with statement, and it’s disturbing to see that it’s been deprecated in JavaScript. I hope this does not mean that there will ever be a version of JavaScript that doesn’t have it.

Here’s when you need it: When you’re implementing a macro language.

  • with (context) {eval (macro)}

It’s the same use-case as the much-maligned eval function.

  • eval used to have a second param, btw, that allowed the caller to provide a context that the code runs in. They took this off, but I wish they hadn’t. (Scroll to the bottom of this Mozilla tech note for the historical note.)
  • We added these to Frontier, and were able to do wonderful stuff, like XML-RPC for example.

Imho, eval and with should only be used by people implementing macro environments.

But they are useful. And both with and eval should exist.

Advertisements

About Dave Winer

Dave Winer, 54, pioneered the development of weblogs, syndication (RSS), podcasting, outlining, and web content management software; former contributing editor at Wired Magazine, research fellow at Harvard Law School, entrepreneur, and investor in web media companies. A native New Yorker, he received a Master's in Computer Science from the University of Wisconsin, a Bachelor's in Mathematics from Tulane University and currently lives in Berkeley, California.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s