Wednesday, 10 March 2010

Rails - Day 2 - The Power of Consistency

So far in the series we have:

Rails  - Day 1 - The RubyGem Love Story
Rails -  Day 10 - Testing Rails Controllers
Rails -  Day 13 -ActiveRecord Relationships Part II
Rails - Day 15 - Rails Forms Part II

In my last post, we gave a brief intro to Rails and touched on what is possible with RubyGems and how easy it is to install Rails itself via RubyGems.

In this post I want to convey the usefulness and importance of simple consistent models like a guaranteed project structure for all your rails projects.

With Rails installed we are now armed and ready with the rails framework primed and ready to serve.  

One thing that might be unsettling or off putting to a lot of .NET developers is how commands to the rails workflow are more often than not issued through the command prompt and not through an IDE like Visual Studio.  

My message to them is simple......"F**K off, this is not for you!!".  We are not running charities for the unable, if you are unfamiliar or uncomfortable with the command prompt then I seriously question your choice of career.

The next part of my guide to enlightenment is one that appears to be on the surface understated or not worthy of a whole blog post but to me this is really the underpinning of what is right about rails and what is wrong about .NET.

As is customary in these type of posts, I am going to create some sort of minimal application to outline the principles without going to deep.  I am going to create a project called DataCapturer which is actually an idea I am toying with for a MicroISV product.

It is an idea that I have created a test web site for in order to do some market research before building the product.  Anyway, that is not the point of these posts.

With the title of our semi-mythical project chosen, we simply cd into our projects directory and type the following command: rails datacatpurer and hey presto, a project folder filled with lots of goodies to ease my transition from the conception of my applciation to reality:

This will create the following folder structure:

A lot of the screenshots I am going to post here are screenshots of the very excellent RubyMine IDE. I am in no rush to give up the power of an IDE for the simplicity of textmate and the various emacs that exist for rails. 

When you create your first rails app, the directories are laid out for you.  The structure is well-organised and everything has a place and everything is in its place.  This does not just go as far as a folder for the MVC stuff like Models being located in app/models,  and controllers in app/controllers etc.  Quite literally everything has its own designated place in a rails project.  There is a good reason for this and the naming conventions are important as it lets rails applications find their parts easily without any additional configuration.  Also, it's easy for another programmer to hit the ground running with any rails app.  A lot of rails convention over configuration paradigms are made possible because the framework can generate alter and pick up paradigms by simply being able to guarantee where the files and directories are.  This point is very important and without it, a lot would not be possible.

Contrast this with the anarchical world of .NET where a blank project is very much a blank slate to do whatever you please.  This blank project is bereft of guidance.   ASP.NET MVC is a step in the right direction with some semblance of a consistent structure.  A step in the right direction that is copied straight from Rails.  In fact ASP.NET MVC is quite obviously and thankfully a rails clone.  A poor man's rails but an attempt none the less.

I think Rails was the first framework that claimed to be an opinionated framework and this is our first real example of how it is opinionated.  It is forcing its personality from the moment you create the project.

It might seem strange to consider a folder structure as a medium for conveying guidance but I hope to try and illustrate that this is indeed the case with rails.

In .NET every, project has carte blanche to do whatever the hell they want.   Any time I mention this to some .NET developers they get very defensive about not having the freedom to put their files where they please.  They see this as some sort of invasion of their working practices or as an obstruction that will hinder progress.  My question to them is this, what exactly do you gain from this freedom?  I honestly cannot think of one thing apart from a pretty and unique folder arrangement.  Unique to yourself that is but useless in practice.

If you look at the folder structure above, it is a very strict imposition that I find very appealing.  You can see that we even have a designated folder for our tests.  Even if you are not currently practicing TDD and you are using rails, you might feel compelled to check out what this whole TDD thing is, simply by the existence of this folder.  A further encouraging factor is that the test directory is further subdivided into categories like unit and integration.  The road to writing maintainable tests for me was both long and arduous.  I was unaware of the distinction between unit and integration tests for some time, with rails the concepts are there in front of me.  Or at least I will more than likely ask myself the question, "why do I have a separate folder for unit and why do I have a folder for integration?".  Again we see rails pushing down certain design decisions.

In the interest of keeping things short which seems to be this season's black when it comes to blogging, I am going to leave things here.  

In the next post, I am going to touch on rails environments.

No comments:

Post a Comment