• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"JavaScript that looks like JavaScript" or, another style question from Mike

 
whippersnapper
Posts: 1843
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another JavaScript style question, in part prompted by Bear Bibeault's clarity vs. cleverness meter.

Some background: I sometimes find myself exhorting some of my colleagues to "write JavaScript that looks like JavaScript" (as opposed JavaScript that looks some other language X). In other words, let's go ahead and take advantage of some of JavaScript's distinctive features. (And as a team, let's grow and share our knowledge.) I sometimes face Crockfordian protests of "excessive cleverness" (or just "the old, familiar way is clearer to everyone, not just JavaScript gurus, so let's continue with the style we know and love.")

(1)
So somebody had a switch statement going that had about a dozen cases but the point was just to map one string value to another. So I proposed replacing it with a plain-old JavaScript object, serving as a hash/map/dictionary thing.



So everyone's good with that. But then I start to get a little crazy. In this particular case "lookup" is used exactly once. So to my mind, why bother giving it a name. Let's do it like this:



And in fact, "input" isn't entirely needed either and the code could be further tightened up. (Rest assured, there is appropriate null-checking, input-scrubbing, etc. in the code, so my proposed changes don't introduce any faults here.)

So, is this now JavaScript that looks like JavaScript (and an idiom every reasonably good JavaScript programmer should be comfortable with) or is it the bad kind of cleverness?

(2)
That was a relatively concrete example, and one that straightfowardly maps a string to a string. What about mapping a string to behavior? (Mit bonus IIFE!)



Now, is this JavaScript that looks like JavaScript? Bad kind of clever? In general, good or bad idea?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, both are fine.

The key, to me, is exactly what your title suggests: the code looks fine to a JavaScript developer. You are using basic concepts of the language, and taking advantage of its functional nature.

What would not be fine, in my opinion, is writing JavaScript so that it looks fine to a Java developer.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How long are those functions that you define in the second example? If they are short and their intent is fairly obvious, then it might be fine. However, if they are more than a few lines of code long and/or are not intention-revealing, I would probably extract them to named functions and use the names in the dictionary instead. That way it's easier to see that what you have is really a dispatch table.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:That way it's easier to see that what you have is really a dispatch table.


I'd agree with that.

Generally I try to avoid needless named items, but there are times where it helps. That whole clarity thing.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The string-to-string mapping I’ve written numerous times, but the string-to-function mapping (dispatch table) example was just speculative, so there are no actual functions of any length to debate at this point. But I get your points, Junilu and Bear, and I frequently have internal debates about inlining something versus naming it, taking into account revealing intention and general clarity.

Interestingly, it turns out that the Wikipedia page for “dispatch table” has a “Javascript” example very similar to mine, only more generalized to handle a default scenario.

http://en.wikipedia.org/wiki/Dispatch_table

I’ve asked a handful of such JavaScript style questions in this forum, and the response has generally been no, my code examples have not been outlandish or clever-bad.

Is anyone willing to post some sample JavaScript code (in this thread or starting another) that *does* cross over into the bad kind of clever territory?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, I can't quickly think of one that's demonstrable in a couple of lines of code -- the most egregious examples I've seen are systems that build abstraction on top of abstraction on top of abstraction so that even though a single line of code may look simple, it actually depends on an ecosystem that's completely incomprehensible to anyone who didn't write it (and in the case I am thinking about to most of the original authors as well).

Abstraction is good. But too much abstraction is far worse than too little, in my opinion.
 
reply
    Bookmark Topic Watch Topic
  • New Topic