Mark Needham

Thoughts on Software Development

Coding: Using a library/rolling your own

with one comment

One of the things that I’ve noticed as we’ve started writing more client side code is that I’m much more likely to look for a library which solves a problem than I would be with server side code.

A requirement that we’ve had on at least the last 3 or 4 projects I’ve worked on is to do client side validation on the values entered into a form by the user.

The jQuery.validate plugin is quite a good fit for this problem and as long as the validation is just on a field by field basis then it works fine.

On the last project I worked on, however, we had validation rules which had field interdependencies and suddenly we ended up writing a lot of custom code to handle that on top of what jQuery.validate already did.

Eventually we got to the stage where the code had become a complete mess and we decided to rewrite the validation code server side and only fire the validation when the user submitted the form.

In this situation that was an acceptable trade off to make but in another we may have needed to write our own Javascript code to handle the various validation rules.

In that case we’d probably want to write our own code to handle the inter field dependencies but still use jQuery.validate to handle individual field validation.

While thinking about this I was reminded of a post written by Michael Feathers back in 2003 where he discusses ‘stunting a framework’:

[...]let’s think about great frameworks… erm.. there aren’t many are there? In fact, even the good ones are a pain in the ass, aren’t they? There was that time when we downloaded framework X and it took quite a bit of time to learn how to use it, and that other time when we thought it would be useful if only it did that, but we spent the next week trying to force it and…”

Framework use is hard, yet we keep trying. Why do we do it? Mainly because we want to [...] leverage the work of others. If we use someone’s else framework, we may save some time. Moreover, we’ve benefited from someone’s “crystalized thought,” thought in the form of working code. The code shows a proven way of doing things and if it’s well-designed it can accommodate our thoughts and we can roll them back to the community.

Although the majority of the article is talking about frameworks from the angle of avoiding the temptation to create frameworks, I think it’s interesting to consider whether we always need to use a framework.

One other area where I’ve noticed we instinctively turn to a framework is when we have to interact with a database. The instinct is to straight away use Hibernate/NHibernate/ActiveRecord when frequently the initial use case doesn’t really require their use.

However, if we don’t make that decision up front then we need to be quite vigilant about observing the point at which we’re actually just reinventing the wheel rather than making a pragmatic decision not to use an ORM tool.

There are certainly other considerations to make when deciding whether to use a library or not such as our familiarity with it and its reputation/reliability in the community but it is still a decision point and one that I’ve frequently not recognised as being one and just gone straight for the library option.

Written by Mark Needham

August 10th, 2010 at 5:25 pm

Posted in Coding

Tagged with

  • ipsi

    I almost always use a framework or a library if I can find one.

    Part of the reason is that there’s often just me and another developer working on something, and the time saved by using a library can be quite substantial, especially if I don’t know everything there is to know about the problem I’m trying to solve with the library, as it then becomes a case of “Understand, at a very low level, how encryption works. Understand how to generate a cryptographic signature for an XML document” (Been doing Web Services Security lately).

    Those are things I really don’t need to understand perfectly to be able to use. I need to have *some* grasp, sure. But being able to understand the whole thing? Totally unnecessary.

    So that’s one place where using a library/framework is either necessary or at least a serious timesaver, unless you *already* know how the underlying technology works, because if you don’t then you’re going to need to learn an awful lot to be able to roll your own code for this, far more than the problem domain requires.

    The other reason I, personally, have for using libraries almost instinctively is bad coders.

    Take your ORM example. I currently work with a home-grown ORM framework, and it’s *shit*. It works, but the code is all over the place and a lot the design decisions are questionable at best. In several areas it’s also really, really slow, and mixes data in with the data structure (so the homegrown linked list contains not only a reference to the next link, but also to the fields pulled from the database).

    Given that, I’d basically always take a library, especially something like Hibernate, where paid support is available if you’re in a serious bind, over rolling my own code every time.

    Why? Because while I might be able to write a decent ORM framework (*might* being the operative word here), I can’t guarantee that someone else won’t come along and start making changes to it that will leave it worse off than when I started. Ok, sure, code reviews and careful hiring practices help significantly in this regard but I bet there are people out there who do not have control over who gets to touch their homegrown code, and in that situation I’d be taking a known library or framework simply to try and limit the damage a bad coder can do.

    But that’s just based on my experiences, and I don’t disagree with what you’re saying about needing to *think* about what you’re doing.