• Music

  • 04.Jan.2010
  • The Debut of Morning Society’s Aerial
  • Been holding back on this one for a while…last year, my band Morning Society began work on our debut CD, Aerial (link to iTunes, Amazon). Totally self-recorded, mixed and produced in the basement studio we practice in—with our old drummer, Adam Schmid (now with Drift Effect) doing the mixing honours—and though it was a […]

  • Code

  • 07.Mar.2008
  • un-user friendly.
  • One of the biggest issues with Open Source software is exactly how unpolished it tends to be; too many developers seem to never realize that they aren’t the only ones who want to use it.

  • Whatever

  • 16.Nov.2007
  • I love this.
  • It’s about trust. If you are unwilling to sign your name on something you post to the Internet, then you are just contributing to the increasing white noise that is the Interwebs(tm).

On implementing language constructs.

Some personal factoids about what I use when coding Javascript—and more importantly, what I don’t.

By Tom Trenka

As an experienced Javascript coder–which has always been more of a hobby for me, and (up until the past 2 or so years) not a major source of income–I have had the privilege of being around for both rounds of Javascript/DHTML/Ajax development. I get (and have participated in) quite a few debates regarding what to call language constructs, best practices, the desire by most to “extend the language” due to “inadequacies”, etc. etc.

So I thought I’d share some factoids with you. Bear in mind I’ve been creating highly interactive, Javascript-based applications since 1998 (and coding for the web since 1995).

1. I have never, ever had occasion to use the eval statement.

It’s true. If you’ve paid any attention to the plethora of articles on the web about Javascript, undoubtedly you’ve heard about the “evils” of the eval statement. I’ve yet to rely on it.

2. I have only once found a really good use for the Function() constructor.

Again, it’s true. In Javascript, there are 3 ways of defining a function, with the least common being the actual Function constructor (where you pass it a list of arguments, and the body of the function as a string). Only in one situation–which happened to be a server-side, classic ASP application–was the use of the Function constructor actually warranted, and worked very well for the need that I had.

3. Never, in all the programs I’ve developed in Javascript, have I had a need to reference a base constructor without first knowing what that actual base constructor was.

One very popular pastime these days seems to be using the flexibility of Javascript to create a hierarchical “class” structure, similar to the way Java or C# works–in that you can reference the “class” that something inherits from without having to know, at run time, what class that actually was.

What bullshit.

The reality is that Javascript’s flat runtime structure can–and does–handle every situation you’d need in that regard, particularly when you take into account the ability to define execution scope (i.e. the meaning of “this”) on the fly using function.apply or function.call. Speaking of which…

4. I’ve never had occasion to actually use the .apply method of a function object.

…though I wouldn’t be surprised if I ran across a situation at some point. Just like with point #3 above, the reality is that I’ve always known, at design time, what constructor or function I would need to actually use with a change of execution scope–and therefore already knew the arguments I’d need to pass to it. This clearly points at the .call form of the method. .apply is neat and nice but almost always (for me, anyways) has been unnecessary.

So…why do I bring this up? Because it seems to me like we are going in circles, yet again, with lots of things that have no business being applied to Javascript being applied to Javascript. Every language is ideally suited to be used in a particular way. When you start trying to apply concepts borrowed from another language simply because you don’t understand how the first language really works, you begin to apply abstractions that have no purpose other than to prevent you from trying to actually learn the subtleties of the language in the first place. You’d never try to implement Java constructs in LISP; why do so many want to do it with Javascript? In the end, you pay for it with both performance hits and with over-arching complexity (i.e. higher maintenance cost).

Always–always–we should strive to understand and take advantage of what the paradigm gives us. It’s more practical, it will always perform better, and it keeps things simple.

5 Comments

  1. Chris Barber added these pithy words on January 21, 2007 | Permalink

    Inheritance in Javascript is such a hack. Sure you can write some cute code that allows you to “extend” one object from another, but it’s such a pain in the ass to handle the object oriented goodness offered by C , Java, or C#.

    Is it that hard to hack the correct execution of constructors down the inheritance chain? How about something as simple as checking if some Javascript “class” extends a specific “class”?

  2. tim scarfe added these pithy words on January 21, 2007 | Permalink

    Dude - always a pleasure to read your insights.

    I think ajax is already past its peak personally. Just another few more years to endure…

  3. Tom Trenka added these pithy words on January 21, 2007 | Permalink

    Chris–

    That’s kind of exactly what I’m talking about when I say that there is a trend to try to implement something like that becomes an obstacle to true understanding of the language. The point is that you don’t need the object-oriented goodness that you refer to. In the end (and bear in mind, in many ways some of the compilers, particularly the .NET ones, reduce that structure to) you are dealing with flat structures. Think of it like a good denormalization for a database to handle fast warehousing queries.

    Javascript merely eschews most of that and goes directly to the flat structure.

    What it really means is that you need to adjust how you think about programmatic structures to take full advantage of what Javascript offers. In other words, instead of bending the language to your paradigm, you need to bend yourself.

  4. Chris Barber added these pithy words on January 21, 2007 | Permalink

    There is no spoon.

  5. Tom Trenka added these pithy words on January 23, 2007 | Permalink

    btw Chris: if the prototype chain of your object structure is setup right, you can always use the instanceof operator to find out if something is on a prototype chain:

    if(myObject instanceof someBaseConstructor){ … }

    Works pretty well when you’re not breaking the native JS concepts :)

    (also, just because someone made it a schlocky type of thing (there is no spoon) doesn’t mean it’s not accurate :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*