Frameworks, Good Code and the Opposite of YAGNI

March 11, 2004 11:46 PM

Hani has been wondering if good code is relevant?

Excluding the children amongst you, almost everyone has seen projects that have awful awful code succeed, along with projects using all the right buzzwords and cool frameworks fail. Out there in the real world, I'd be amazed if there's any correlation at all between success and anything that actually matters to a developer.

Reading this gives me one of those "Well... Duh!" moments. There are so many factors that go towards a project succeeding or failing that one of them -- any one of them -- will get lost in the statistical noise if it is singled out. We all know projects that have been based on good code and well-practiced methodology, but never seen the light of day because something else went wrong. And we all know projects that have done everything wrong, but have bludgeoned through with sheer bloody-mindedness and force of will.

The thing about writing good code is that while you might still fail with it, at least if you fail you'll know it wasn't the fault of the code1. As a programmer, you can't take responsibility for the relationship with- or demands of the customer, you can't take responsibility for changes in the economy or budget cut-backs, you can't take responsibility for the market-research that drives the project or the marketing that must sell it when you're done.

As a programmer, you're responsible for the code. If any of these other things get screwed up, your project may well be doomed. If any of these other things are done well, your project may succeed despite bad code. But at the end of the day, your job is to make the best of what you're being paid to do.

Much of Hani's wrath falls on the creation of web frameworks.

All these frameworks and web doodahs are more often than not simply the product of a hopelessly bored mind desperate to inject some sense of meaning into their daily grind. All the business asked them to do was product an app that solved a specific need. Nobody told them to go invent a framework for it, or to maximise reusability, or to componentise the moving bits, or to use TDD, or to opensource anything.

If I were to write a web application of any significant size using purely YAGNI methods: starting with a blank slate and only ever writing code that takes me towards concrete functional goals, it wouldn't be long before I started seeing that I was wasting a great deal of effort. I'd be copying and pasting a whole lot of code, and modifying the app to add new stuff would be increasingly difficult.

So I'd start refactoring the repetitive stuff into modules that I could reuse. This wouldn't be the result of a hopelessly bored mind, it'd be necessary for me to be able to work faster. I could do without it, and it probably wouldn't mean the difference between the success and failure of the project. Lots of projects have succeeded through cut-and-paste programming. But reducing the amount of code you have to write (or paste) for every function, you reduce the probability of bugs2, reduce the amount of time it takes to write the damn thing, and generally save somebody some money.

Do that enough, you'll end up with a framework.

Work on enough web application projects, and you'll get sick of working up to the framework from scratch. You know raw web applications are tedious exercises in repetition. You know you've solved this problem before. YAGNI gives way to IKIFNI (I Know I Need It)

I've recently started writing a web application in Ruby that will never see the light of day (it's purely a personal exercise to keep my brain ticking over), and the first thing I did, in accordance with IKIFNI, was to look around at the available web frameworks. Having spent the last three months working on a project based on Spring, WebWork2 and SiteMesh, the last thing I want to do is go back to plodding around doing the same things that these frameworks have helped me avoid having to do.

Unfortunately, I couldn't find one that didn't seem like the bastard child of Struts3. So I started building my own4. Because I know I'm going to need it.


1 Like all blanket statements, this isn't always true. In some death-march cases, you're best off writing really bad code really quickly, just to get something up and avert the cancellation of the project. But for the general case, it's a good rule of thumb.
2 As bad as "lines of code" are as a metric of productivity, they're still a remarkably good predictor of bugs.
3 Give a reasonably good OO developer the task of building a web framework, their first attempt will probably be something resembling Struts. Which I suppose gives good support to the "Build one to throw away" theory.
4 Which bears only superficial resemblance to any of the aforementioned, but I suppose pays homage to each.

mapper = Mapper.new()
mapper.add_mapping(".*\.ahtml", AmritaAction5, { Mapper::ALL => :render }, {})
mapper.add_mapping("/hello", HelloWorldAction,
  { Mapper::POST => :greet, Mapper::GET => :display_form },
  { :input => "/form.ahtml", :greet => "/out.ahtml" })
mapper.bind_to_server("/", webrick)

5 Amrita is a temporary measure. It's a clever idea, but one gets the feeling nobody's ever done anything serious with it, because its limitations became painfully apparent just from writing Hello World.

3 TrackBacks

Listed below are links to blogs that reference this entry: Frameworks, Good Code and the Opposite of YAGNI.

TrackBack URL for this entry: http://fishbowl.pastiche.org/mt-tb.cgi/489

You can have a hardware shop full of tools, but if all you need is a hammer, then you could waste hours cruising the shelves for that special tool, hours learning how to use it, and require of the other team members that they do the same. Read More

Just a few points in response to a recent post by Charles . 1. The choice of whether to use YAGNI has nothing do with YAGNI as such. Whether to use a framework is more an architectural concern. YAGNI is a process used during coding. Perhaps we need Read More

The Servlet and JSP standard is a good first attempt. But, like all first attempts, it should have been thrown away once the world had learnt some lessons from it. These are some of the lessons I learnt from Servlets and JSPs. Read More

5 Comments

IKIFNI? Hmm, what's the "F" stand for, eh?

Some of us like to have specialized tools for every application, or at least an excuse to have more tools....

In all seriousness, the single biggest reason for tool proliferation (of any sort) is that it is more efficient to have a tool designed for the specific application in question. The alternative is to increase the complexity of the tool -- adjustability, add a power source, add plug-in support, configuration files, etc.

If you're working with hand tools, you should have a wide range of planes, for example, because it takes a significant amount of sweat and time to plane a board from rough to smooth without using a different plane for each stage. Once you add electric motors and large chunks of cast iron to the equation, you end up with a single general-purpose machine that can handle all of the formerly separate subtasks, but one that might cost 10x (or more) as much as an individual hand plane, makes a huge amount of noise and dust, and may not be able to handle the same range of applications that hand tools can.

I think the analogy with software development is clear: a tailored tool set/framework for churning out DB-driven web sites, for example, is probably simpler and easier to construct. A general-purpose Web design framework is going to be a lot more complex and harder to use when compared to a special-purpose tool. Hence the proliferation of frameworks, and the fact that they all end up looking quite similar when they're rewritten to be more general. There is probably a connection to Greenspun's Law here, that all software system eventually grow to include a half-baked, buggy equivalent of Lisp.

The truly brilliant folks out there create tools that are both general-purpose and simple by radically rethinking the fundamental problem; the rest of us are condemned to slide up and down the curve that relates complexity to generality.

"Unfortunately, I couldn’t find one that didn’t seem like the bastard child of Struts3. So I started building my own4. Because I know I’m going to need it."

You really couldn't find one? How about Tapestry, or Echo, or InverSoft

http://www.frameworks-boulder.org/Application_Frameworks.html

I completely agree with you that your job is to make the best of what you’re being paid to do. It concerns programmers, designers, coders and everybody. Then all the team works with high productivity, and the project is less liable to changes in economics, market, etc.

Newer than this post, but here's a reputedly good framework for Ruby:
http://rubyonrails.org/

Comments are no longer being accepted for this blog entry. If you really want to make your voice heard, you can always email me.

Previously: Introspection is the Enemy of Blogging

Next: Google Evil?