Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Sunday, July 31, 2011

From a Java/C# world to Ruby's: Writing modular code

For the past handful of months I've been dabbling in Rails in my free time to play with some projects.  It's a fun platform for creating applications and I (as has been mentioned) dig Ruby.  I'm starting to actually build up enough code to warrant a little more discipline than what I've had so far.  For the most part, I've been plowing through just trying to experience all the tools and tricks the platform has to offer.  I couldn't really be effective otherwise if I hadn't given myself any time to play in the sandbox.

One thing I'm trying to learn is how to modularize code in Ruby.  How and where do you draw lines of responsibilities?  Where should code live?  Should I use a module or should I use a class?  There hasn't been a straight forward answer.  Ruby being dynamic, open classes and being able to intercept/modify just about any behavior in the system...it feels like my first year away at college (well, not really but you catch my drift).  While it's a lot of fun, you have to make sure you don't run wild with your freedoms.  There's a limit and you have to govern yourself.

Again, where are the lines drawn?  What are the right things to do?  It's not apparent.  Look at models as they're known in Rails.  Models use the Active Record pattern where each instance constitutes a record in a persistent store.  So what is the typical Rails model responsible for?  Reading and writing itself from a persistent store at the least (along with any of its child models).  This alone will make members of the CQRS Illuminati grow faint.  But it doesn't stop there.  Also, you need to perform your validation there which makes sense for any stateful, data-driven creature to do.  

At first, I let everything pile into my models.  If I had 3 ways I wanted to query things from the DB?  Oh, hey, I need to query my associations too, what should I do with all that logic?  Put it in the model.  If I had extended validations dependent on certain conditions?  It undoubtedly went in the model.  If I add an authentication framework that needed to decorate the client classes?  Hey, I'll just add it to the model!

It never felt right and as I piled more functionality on, things became especially itchy.  Instead of trying to foresee how this would all pan out and try to apply some half-brained pattern of my own, I just went for it and made things a sloppy mess.  I really wanted to see what the wrong way to do things was so then the answer would be more apparent.  Just like my early days when I realized how tests benefited my code, I could learn from it.  Why?  Because Ruby isn't Java or C# and I'm a Ruby part-timer.  I've seen and read about trying to apply patterns from either of those languages that is inappropriate.  I decided to let mother nature dictate how I should proceed.

In the case of the rogue models, I found what makes me most comfortable.  First, anything related to the data and validation of a model stays in its class definition.  Second, any associations defined stay in the model's class definition.  Third, anything that demonstrates how that model behaves in its domain should stick around (if possible).  I want to be able to see and quickly digest what the model is and what it's related to.  

Last, everything else, provided that its a significant amount of code, is placed into modules.  Modules allow me to create meaningful, cohesive groups of methods and constants.  For example, the code to query the DB (in any number of ways) is pulled out and placed in some sort of data access module.  Modules, while not being the same as classes, act very much like a class in most senses.  What I don't have a feel for is how many includes is too many includes.  The models get this very facade-like feeling.  They do a lot.  It's still something I haven't quite gotten used to yet.  

So, the short of the long, modules are nifty and I can draw parallels with how I used interfaces (and ultimately their implementations) in Java/C#.  It's the same song, just a different dance.  Use them to decompose the larger objects and group logically related functionality.  

Wednesday, April 21, 2010

Playing with Ruby

With the 1.0 release of IronRuby I decided to take a brief detour from my prior game plan of outside-in development . Part of that decision was because I'd wanted to check out Ruby for a while as a dynamic language (I've lived in a stuffy, static world for too long) with a lot of hype around it. Part of it was because most of the cutting edge BDD work is happening in the Ruby space. And the last part is that Ruby is in the .NET space now (at least up to 1.8.6) so the barrier for entry couldn't possibly get any lower. Yeah, I know the IronRuby project has been going on for years and prior to that it was dirt simple to get Ruby running on Windows but whatever. I'm here now.

Last year I did manage to pick up the book Beginning Ruby which I, unfortunately, didn't take a shine to. The first handful of chapters were light on the details of Ruby and deferred it for later chapters that I never got to. But that's also my fault because I'm a reference book kind of guy. I just picked up The Ruby Programming Language the other day which totally itches that scratch.

I've been playing around with the interactive Ruby shell which has been fun. It gives me a chance to take a spin before I do anything serious. Now I'm looking to pick up an IDE. I downloaded NetBeans and RubyMine. I'll post back with impressions on both the language and the tools.

Wednesday, October 15, 2008

Too many interests, too little time

I have too many goddam things I want to check out in the world of software development and not nearly enough time to entertain them in any significant fashion.  There are a few tricklets missing or otherwise unpolished from my programming bag of demons that would prove to be valuable to my ability to dispense truth and beauty to the world of code.  Here's the latest group of stuff that I want to dig into:
  • Ruby - Believe it or not I'm beginning to feel constrained in statically typed languages (it's a mostly love and partially hate relationship).  Ruby seems to be all the rage so I want to poke around in it.
  • AOP - I would love nothing more than to break logging out of the components I write.  I know I can abstract them out in one of a billion ways but I'd rather just have it be completely invisible to classes whose responsibilities should be focused on their task and not making sure log4net gets the message.
  • Being a computer scientist - I got so wrapped into the abstraction of software that I stopped thinking about the science of it.  Steve Yegge reminded me just how much rust I've accumulated since graduating.
  • Agile Development - I've read the manifestos and some online documentation but I haven't committed to reading more details or purchasing any publications.  The only reason I'm partially reluctant to start now is that it's a major uphill battle with my current team.  They aren't dumb and they read about this stuff but there's no interest there.  I'm not much on lobbying and I can't train them all (including the role of our manager).
Right now I'm mixed between finding out what AOP offerings there are for .NET and playing in Ruby development.  I want to play with toys instead of read books.