Archive for the ‘Programming’ Category

MVC Done Right?

Friday, October 17th, 2008

Model-View-Controller (MVC) is a design style that dates back (as so much does) to the pioneering work of the Xerox PARC facility. The idea is that applications should be separated into:

  • Model: the data that the application manipulates, and the fundamental operations it performs on that data
  • View: How the application looks to the user.
  • Controller: How user, view, and model interact.

(Yes, yes, I’m sure anyone who cares to can pick apart my definitions. Don’t care.)

The idea is, views change all the time (today it’s radio buttons, tomorrow it’s drop-downs), controllers change moderately (add a confirmation step, tighten up the authentication), and models hardly change at all (an accounting program isn’t going to change into an MMORPG). So isolate the fast-changing bits from the slow-changing ones. And also allow different specialties — database hackers, usability analysts, graphical designers — to put apply themselves to their own field exclusively.

How do you do that in web applications? The received wisdom of the day is:

  • Model: database + ORM (e.g., Hibernate, ActiveRecord)
  • View: templating language
  • Controller: application framework (Struts, Spring, Rails, etc.)

In other words, do it all on the server, all in one language (which, of course, fits all purposes, because it is the One True Language). Maintain separation of concerns with iron discipline and wishful thinking.

My own view:

  • Model: database + RESTful web service, delivering information in JSON
  • View: Static (X)HTML/CSS
  • Controller: Javascript/AJAX

The controller code can be implemented in any server-side language: Java, C#, Ruby, Python, Erlang, whatever; it just needs to be able to talk to the database and send JSON over the wire. The view can be created in an HTML/CSS environment like Dreamweaver, with no worries about whether it’s compatible with the developer toolset. And the controller logic… Well, I’ve been playing with jQuery lately, and it may shock some folks to learn that coding client-side Javascript with a good compatibility layer library is actually fun.

Dividing tasks by physical location and implementation language creates a strict separation of concerns. Server-side code does not generate HTML. Controller code does not do SQL queries. View markup contains no executable code at all: there is no template language. There is zero possibility of the parts bleeding together.

Why wouldn’t this work? Am I missing something?

ECMAscript4 is Dead; Long Live 3.1

Wednesday, August 20th, 2008

Okay, I’m a week late hearing the news, but it seems the EcmaScript (aka JavaScript, JScript, etc.) committee has cut its losses, and settled on a less ambitious next version of the standard, ECMAScript Harmony. Packages, namespaces, and early binding are gone. Which kind of leaves Adobe with a problem, as they had started implementing a lot of the ES4 features in ActionScript for Flash/Flex.

Namespaces are, in other languages, an important means of controlling complexity by partitioning the code into well-defined pieces. Maybe it’s not so essential in Jav(ahem) EcmaScript, especially in web browsers, which account for 99% of the current use cases. I think dropping them reduces the chance that EcmaScript will break out into other domains, and I think that’s unfortunate. But I’ll admit, the technical trade-offs involved are beyond my current understanding.

I’d very much like to know how this is going to affect the Tamarin project, though.

One More Data Point

Wednesday, December 12th, 2007

Safari Books Online is a service by technical publisher O’Reilly and Associates, that allows subscribers to their service to access their books, and those of several other publishers, online. (Highly recommended, BTW.)

Anyway, their front page lists the most popular books on their service. For as long as I can remember, the top book was David Flanagan’s Java in a Nutshell; but today, it has been displaced by JavaScript, the Definitive Guide (also by Flanagan, as it happens).

It’s notable mainly in support of Steve Yegge’s proposition that JavaScript is the Next Big Language. It is certainly popping up everywhere, and it is, for now, the only Apple-approved method of developing for the iPhone.

UPDATE: Okay, now both of Flanagan’s books have been displaced by a C# book. Please forget I said anything.  :)

RADAR: RESTful Application, Dumb-Ass Recipient

Thursday, November 29th, 2007

PragDave: The RADAR Architecture: RESTful Application, Dumb-Ass Recipient

I’d been thinking along similar lines for a new project (which is expected to be a sort of testbed for a web services architecture). It’s a web service meant for use by third parties.

I decided, after thrashing around a bit, that machines that reside outside our firewall should be treated like end users: the interface presented is presentation logic. The fact that it is a web service is immaterial; it is distinct from the bare RESTful API to our business rules that we present to internal applications.

The application layer is what makes a process “presentable” to outsiders. It may mean consolidating several actions that are internally seen as distinct into a single operation; or adding authentication and data validation; or conforming to externally-imposed specifications (e.g., presenting an external SOAP interface); or information hiding to allow for future changes to your model and business rules.

If Programming Languages Could Speak

Friday, October 26th, 2007

I love obscure humor. Being the only person in the room to get a joke makes me feel so special.

You pretty much have to be a programmer — and not a Johnny One-Language, but a Real Programmer — to get the humor in this post. If you aren’t, then take my word for it, it’s damn funny. (Be sure to read the comment about Python.)

JavaScript Rising

Wednesday, July 11th, 2007

In this post I wondered why no one (that I knew of) had proposed using JavaScript as a server-side language, and developed tools to support it. Well, they say it steam-engines when it comes steam-engine time. A lot of other people had the same idea; and unlike me, some of them did more than just blog about it.

Googler and blogger Steve Yegge (whose keynote at OSCON I am going to miss, dammit) made a splash by porting Rails to JavaScript — specifically, the Rhino JavaScript-on-Java interpreter that is bundled with Java SE 6. He calls it “Rhino on Rails”, the clever bastard.

In an effort to increase developer productivity at Google, Steve tried to convince the company to adopt Rails (and consequently Ruby) as a programming language. When that fell on deaf ears (Google really does not want to increase the number of languages that must be supported by their infrastructure), Steve decided to do what any other frustrated programmer would do: he ported Rails to JavaScript. Line by line. In 6 months. Working 2000 hours. Steve is a coding stud.

And he is not alone: witness Project Phobos.

Phobos is a lightweight, scripting-friendly, web application environment running on the Java platform.

It comes with a set of plugins for the NetBeans IDE that cover the complete development process. These include a fully-featured debugger; wizards to help you get started faster; a palette of Ajax widgets that can be dropped on a page, thanks to jMaki; and the ability to generate a standard web application for deployment on any servlet container or Java EE application server.

Currently, the primary language supported by Phobos is JavaScript. By leveraging JavaScript on the server, Phobos allows developers to use the same language on the client and server tier of a web application, eliminating the impedance mismatch that characterizes other approaches to Ajax.

Digging farther back in Steve Yegge’s archive, one finds he has been thinking along these lines for some time:

JavaScript is probably the most important language in the world today. Funny, huh? You’d think it would be Java or C++ or something. But I think it just might be JavaScript.

For one thing, despite JavaScript’s inevitable quirks and flaws and warts and hairy boogers and severe body odor, it possesses that magical property that you can get stuff done really fast with it…

See, JavaScript has a captive audience. It’s one of those languages you just have to know, or you get to miss out on Web programming, and in case you hadn’t noticed, thick clients are like Big Hair these days. Most non-technical people I know pretty much live in their browsers, and they only emerge periodically to stare in puzzlement at iTunes or a game or something, and wonder why isn’t it in the browser, because everything else useful seems to be. It’s where the whole world is. To non-technical people, of course. Which is, like, practically everyone.

What other language is supported, in a reasonably cross-platform manner, on the Windows, MacOS X, and Linux native APIs, the Java virtual machine, the .Net virtual machine, the Parrot virtual machine, the Flash runtime, the Silverlight runtime, and 99% of the web browsers in the world? (And, oh yeah, what’s that new thingamajig from Apple? I vaguely remember reading something about it. Can’t seem to recall the name…) In other words, JavaScript not only runs on every platform of interest; it is the only language that runs on every platform. [1]

JavaScript isn’t winning the fight to be the Next Big Language; it’s already won.

[1] Actually, I don’t know if it runs natively on PlayStation3 and XBox 360. But given the little-bitty interpreter and the great big storage media, the games could ship with their own JS runtime and not miss the space.

Brendan Explains the Value of Forking

Saturday, June 23rd, 2007

Okay, I have nothing really to add on this, but this blog entry by Brendan Eich, inventor of JavaScript, was simply so brilliant I had to point it out:

Forking is an extreme point in a continuum of options that exist with open source. The option to fork must exist as a feedback mechanism, but it need not be used in order for users to gain benefits not available with closed source and proprietary standards. Forking can be the right thing, or it can be a kind of mutually-assured-destruction option that keeps everyone acting in the interest of not forking.

Forking is not evil. The right to fork is a feature, not a bug. (And this makes Sun’s long-standing resistance to open-sourcing Java — recently overcome, happy happy joy joy — out of “fear of forking” seem especially short-sighted.)

Modularity and OOP

Saturday, June 23rd, 2007

Coding Horror: Your Code: OOP or POO?

From the comments:

generally, people need to use module-oriented programming, but the lack of support by most mainstream language of the day makes them rely on object-oriented programming instead, calling for all possible abusements.

My own career followed an unusual trajectory. Because of where and when I got my Master’s degree, I was exposed to Ada 83; and because of where and when I worked, I had a chance to actually apply it, before I ever did any C++ programming.

Ada 83 was the first standardized version of the Ada programming language, and it was a curious beast. It’s one of the few examples of the transitional phase in software development, after language designers had learned of the value of modularity and information hiding, but before object-oriented programming set the world on fire. (The only other example that comes to mind is Modula-2, another creation of Pascal’s designer, Niklaus Wirth.)

Pascal — the language from which Ada derived its basic syntax — ruthlessly enforced the principles of structured programming, and taught a generation of comp-sci graduates (including me, class of ‘85) the value of those principles. Ada did the same with modularity and information hiding; data and subroutines were grouped into packages, interface modules controlled interaction between packages, and the visibility of package components could be finely controlled. This appealed to me, as it reinforced the ideas I’d picked up from my senior-year courses in software engineering, using the text Structured Design, by Edward Yourdon and Larry Constantine: maximize cohesion (keep closely related things within the same syntactic unit) and minimize coupling (make sure interactions between units are few in number and explicitly defined).

The idea of all this being to make maintenance easier, by allowing program components to be modified in isolation, without breaking other modules that interacted with them. Unless the modifications resulted in changes in the (narrowly and explicitly defined) interface, changes would not propagate to other modules. (Bertrand Meyer’s “design by contract” principle took this idea further still.) That was the theory, anyway; but practice actually approximated theory, if not perfectly. Modularity is one of the few ideas that everyone (now) agrees is a Good Thing.

All the tools to do modular programming are present in C; you can declare your interfaces in .h files, and keep everything else private using the ’static’ keyword. But typical C programmers of that time didn’t know that one should program that way, and the language design didn’t make it the path of least resistance. (Top-level functions have linkage unless you explicitly say otherwise; the compiler doesn’t disallow referencing declarations in .c files or defining declarations in .h files, no support for namespaces, etc.) So it wasn’t typically done that way. But knowing Ada, and knowing the rationale behind it, made me a better C programmer; my programs followed modular conventions, and strictly controlled visibility. (And when C programming is still done these days, that is the typical practice.)

All of these principles got swept up into the meme-complex known as “object oriented programming”; and since the industry transitioned from the kinda-sorta modular C to the object-oriented C++, without a fully modular language in between, people credited OOP with benefits that weren’t really intrinsic to OOP, but were intrinsic to — for want of a better buzzphrase — module-oriented programming.

OOP was oversold. You had to live through it to get an idea of how intense the hype was. The industry was convinced that non-object programming was evil; that hybrid programming, such as was allowed in C++, made baby angels cry. So one of the design criteria for languages like Java was that everything had to be an object, for better or worse. The basic usefulness of hybrid programming eventually led to the development of the Singleton design pattern and EJB session beans, and other semantic dodges that let programmers escape the ideology in order to get their job done.

As you might guess, I am not too impressed by the object-oriented everything ideology. Inheritance and dynamic binding are useful tools, but organizing your whole tool stack and API around them is a mistake. I prefer languages that allow hybrid programming (like Python, JavaScript, Perl, and C++); though I’ll allow there’s something to be said for languages like Ruby, where the object metaphor is so thoroughly baked in that you can choose to ignore it.

This is another reason I’m intrigued by the Scala programming language; while nominally as thoroughly OOP as Java, the Singleton pattern is baked in; you can just declare an Object, using the same syntax as for declaring a class. The distinction between this and a plain old Module is inconsequential.

I’m hopeful. After nearly twenty years, the industry and the profession seem to be recovering from the “object-oriented everything” mania. It’s about time. I wouldn’t want to do without classes and objects; but I’d rather have more than that hammer to work with, so that I can deal with whatever un-nail-like projects come along.

(Other People’s) Thoughts on Concurrency

Friday, June 22nd, 2007

Actors that Unify Threads and Events (Lambda the Ultimate)

Threads Considered Harmful (O’Reilly Radar)

Threads Suck (Brendan Eich)

Conclusion 1: Concurrency is massively important for the future, and threads are a lousy way of addressing it.

Conclusion 2: The Languages of the Future are ECMAScript and Scala, because (among other reasons) their creators are doing concurrency right.

Open Question: What about .NET? Obviously, ECMAScript (JScript) will work there, but will Scala be rehosted to the CLR? Or is there another language that can carry the standard (static typing + type inference + shared-nothing concurrency + closures + functions as first-class objects) on that platform?

XML + ECMAScript: It’s not silly, it’s the Future.

Friday, June 22nd, 2007

Silly season [dive into mark]

Mark’s rant is red and meaty, just the way I like it, but in the end I have to disagree.

The fundamental wisdom at work here is not, “stay away from proprietary platforms”; it’s “don’t marry the platform… any platform”. If you think that open source is insurance against your technology choices going wrong, I have to ask: how many job openings do you see these days for Tcl/Tk hackers? And doesn’t Perl seem to be going down the same path? (And no, I am not a Perl hater; on the contrary, I am a Perl lover who’s feeling kind of unrequited these days…)

I’ve been itching to dig into Flex (darn SCWCD certification still to do first…), and I feel fairly confident that the time will not be wasted. After all, at its base Flex is just ECMAScript and XML. Learning Flex is, in fact, good practice for programming the next generation of browsers: it’s the first available implementation of ECMAScript 4, the standard to which the browsers will eventually hew. And learning a new XML dialect is something that a professional programmer is going to have to do over and over and over in the decade or so to come. So where’s the risk?

That Adobe is going to hose the runtime? Backward compatibility with gazillions of Flash apps is their main selling point; and keeping it free-as-in-beer is how they (okay, Macromedia) established their amazing level of market penetration. Why the hell would they give any of that up? And they are open-sourcing the SDK, so no sale there either. Where’s the downside, honestly?

(Now Silverlight is another matter; Microsoft has neither an installed base to appease nor any commitment to open source anything; the only thing keeping them semi-honest is, well, competition from Adobe. So I’ll be steering clear of that.)