# Code refactoring is the key

**URL:** <https://www.ruby-forum.com/t/code-refactoring-is-the-key/156595>\
**Category:** Rails\
**Created:** [December 30, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/code-refactoring-is-the-key/156595 "2008-12-30T17:39:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vlain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@vlain](https://www.ruby-forum.com/u/vlain)\
**Post date:** [December 30, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/code-refactoring-is-the-key/156595/1 "2008-12-30T17:39:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vlain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@vlain](https://www.ruby-forum.com/u/vlain)\
**Post date:** [December 30, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/code-refactoring-is-the-key/156595/2 "2008-12-30T17:49:00Z")

</div>

On Tue, Dec 30, 2008 at 11:39 AM, vlain [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![vlain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@vlain](https://www.ruby-forum.com/u/vlain)\
**Post date:** [December 30, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/code-refactoring-is-the-key/156595/3 "2008-12-30T17:51:58Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![vlain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@vlain](https://www.ruby-forum.com/u/vlain)\
**Post date:** [December 30, 2008, 7:13pm UTC](https://www.ruby-forum.com/t/code-refactoring-is-the-key/156595/4 "2008-12-30T19:13:05Z")

</div>

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
