Feeling good about less lines of code

At the Rails Edge conference, one of the speakers mentioned how, as a young programmer, there was no better feeling than to write a program with an enormous LOC count. It made him proud. And I tend to agree. I remember first learning C/C++ and how good it felt to have a rather large program compile successfully. “The more the better,” I thought. Well, that’s hardly the case. Software development entails writing less and less LOC, intentionally. The creator(s) of Rails have made some logical assumptions that have allowed them to extract a lot of the annoying and incessant work, which provides a more focused development process.

As an example, here’s the controller portion for an edit method using the well-known MVC Java framework, Jakarta Struts:

public ActionForward edit(ActionMapping mapping,
                          ActionForm form,
                          HttpServletRequest request,
                          HttpServletResponse response)
  throws Exception {
  PersonForm personForm = (PersonForm) form;
  if (personForm.getId() != null) {
    PersonManager mgr = (PersonManager) getBean("personManager");
    Person person = mgr.getPerson(personForm.getId());
    personForm = (PersonForm) convert(person);
    updateFormBean(mapping, request, personForm);
  }
  return mapping.findForward("edit");
}

And here’s the cleaner, simpler Rails version:

1
2
3
def edit
  @person = Person.find(params[:id])
end

Rails takes care of the tedious, repetitive, grunt work—much more than I realize am making note of, here. From this example, to cut the Struts method down to one line, a few assumptions had to be made:

  1. Most methods are usually public
  2. Put regularly used objects in scope
  3. No need for a “manager” (which caused two objects to represent person, plus conversion code)
  4. No mapping
  5. It’s a web application, so stuff will fail (remove the explicit throws Exception)

That makes sense to me. While working with Ruby and Rails, I’ve enforced my desire to do more with less. It’s often more of a challenge to write less code to achieve the same results, but it’s much more fun.

Comments

01

Luke on Sat Feb 17 at 01:22PM

Do you mind if I ask a question or two about Ruby on Rails Ryan? I mean, I’m just teaching myself it finally using http://tryruby.hobix.com/ which is about 1000 times more useful than the main site, and it’s got me thinking. Okay …

1: Did you ever learn PHP? Because thats what I know fairly well at the moment, so do you think a transition to Ruby would be easy, especially in terms of high level functions, such as interacting with a database?

2: How did you learn? i.e. books, etc

3: How widespread is the use of Ruby on Rails in actual web design circles, as opposed to ASP or PHP, (might be too hopeful, but any idea about this in the UK)?

Any answers would be seriously appreciated mate, as I’m finally beginning to see what all the fuss is about from that site, so I thought I’d ask the one person who is always writing about it some advice. Cheers!

02

Ryan on Sat Feb 17 at 08:10PM

@Luke – I don’t mind at all. I love talking about the things I enjoy. Especially if it helps out someone else.

  1. I’ve worked with PHP for a bit, but I found myself more attracted to the CSS/design side of things, so I would do what I had to do in order to get my server-side functionality in order, then play with CSS like a madman. And that, believe it or not, was when I first began to really get into programming, specifically, programming for the web. It was all knew to me. But to answer your question, you seem to be proficient in what you do, and very capable of learning. Ruby is an extremely intriguing language, an really, really fun to learn. As I’m sure you’re realizing, everything is an object, which gives you this sort of intuition to try different things with it. And often enough, your guesses will work. It’s very intuitive. There is a learning curve, and it’s definitely different from any language I’ve written in before, but once you get the hang of it, the code becomes very pleasant to look at. And as far as connection to a database, if you’re writing Ruby using Rails, it’s extremely simple, and in fact, you simply supply a couple of things in a YAML file, and it does the work for you.
  2. Ruby on Rails is, honestly, my first web development language that I’ve taken seriously. I used PHP to get familiar with the ideas of server-side vs. client-side, connecting to a DB, and a few minor other things. Like I said, I was more focused on the presentation of it all over the concepts behind it. So, when I began learning Ruby on Rails, I wanted to take it a little more serious. I read a lot online about it, and the AWDWR book pretty much got me up and running. I learned a lot the first time I read it, but probably even more the second time (in the second edition). All in all, I have probably 5 books on Ruby and/or Rails. What I recommend is you don’t learn Ruby via Rails, but learn Ruby for the sake of learning Ruby. Then learn Rails. I didn’t do that, and I often find myself in situations where I have to either look up syntax or simply miss out on the nicer way of handling something. One thing that I found was a little confusing in Rails was the MVC architecture. Once you get a handle on that, though, the benefits are amazing. And I have to mention, I don’t think I could have made it this far without Chris Scharf—I’ve bothered him a countless amount of times, and I’ve learned a ton from just reading his posts. He really knows his stuff, as you can probably tell from all of the constant suggestions, corrections, and help he provides on my posts (if you read the comments, that is).
  3. Ruby on Rails is growing rapidly. I think Ruby climbed to like number 11 in the top web programming languages used today? It’s still relatively new I suppose (and Rails was released in 2004), but there is a lot of support from the open source communities. I honestly haven’t been in the game long enough to say how it compares to ASP or PHP, but I know it’s a blast to work with, and I can’t fathom someone trying it out and not liking it. It’s just a matter of getting people to try it out. I’ve worked with PHP a little, ASP and ASP.NET a decent amount, and they, in no way, can compete with how much I enjoy writing applications in Ruby on Rails.

Hopefully that answered some of your questions, and feel free to ask any others that you have. I’ll do the best I can answering them, and if I can’t, maybe someone else will! I’m glad to hear you are diving into Ruby on Rails, I think you’ll be pleasantly surprised at how productive and fun it will be.

03

Luke on Sun Feb 18 at 12:18AM

Thank you very much Ryan, all that is really helpful. And looking what you saiding about Ruby being intuitive in your first answer, I know exactly what you mean, I was trying all sorts of commands of thinks, such as .include? on … I think it was a Hash? didnt work, and it really messed up the tutorial, but it was fun anyway to see if it did work.

Another advantage of learning this is now I can sort of understand what your Ruby posts are about haha! Thanks again!

04

Chris on Sun Feb 18 at 05:51AM

@Ryan – Well, I’m glad I can help :-)

@Luke – I agree with Ryan on learning Ruby on its own first – you’ll want to get a copy of the pickaxe – it’s how I learned Ruby. (You can even read the first edition online for free).

And don’t forget the ri command – it provides a handy reference at the command line for Ruby classes and methods.

Have something to say?
Please rewrite the image text Are You Human? Hint: Are You Human? Formatting Tips

or

© 2009 Ryan Heath | Site Management A Ruby on Rails production.

Get in Touch