60,000 Lines of Javascript

By Harry Fuecks

Koranteng Ofosu-Amaah makes some fascinating and insightful remarks on Applications vs. W3C DOM (this goes back to July, 2003 BTW – skip past the ad). Stepping out of the discussion of rich web applications for a moment, what struck a cord was point 8. “Javascript is too Flexible”;

In the 60,000 lines of Javascript I’ve been dealing with, I’ve sometimes found up to 7 ways to solve the same issue in the code; some approaches being incredibly powerful, ‘smart’ and idiosyncratic but when it come down to it incomprehensible in the long run.

Part of this problem is specific to Javascript – try searching Google for Javascript and inheritance and compare the advice you get – but think there’s a general issue here which applies to all the dynamic languages. Koranteng calls for an effort by developers to codify best practices.

Over the last year have seen more and more people talking about best practices in PHP which is a very good thing, but at the same time, still seeing new developers making “mistakes” PHP allowed them to make. At the other end of the scale there’s developers who know a language well and can’t resist adding ‘smart’ features. That needs to be a golden rule of what not to do with dynamic languages – don’t extend the language (at least not using the language itself).

In PHP that probably means things like “don’t define a class String”. In Javscript perhaps it’s adding a property “superclass” to make inheritance possible. Meanwhile Python, which has a great design by default, draws things like this.

Trying to grasp the designers intent should be the first step of getting “serious” with a dynamic language. In the absence of a clear statement of intent (and PHP has many grey areas) it’s time to open the debate up with other developers.

Next on the list of best practices should probably be “you must write unit tests”. Perl, PHP, Python and Javascript (that I’ve used) have excellent unit test frameworks available.

What else?

The Platform Divide

Back to Koranteng;

Not to get into language wars but I’ll offer a bounty for whoever solves this issue. I want to be spoilt as much as a J2EE or .Net programmer is spoilt.

There’s a challenge ;) Sadly the big software houses are still missing this boat. The attitude is summarized by Thin To My Chagrin (Nov 2003);

But the best advice I can give you is simply use the right tool for the right job. The script languages are fabulous tools for their intended purpose. So are C# and C++. But they really are quite awful at doing each other’s jobs!

Personally think that’s a self fulfilling prophecy. It’s because the big vendors hold this kind of view that the state of dynamic languages is improving only incrementally, through Open Source (which may be a good thing). Microsoft does seem to be hedging it’s bets though, with the odd smart hire

The counter, in favor of dynamic languages, begins with Strong Typing vs. Strong Testing and ends in results like wxPython, Gmail and A9.

In contrast to Thin To My Chagrin, Koranteng makes further excellent commentary in On rich web applications, AlphaBlox and Oddpost (with follow up here – uptime varies).

  • *cough*classkit*cough*


  • Well the way I see it is that you can “enforce” some best practices in the language. for example Java has the keyword ‘interface’ In PHP you will write in the manual what you want people your framework to do.

    Now to me I don’t consider any language better than the other, it depends on the domain.

    use the right tool for the right job

    PHP was perfectly designed for a certain purpose( i.e. building web apps ). Sometimes it’s bad to enforce some features in the language.

    I wrote about this once in my blog, you can read the post here

  • Version0-00e

    maybe ol’ tony needs to look at this? ;) :p

    seriously though i agree that best practices specially in todays industry, need to be followed.

    unfortuantely there are some amatuers out there and in a way it’s a PHP thing as the language is so damn easy to pick up :eek2:

    what the language lacks in complexity, it makes up in stupidity from some of it’s users :D

  • Ian Bicking

    I think there’s a problem specifically with Javascript best practices. They simply don’t exist. There’s no basic standards about naming, layout, objects, or abstraction. Coming from the Python world, it’s certainly possible to create uniform, easy-to-work-with code in a dynamic language. With Javascript, I think there’s no cultural basis, there’s not even a culture of craftsmanship, just lots of hacks. It’s not hard to see why — there’s lots of valid reasons, like Javascript OO is funky and widely misunderstood; people are used to cross-browser incompatibilities; and people come into the language through recipes instead of instruction. Recipes are a poor substitute for software. I do wish it would change.

  • matthewh

    What javascript unit testing frameworks are available?

  • 60K lines of Javasript must make a seriously rich interface. Hope its not all in the same app, cause I’d hate to be looking after that beasty because its probably backed up by over 200K lines of server side code.

  • josheli

    “best practices” -1
    “worse is better” +1

    if php ever starts enforcing “best practices” at the language level, its death knell will have sounded.

  • It’s interesting that nobody has mentioned Flash in these discussions…

    Flash ActionScript is ECMA compatible, and in some aspects, nearly identical to javascript, but has a rich API, which offers many possibilities… Latest releases provide a fully featured object model, similar to Java in some respects, but without reducing the possibilities of the func/obj literal notation…

    Ok – it does have its drawbacks, regarding accessibility and whatnot, but it seems to be a far more comfortable environment to build rich internet apps, especially interfaces with more complex dynamic form controls.

    You could say that Macromedia took up the challenge described above, some 5 years ago, and its not hard to see how successful they have been… pretty amazing for what started out as a simple vector graphics format…

    I think the browser divide (IE6 vs Mozilla) is the main thing holding up open source progress in this area. Using XUL bindings, it should be possible to create some kind of in-browser debugger/editor for dynamic wep apps. People have already done it for CSS – so where is my runtime javascript toolkit?

    I needed it yesterday not tomorrow ;)

  • Just a thought, when someone mentions 60,000 lines of code why do I immediately think poor quality?

  • Well the way I see it is that you can “enforce” some best practices in the language. for example Java has the keyword ‘interface’ In PHP you will write in the manual what you want people your framework to do.

    In general I’m dead against “enforcement” in programming languages but that’s a long discussion.

    As it goes I think PHP does about the best job of flagging the “right way” to use it, where possible, compared to the other dynamic languages I’ve played with. In particular the class syntax is good (ignoring issues like PHP4 references).

    PHP was perfectly designed for a certain purpose( i.e. building web apps ). Sometimes it’s bad to enforce some features in the language.

    Very true. But taking a general purpose language like Python and comparing with a statically typed language, identifying the “right tool” is another matter. Which is why I mentioned wxPython, for example – GUI developers should be raving about it – it delivers (for Win/Linux/MAC) where Java AWT/Swing still looks ugly / slow.

    Just a thought, when someone mentions 60,000 lines of code why do I immediately think poor quality?

    Words like pain and suffering spring to mind as well ;) That said think there is a way to write pain-free Javascript (not that I know exactly what it is) and codifying that would be a great achievement.

  • Louis-Philippe Huberdeau

    I wrote a complete spreadsheet application in JavaScript in a little over 1000 lines (LGPL, included in TikiWiki). I have to admit it was turning to hell in the end, but still works in both IE and Mozilla. There are not so many differences, other than the fact that IE incorrectly support quite a few details.

    I guess one of the hardest things with JavaScript is to divide and document code. As everything has to be sent to the client, adding comments does slow down the application (at least load time). Using too much separate files also causes problems. The only thing I hope right now is that no one reports a bug about it.

    As for Flash, I’ll quote Vincent Fran

  • It’s definitely apparent that a lot of flash apps are written for showing off. The technology is based on a vector rendering engine, but I don’t think that it just applies to pure presentation. It is also a platform to support client-server connectivity, with multiple approaches, like XMLSockets, or remote scripting, as well as built in representations of record sets and iterators… Javscript/DOM/XMLHttpRequest has nothing near the level of support for client-server interaction that Flash does, so if you are building applications with these technologies you are definitely doing it the hard way (and respect to you for that!)

    Having said that, I totally agree that its hard to find a better solution than HTML forms most of the time…

  • arborint

    With all the talk about Best Practices in PHP or Javascript, the thing I never see are proposed examples of actual Best Practices. Instead you get comments like “if php ever starts enforcing ‘best practices’ at the language level, its death knell will have sounded.”

    Best Practices aren’t enforced, people use them because they are what they say they are: proven quality solutions to common problems. And that does not mean that they don’t change over time, nor that there is always a single solution for each problem.

    Best Practices are not for the Not-Invented-Here crowd; They are for solution oriented programmers.

    I honestly don’t think defining some Best Practices for PHP and Javascript is as hard as people make it out. If you actually surveyed PHP and Javascript code that solves a common problem, form validation for example, and defined requirements like testablity, PHP and Javascript working together, preformance, etc. you would very quickly narrow it down to one or two approaches that would be Best Practices.

    No writer or group has yet to tackle Best Practices for PHP. I think the reason is that the PHP culture is still immature.

    Here is about the only match when searching the web for “PHP Best Practices”, and the guy is not really a PHP fan:

  • Eric Vautier

    (Late) response to matthewh’s question on unit-testing JavaScript: see

  • Pingback: SitePoint Blogs » Javascript Inheritance()

  • Pingback: » Blog Archive » Javascript Inheritance()

Get the latest in Front-end, once a week, for free.