2012-10-08

How to pronounce __proto__

[This post is part of a series on the special property __proto__]

Bracketing variable names with double underscores is a tradition in Python that JavaScript has borrowed a few times, most prominently for the property __proto__ [1] (which is currently non-standard, but will become part of ECMAScript 6). For Python, the following pronounciation has been suggested by Ned Batchelder:

An awkward thing about programming in Python: there are lots of double underscores. For example, the standard method names beneath the syntactic sugar have names like __getattr__, constructors are __init__, built-in operators can be overloaded with __add__, and so on. [...]

My problem with the double underscore is that it's hard to say. How do you pronounce __init__? “underscore underscore init underscore underscore”? “under under init under under”? Just plain “init” seems to leave out something important.

I have a solution: double underscore should be pronounced “dunder”. So __init__ is “dunder init dunder”, or just “dunder init”.

Thus, __proto__ is pronounced “dunder proto”.

Why the double underscore?

The double underscores are used to prevent name clashes between system-defined names and user-defined names. But that measure is problematic in JavaScript, because objects are sometimes used as maps from strings to values:
    var record = {};
    record["first"]     = "Jane"; // OK
    record["last"]      = "Doe";  // OK
    record["__proto__"] = "Type"; // Not OK, sets prototype
The last line changes the object’s prototype – meta-data is influenced by data. You can protect yourself against this kind of problem [2], but it would be better to not rely on names for special properties that might appear as strings in user data. Therefore, special properties will be implemented via name objects [3] in ECMAScript.next. Those objects don’t ever clash with each other and are disjoint from strings. Then JavaScript programmers can freely choose names for their properties, without having to worry about clashing with current and future special properties.

Reactions

Reactions to this blog post were mixed. Some people hate “dunder”. Alternatives? How about:
  • “du” (pronounced “dee yoo”), suggested by Mark McDonnell
  • “dubscore”
  • “scorescore”
Others joked about it (Jens Roland mentioned “__mifflin__”; “__head__“ would work equally well). I agree that it sounds a bit corny, but given the precedent in the Python community, it is likely to stick. Brendan Eich seems OK with “dunder”, too (as in “he uses it”).

References

  1. JavaScript: __proto__
  2. The pitfalls of using objects as maps in JavaScript
  3. Private data for objects in JavaScript

No comments: