1

I am working with Ruby on Rails at work and have been reading the Pragmatic Programmer on the side.

I was given the task to create some small registration application with ~10 form fields. From my previous experience, when dealing with forms it becomes a painful process and Rails doesn't seem to make my life much easier.

Influenced by the book I started creating a library that based on a YAML metadata does a few things:

  • Attaches the metadata to the Model class
  • Generates attribute getters and setters based on the metadata
  • Generates validators (ie. myuser.valid?) based on the metadata

The metadata file for a user (user.yml) would look something like this:

First Name(s):
  programmatic_name: forename
  example: James
  validate: \D*
  require: true
  input_instruction: Type your first name(s) here
Surname:
  programmatic_name: surname
  example: Green
  validate: \D*
  require: true
  input_instruction: Type your surname here
Club:
  example: 'Hawks'
  programmatic_name: club
  require: yes
  input_instruction: Choose your club
  valid_num_values: 1
  valid_values:
    - Darlings
    - Hawks
    - SSG

Currently with this configuration I generate form fields with a loop over the metadata. I validate every field based on the valid* entries in the metadata.

Form generation:

<html>
<form>
<% 
  for attr_name, meta in metadata:
    <input ..
  ..
%>
</form>
</html>

Also I do automate the testing partially by using the example entry in the metadata as something that should be valid and a random garbe to be invalid.

Testing:

test 'valid attributes should be accepted'
  for attr_name, meta in metadata:
    u =  User.new({ attr_name: meta['example'])
    assert u.valid
    u =  User.new({ attr_name: random_garbage())
    assert not u.valid
end

My concerns

The first comments from my supervisor was that although it is not wrong, it adds maintenance overhead. From my point of view it is the opposite since it let's you have a central place to reuse things. However I am not that experienced so it's nothing I can really argue about.

I am pretty sure that I am not the first doing something like this, but how come there isn't much talk about this in the wild web? I found this interesting article dating from back in 2006 that did indeed scare me a bit: https://www.simple-talk.com/opinion/opinion-pieces/bad-carma/

So how do you know if metadata turns into an overhead or actually is helping? Why aren't there any applications in the wild using a more metadriven approach to generate things on the fly? Any success stories? Is metadriven applications in an MVC context a benefit or a burden?

2

Custom-built metadata-driven systems like this have a habit of turning over time into unmaintainable messes. Your supervisor is right to be cautious about taking the approach. While they can be useful (if carefully designed) they can also end up with a large list of special cases added to handle facilities that are needed for individual operations on an ad-hoc basis, and sooner or later they become too difficult for anybody but an expert to work with. A widely discussed example of this kind of system and the problems it causes is described in this article. Read the comments; there are lots of war stories from people who've had to deal with this kind of system.

A common aphorism is the suggestion that you should "do the simplest thing that could possibly work", which is definitely an idea that applies in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.