Code refactoring is the key

After six month of development and a first release we decide to
refactor our application. Since it was my first Ruby/Rails application
there are a lot of horrible things inside it.

And now? Some general hints?

I start with 2 simple questions:
1 - Where I put some common methods? Inside application.rb
(controller) is ugly…
2 - In order to make skinny controller I write a lot of class method
inside the model, maybe too much? Is this a common practice?

On Tue, Dec 30, 2008 at 11:39 AM, vlain [email protected] wrote:

2 - In order to make skinny controller I write a lot of class method
inside the model, maybe too much? Is this a common practice?

I’ll chime in with the obvious
I would start by making sure that the application passes all of your
tests.
If you have have not done this yet, be prepared for a lot of work. Then
be
prepared for more work as you write tests to cover everything you’ve
written.

–wpd

You can break up common methods and place them in modules. Then simply
“include” the module in ApplicationController (or any other set or sub
set of controllers as needed).

vlain wrote:

After six month of development and a first release we decide to
refactor our application. Since it was my first Ruby/Rails application
there are a lot of horrible things inside it.

Next time, use TDD and refactoring as you go. You would have shipped
much
sooner, with fewer bugs.

And now? Some general hints?

If you do not have unit tests (or “functional” tests, which are really
just unit
tests with a suggestive name), then do this:

  • start a new Rails project
  • pick the most important single tiny feature in the old app
  • use TDD and refactoring to implement it int the new app
  • use the old app as a cheat sheet, but refrain from copying code out
  • repeat with each feature until done.

You will have a much cleaner application, with an exemplary design.

I start with 2 simple questions:
1 - Where I put some common methods? Inside application.rb
(controller) is ugly…

Refactor low hanging fruit. If your common methods have redundant lines
that
only talk about models, move them to models. Eventually your common
methods will
DRY up as if by themselves. Find the easiest possible refactors first -
never
attempt the hard ones while a bunch of easy ones are in the way.
Eventually the
hard ones will take care of themselves.

2 - In order to make skinny controller I write a lot of class method
inside the model, maybe too much? Is this a common practice?

Yes - your models should fill up with lots of tiny methods, all
apparently useless.


Phlip