Rails schema migrations… wow!

Managing code versions, unit testing, building releases from a branch,
etc… are generally well understood processes where you’ll find
numerous organizations following similar practices.

Now go to the database schema and every company seems to have a
different home grown aproach to managing the database schema, migrating
changes, restoring older versions, etc.. Also, every solution I’ve ever
seen has involved updating the overall schema for fresh installations
as well as writing a migration SQL script for existing schemas. Not so
with Rails migrations!

I was blown away with this simple and elegant solution where you do the
migration to update an existing schema and then your overall schema
(for new installations) is automatically updated. This video demoing Rails migrations is a must see in my opinion!

Some other interesting reading on the subject: Robby on Rails, Buck Blogs, ScottStuff, API docs, and Gluttonous

Posted in Ruby | Comments Off on Rails schema migrations… wow!

Here's hoping Radrails takes off as a Ruby on Rails IDE

In my
hunt for a good Ruby IDE I’ve tried almost all of the Ruby IDE’s I
could find. After using a variety of them I’ve lately been switching
between Radrails and JEdit with the Ruby plugin.
As a Java programmer I have a natural bias towards Radrails since it’s
based on Eclipse. The future of Radrails is especially promising as
they are planning to work in conjunction with the RDT
project where RDT will provide the core Ruby editing features and
TestUnit support, whereas, Radrails will provide the Rails specific
pieces such as editing RHTML, starting and stopping web servers, an
interface to script/generate, etc…

A really nice IDE is the
one thing I miss most when programming Ruby on Rails so here’s hoping
to the continued success of Radrails! Check it out if you’re looking
for a Ruby IDE, it’s still got a ways to go but it looks very
promising. They have a full installer (if you don’t have eclipse) that
incluces subclipse for subversion support which is a nice touch. If you’d rather watch than try they’ve also started creating videos.

Posted in Ruby | Comments Off on Here's hoping Radrails takes off as a Ruby on Rails IDE

Validation belongs in the domain model, not the MVC framework

In the Java world validation almost always seems to happen in the MVC
framework. For example submitting a form causes some validation to
occur on the form and then if all checks out the domain objects are
populated and saved. I’ve never been crazy about this approach but
Hibernate doesn’t really address the validation issue and most of the
MVC frameworks do, so that’s often the easiest place to put it. The
problem is that most applications have numerous paths to load data
including web services, web pages, bulk data loading tools, etc… and
if your validation is all tied to your MVC framework you can’t easily
reuse it in the non-web paths.

Using Ruby on Rails has convinced me that putting your validation in
the domain model is generally the best approach. With Rails using
ActiveRecord that’s how it’s done and when the object fails to validate
those validation errors can be returned in a web form, data loading
tool, web service, etc… The validation is centralized and just
happens by default. As long as I get the validation right then I don’t
need to worry about another programmer writing a web service to
populate data and all of a sudden we have bad data in the database.

Here’s an example of one of my active record domain model classes with validation built in.

class Entry < ActiveRecord::Base

# Relationships
belongs_to :bliki

# Validation
validates_length_of :name, :within => 6..100
validates_uniqueness_of :name
validates_format_of :name, :with => /^\w+$/,
:message => “cannot contain whitespace”
validates_length_of :content, :minimum => 10
validates_associated :bliki
end

If you know of a good way to add validation to domain model objects in
Hibernate that gets called whenever a save or update is attempted
(perhaps via interceptor) I’d like to hear about it. I’d really like to
find an ActiveRecord style validation approach for Hibernate!

Posted in Software Engineering | Comments Off on Validation belongs in the domain model, not the MVC framework

Cloaking, no need to be ashamed

Cloaking, the process of showing a user one view of your page and a
search engine crawler another view of your page is (I believe) a
fairly common practice that’s been hotly debated in the past and
discouraged by search engines.

In reality I’ve seen sites use cloaking to strip navigation elements,
hide form data, disable multi-page results, etc… to increase search
engine ranking while at the same time decreasing page size for crawlers
and decreasing the number of pages that need to be crawled. Given that
crawlers represent over a 3rd of page views for many high traffic sites,
cloaking has its economic advantages as well.

In my opinion there is nothing wrong with cloaking and it can be
used effectively as long as it’s not abused and the basic underlying
content and concept of a page stay the same. It’s a bad thing when used
to artificially increase keyword density or misrepresent the content of the
page to simply get page views.

I’ve always been a little hesitant to talk about effective cloaking
techniques with colleagues at other companies though. Cloaking
seems to be a dirty secret that most people feel they shouldn’t talk
about. Just observe how most people who work for a website that
economically depends on search engine traffic will publicly deny
cloaking.

Anyhow, I was surfing around with my user agent
set to GoogleBot to see which other sites are engaging in a little
cloakatude. Low and behold
Amazon behaves differently when you send it a user agent of Google Bot.
No search box in the top nav… makes sense… crawlers don’t use
forms anyhow, why spend money on bandwidth to serve them up a form
they’ll just ignore.

I found other major sites as well that were doing similar cloaking.
Not
bothering to serve ad code to known crawlers, removing unnecessary
content, etc… Anyhow it just goes to show that some degree of
cloaking seems to be happening at many major websites. I for one think
it’s fine. Why spend tens of thousands in bandwidth serving up extra
pages or content that’s not relevant to crawlers anyhow?

Posted in Search Engine Optimization | 3 Comments

Rails has some great generate commands and a whole lot more!

If you’ve played with Ruby on Rails you’ve likely used script/generate to scaffold a simple CRUD application (including controller, views, helper, database fixtures, unit test, and functional test) as follows:

ruby script/generate DomainModel Controller

In my side project to learn Ruby I’ve grown very attached to using script/generate to put the rough pieces in place for me and then fill in the details. I really like that unit and functional testing are core to the approach instead of an afterthought or 3rd party library that takes a lot of work to integrate. I write much better functional tests in Ruby because it’s so easy, whereas in Java with the whole httpunit/cactus/cargo approach is a lot more work!

Anyhow, as I started playing with web services and email I was pleasantly surprised to discover I could also use the same simple approach to generate web services using Action Web Service and email integration with Action Mailer as follows. Need email confirmation when a user signs up for a newsletter as well as a system to send the newsletter:

ruby script/generate mailer Newsletter confirmation send

How about a web service to let others get the most viewed blog posts today or for the week:

ruby script/generate web_service BlogService get_top_blog_posts_today get_top_blog_posts_for_week

Need a controller that accesses a web service, why bother with generating client stubs that need to be maintained in source control when you can just start coding against the webservice:

class MyWeatherReport < ApplicationController
web_client_api :weatherforecast, :soap, “http://somesite/forecast/api”

def forecast
@forecast = weatherforecast.get_todays_forecast(@params[‘zipcode’])
end
end

The more I play with Ruby on Rails the more impressed I am. It’s a fairly flexible environment that really seems to have it all but it’s ideal use in my opinion is for greendfield projects that can do it right from the start. If you’re looking at porting your site from C++ to the latest greatest thing but need to keep a lot of old legacy code running against a poorly designed database schema it may not be the right fit.

That said I think Ruby on Rails really is the next great thing. I haven’t seen anything else that provides such a simple, elegant, and comprehensive solution to web development that you can get up and running on so quickly! It has ORM, MVC (supporting components, partials, page decoration), the best domain model validation framework out there, built in and very easy to use AJAX support, unit testing (with database test fixture loading), functional testing, AOP style interception, web services, email integration, you name it… (just don’t name static typing ;-)).

If you’re looking to learn more about Ruby on Rails I can’t say enough good things about Dave Thomas and David Heinemeier Hansson’s Agile Web Development with Rails, it’s one of the few tech books I’ve read cover to cover. It’s so well organized I generally find it quicker to lookup a solution in the book than to hit the web. Needless to say I’m really enjoying learning Rails!

Posted in Ruby | Comments Off on Rails has some great generate commands and a whole lot more!

The pain of redeploying makes for better TDD

Dions thought provoking blog entry on Refactoring tools are fantastic and overrated inspired me to think more in-depth about the deploy/redeploy nature of Java web development. He makes the point that while tools like IDEA have great refactoring, that other factors such as the pain of constantly redeploying in Java containers outweighs the benefits of these tools. I totally agree in theory, however, in practice I've found that the pain of redeploying has a very nice side benefit (at least for me).

Before I go any further let me add that we develop in Tomcat with class reloading enabled so when you change a class you just hit refresh in the browser. However, as Dion points out that doesn't solve non-trivial changes such as configuration changes which do require a Tomcat reload.

Anyhow, to my point, I personally like the pain of redeploying because it forces me to develop using TDD for just about everything up to and including the controller. Then I generally just make minor code tweaks and the HTML changes in a live Tomcat (where a simple browser refresh will give me the feedback). If a bigger code change is involved I go back to TDD to drive the controller or business logic until I get them working as desired.

On the other hand with Ruby and Perl I often find myself starting to get lazy by developing from a page centric point of view because it's much easier to just hit refresh in the browser. It's easy to get carried away by relying on the browser for the feedback, even as deeper business logic changes creep into the picture. When this happens I come back after an hour of coding only to realize I now have a bunch of out of date unit tests that need to be updated and the test coverage is never quite as good then.

Maybe it's just me, but since TDD is so much easier than constantly redeploying I actually practice better TDD under Java than I do in Ruby.

Posted in Java | Comments Off on The pain of redeploying makes for better TDD

Tabbed terminals for Windows redux

As I’ve written about here before, I’m always on the lookout for a good tabbed terminal for Windows that I can use to run multiple cygwin/bash shells locally. Running Screen in my Rxvt or alt-tabbing between 8 terminal windows just doesn’t cut the mustard.

On Linux and FreeBSD I use Konsole or Gnome-Terminal, on Mac OSX I use ITerm, and on straight up Windows there’s nothing! I’ve come to realize that the only way I’m getting a tabbed term under Windows is running XFree86 which has opened up these possibilities for me that work:

1. Mrxvt builds under Cygwin or if you’re lazy like I am you can just download mrxvt.exe here and drop it in your Cygwin path. Thanks for the tip Naresh!
2. Konsole running under KDE-Cygwin is another option but carries with it the footprint of all the other KDE services that need to run. As I discovered the hard way it requires you have all of your Cygwin filesystems mounted in binmode.
3. Gnome-terminal took the most digging to figure out how to get it to work under Cygwin but I finally came across this post. Start Cygwin’s setup.exe, select install from internet, paste in this URL ftp://sunsite.dk/projects/cygwinports/ and click add, hit next, and then select gnome-terminal from the package list and off you go.

These three solutions do the trick for me but, I just wish I didn’t have to run XFree86 to get the benefit. That’s what I really liked about the native Windows port of Rxvt under Cygwin, it’s a great terminal emulator and it doesn’t require XFree86 to run!

Posted in Desktop | 2 Comments

Ruby needs "my" for variable declaration and scoping

I’m still digging Ruby as I plug along over the weekends working on writing a Bliki as an exercise to learn Ruby and Rails.

One lesson many of us learned in the Perl days that seems not to have made it into Ruby was doing a “use strict” to require variable declaration. The biggest benefit being that it required you to declare and scope your variables before you could use them, especially critical in a mod_perl environment. This IMHO is a “very good thing” and saves a lot of headache and troubleshooting to find minor typos.

That said, Ruby does scope your variables nicely for you and is smarter than Perl was about it when not running strict, however, I totally agree with this quote from RubyGarden:


“One thing Perl’s my() gives you is a guarantee that you aren’t stomping on a given variable. For example, if I’m in a block of code in Perl and want to use a private variable I can just declare it with my() and never have to worry about whether a variable of that name existed previously. In Ruby, if you are inside of a block and want a private variable, you need to choose a variable name that has never been used before. I don’t want to have to hold the entire symbol table in my head!! What can I do?”

No language is perfect and just like Java has it’s flaws for my tastes, I’m finding Ruby does also. I wouldn’t be as excited about Ruby as a language if it weren’t for rails, gems, and rake but rails really is what puts it over the edge for me to make it a winning web development platform.

Posted in Ruby | Comments Off on Ruby needs "my" for variable declaration and scoping

Ruby on Rails is my new favorite web stack

I’ve been using Ruby on Rails on and off for personal projects and just finished working my way through Agile Web Development with Rails and Programming Ruby. Before I go on I would like to commend Dave Thomas and David Hansson for what is the most well written book on a web framework that I’ve read to date (and I’ve read a lot). Great work guys!

What Ruby on Rails does best is make web programming easy and quick. It provides a default project structure that’s sensible and ready to go out of the box. With it’s sensible defaults it only requires filling in configuration files when you want to do something out of the norm. It provides seamless integration between the domain layer and MVC layer (including validation), a powerful OR mapping layer (with native support for single table inheritance, trees, ordered lists, etc), provides a great unit and functional testing environment, and above all it allows you to override it’s sensible defaults when you need to do the complicated stuff. In other words it makes the easy things very easy while making the hard things still very doable.

That said there are a few things I don’t like but those have more to wih Ruby the language, than Ruby on Rails:

1. I miss being able to look at a method declaration and see what types of objects it expects.
2. I miss being able to do across the board powerful refactorings in IDEA or Eclipse. With Ruby’s duck typing you just can’t get the level of refactoring in an IDE as you can in a statically typed language.
3. I miss having to explicitly declare a variable because I occasionally make typos later on in the code and Ruby silently creates a new variable for me instead of telling me I’m using an undeclared variable.
4. My only complaint with Rails is that the erb templating language is intrusive just like PHP or JSP, I wish it were more like Tapestry where pages are pure HTML that can be filled with sample data that gets replaced with real data at runtime.

Despite those complaints Ruby on Rails has just barely edged out the mostly open-source Java stack as my favorite web development environment simply because I can work so much faster in it.

Posted in Ruby | Comments Off on Ruby on Rails is my new favorite web stack

Where are all the XHTML and CSS websites?

With browser differences being what they are, I still don't think it's realistic to design a large and heavily trafficked website with validating XHTML and 100% true separation of content from display/layout using CSS. If you disagree prove me wrong by showing me just one site with an Alexa traffic rank of 5000 or better ( that has both validating XHTML on all pages and uses CSS for true separation of content from display. If you can find me one example I'll publicly admit that I was wrong!

As we've been porting our site from Perl to Java we've been looking at making our pages validating XHTML and CSS. The main goal for us was to separate content and design ala CSS Zen Garden which is mostly doable, however, getting this to 100% turned out to be a lot more difficult than I'd initially hoped. There are 3 main challenges we faced when we attempted this:

1. To get the pages to a point where we could guarantee that the pages were well-formed XML. We're using Spring MVC and JSP and fortunately JSPX and TAGX files have to be well formed XML, otherwise they don't build. However, that only gets you 95% there because JSPX likes to collapse XML and many browsers don't like <script … /> and insist upon <script …></script>. So you need to introduce a little CDATA here and there and there goes your guarantee of well formed XML.

2. To get the outputting XHTML to validate with a W3C validator. This is much easier said than done and I think to truly achieve this goal, as part of the build-test process, you need to be able to generate the XHTML for each page and have tests fail if it doesn't validate. If it's not part of the continuous integration testing process it's just too easy to slip in some non-valid XHTML.

3. To to use CSS class's and id's for everything so that your XHTML is just the content of the page and then the CSS fills in all of the look and feel. Ugghh… with very different results for CSS between Safara, IE, and Firefox this can eat up a whole lot of time better spent building more pages that serve your customers than being a slave to cross-browser incompatibilities.

We've found given the state of the major browsers in use that we always shoot for 1, 2, and 3 at the beginning of each new page we create but to keep new development moving forward and from getting derailed in XHTML/CSS/browser compatability land we often need to compromise to get a page working well in a reasonable amount of time on all of the browsers we support.

What's your experience been in trying to move to XHTML and CSS?

Update: A coworker sent me the two links about ESPN and ABCNews and their redesign into heavy CSS use. It's good to see sites moving in this direction, I just wish we could use page validation as a tool to tell us whether our pages are valid or not!

Posted in Web | Comments Off on Where are all the XHTML and CSS websites?