DRY - 多行代码执行相同的更新功能

发布于 2024-12-08 13:25:12 字数 990 浏览 0 评论 0原文

这是我的代码的一个简短示例:

def update_records!

  # Teams.
  home_team_record = PoolRecord.for_recordable_and_user(event_home_team, user)
  home_team_record.update_for!(self)

  away_team_record = PoolRecord.for_recordable_and_user(event_away_team, user)
  away_team_record.update_for!(self)

  # Division(s).
  home_team_div_record = PoolRecord.for_recordable_and_user(event_home_team_division, user)
  home_team_div_record.update_for!(self)

  # Create/update PoolRecord for away_team division if they're in a different division.
  unless event_away_team_division == event_home_team_division
    away_team_div_record = PoolRecord.for_recordable_and_user(event_away_team_division, user)
    away_team_div_record.update_for!(self)
  end

  # User.
  user_record = PoolRecord.for_recordable_and_user(user, user)
  user_record.update_for!(self)

end

如果不是需要检查away_team 部门的条件,那么干燥此代码实际上会相当简单。我可以创建一个传入的第一个参数的字符串数组,然后您使用 Object#send。但是,正如我所说,我需要检查一种情况下的条件。您如何建议干燥这个?

Here's a short sample of my code:

def update_records!

  # Teams.
  home_team_record = PoolRecord.for_recordable_and_user(event_home_team, user)
  home_team_record.update_for!(self)

  away_team_record = PoolRecord.for_recordable_and_user(event_away_team, user)
  away_team_record.update_for!(self)

  # Division(s).
  home_team_div_record = PoolRecord.for_recordable_and_user(event_home_team_division, user)
  home_team_div_record.update_for!(self)

  # Create/update PoolRecord for away_team division if they're in a different division.
  unless event_away_team_division == event_home_team_division
    away_team_div_record = PoolRecord.for_recordable_and_user(event_away_team_division, user)
    away_team_div_record.update_for!(self)
  end

  # User.
  user_record = PoolRecord.for_recordable_and_user(user, user)
  user_record.update_for!(self)

end

DRY'ing this code would actually be fairly straightforward if it weren't for the condition that needs to be checked for the away_team division. I could create create a string array of the first params passed in and you Object#send. However, like I said, I need to check a condition in one scenario. How would you recommend DRY'ing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向日葵 2024-12-15 13:25:13

一个简单的助手将减少噪音:

def update_records!
    update_one(event_home_team)
    update_one(event_away_team)
    update_one(event_home_team_division)
    update_one(event_away_team_division) unless event_away_team_division == event_home_team_division
    update_one(user)
end

private

def update_one(team)
    PoolRecord.for_recordable_and_user(team, user).update_for!(self)
end

如果出于某种原因你想采取另一步骤,你可以这样做:

def update_records!
    [
        [ event_home_team,          true ],
        [ event_away_team,          true ],
        [ event_home_team_division, true ]
        [ event_away_team_division, event_away_team_division != event_home_team_division ],
        [ user,                     true ]
    ].each do |team, do_it|
        if(do_it)
            PoolRecord.for_recordable_and_user(team, user).update_for!(self)
        end
    end
end

或者,根据你的数据,这可能会起作用:

def update_records!
    [
        event_home_team, 
        event_away_team,
        event_home_team_division, 
        event_away_team_division,  
        user
    ].uniq.each do |team|
        PoolRecord.for_recordable_and_user(team, user).update_for!(self)
    end
end

这最后一个替换了单个除非使用简单的 uniq 过滤器在原始文件中执行 a == b 条件。

我不知道您的数据的确切属性,也不知道您想将其发展到什么程度,所以我提供了一些想法。我认为最后一个最接近“说出你的意思”,但也许不是。

A simple helper will cut down on the noise:

def update_records!
    update_one(event_home_team)
    update_one(event_away_team)
    update_one(event_home_team_division)
    update_one(event_away_team_division) unless event_away_team_division == event_home_team_division
    update_one(user)
end

private

def update_one(team)
    PoolRecord.for_recordable_and_user(team, user).update_for!(self)
end

And if for some reason you want to take it another step you could do something like this:

def update_records!
    [
        [ event_home_team,          true ],
        [ event_away_team,          true ],
        [ event_home_team_division, true ]
        [ event_away_team_division, event_away_team_division != event_home_team_division ],
        [ user,                     true ]
    ].each do |team, do_it|
        if(do_it)
            PoolRecord.for_recordable_and_user(team, user).update_for!(self)
        end
    end
end

Or, depending on your data, this might work:

def update_records!
    [
        event_home_team, 
        event_away_team,
        event_home_team_division, 
        event_away_team_division,  
        user
    ].uniq.each do |team|
        PoolRecord.for_recordable_and_user(team, user).update_for!(self)
    end
end

This last one replaces the single unless a == b condition in your original with a simple uniq filter.

I don't know the exact properties of your data or how far you want to take this so I've offered a few ideas. I think the last one is the closest to "say what you mean" but maybe not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文