Daily Refactor #38: Extract Method in StuffToDo

Since I’m planning to release an update to my StuffToDo plugin next, I decided it could use a week of refactoring in order to clean it up.

The Refactoring

Since this plugin doesn’t have a lot of code (349 lines), there aren’t as many code smells as in the Redmine core. But according to Caliper there are still a few sections that are worse than the community averages. Starting with my trusted friend flay, I found this refactoring.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# app/models/stuff_to_do.rb
class StuffToDo  '0',
    'Only Issues' => '1',
    'Only Projects' => '2'
  }
 
  def self.using_projects_as_items?
    using = USE.index(Setting.plugin_stuff_to_do_plugin['use_as_stuff_to_do'])
    using == 'All' || using  == 'Only Projects'
  end
 
  def self.using_issues_as_items?
    using = USE.index(Setting.plugin_stuff_to_do_plugin['use_as_stuff_to_do'])
    using == 'All' || using  == 'Only Issues'
  end
end

After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class StuffToDo  '0',
    'Only Issues' => '1',
    'Only Projects' => '2'
  }
  def self.using_projects_as_items?
    use_setting == 'All' || use_setting  == 'Only Projects'
  end
 
  def self.using_issues_as_items?
    use_setting == 'All' || use_setting  == 'Only Issues'
  end
 
  private
 
  def self.use_setting
    USE.index(Setting.plugin_stuff_to_do_plugin['use_as_stuff_to_do'])
  end
end

Review

This alone wasn’t a big enough refactoring for flay, it still sees #using_projects_as_items? and #using_issues_as_items? as similar enough code to flag. I already have the next refactoring planned which should remove that duplication. Do you have an idea of what it could be? Post your idea to the comments below.

Reference commit

Share

  • Facebook
  • Twitter
  • HackerNews
  • Reddit
  • Tumblr
  • Delicious
  • Email
  • RSS

Related posts:

  1. Redmine Refactor #140: Extract Method WikiController#edit to #update
  2. Redmine Refactor #118: Split Edit Method in NewsController
  3. Redmine Refactor #88: Extract Method from move and perform_move
  4. Daily Refactor #63: Extract Method to before_filter in IssuesController
  5. Daily Refactor #47: Extract Method for Kanban panes – Part 3

About Eric Davis

I founded Little Stream Software where I provide Redmine and ChiliProject services to help projects teams. I also created an ebook, Redmine Tips, were I show you how to become more productive using Redmine. I am also the author of Refactoring Redmine, where I go about refactoring Rails using Redmine as an example.

, , ,

Chirk HR     Reuse your existing job applicants »
WP Socializer Aakash Web