W3C home > Mailing lists > Public > public-script-coord@w3.org > January to March 2012

Re: Numeric constants vs enumerated strings

From: Tobie Langel <tobie@fb.com>
Date: Wed, 15 Feb 2012 14:38:29 +0000
To: Anne van Kesteren <annevk@opera.com>, "public-script-coord@w3.org" <public-script-coord@w3.org>, Harald Alvestrand <harald@alvestrand.no>
CC: "public-webrtc@w3.org" <public-webrtc@w3.org>, "public-webapps@w3.org" <public-webapps@w3.org>, "public-html@w3.org" <public-html@w3.org>
Message-ID: <CB617B9F.5826B%tobie@fb.com>

>On Wed, 15 Feb 2012 14:57:00 +0100, Harald Alvestrand
><harald@alvestrand.no> wrote:
>> *This is a call for help from the WEBRTC working group.
>> We are defining a new API
>> (http://dev.w3.org/2011/webrtc/editor/webrtc.html) <about:blank>which
>> has a number of cases where it needs to use arguments, or expose state
>> variables, that can take only a limited set of values.
>
>Unless there are strong ties to certain legacy APIs I would suggest using
> 
>strings. They are easier for developers to author, easier for developers
>to maintain, easier in the future to extend, and have practically no
>drawbacks.

I second that. Authors barely ever used the defined constants (for good
reason, some implementations were missing them) preferring to use integers
directly.

Instead of seeing the verbose but descriptive:

    if (node.nodeType == Node.ELEMENT_NODE) { ... }

one came across the following more often than not:

    if (node.nodeType == 1) { ... }

to which: 

    if (node.nodeType ==  "element") { ... }

should be preferred.

Constants would only have practical benefits over strings if they were
defined in the global scope, as in:




    if (node.nodeType == NODE_ELEMENT_NODE) { ... }

as typos would be caught early on (undeclared variables throw
ReferenceErrors).

--tobie
Received on Wednesday, 15 February 2012 14:39:35 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:05 UTC