大规模重构的策略 [英] Strategy for large scale refactoring

查看:46
本文介绍了大规模重构的策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一段代码,其中逻辑和数据访问都存在于 GUI 类中.显然,我想改善这种情况.

I'm currently working in a piece of code where both logic and data access are present in the GUI classes. Obviously, I would like to improve on this situation.

目前的结构基本上是:

  • 大泥球

最终目标是实现类似 DDD 的结构:

The ultimate goal is to achieve a DDD-like structure:

  • DAL
  • 领域模型
  • 服务层
  • 演示模型
  • 图形界面

那么,你会如何解决这个问题?

So, how would you attack the problem?

  • 大爆炸
    • 定义最终状态的结构并将代码推送到其最终位置.
    • 尝试将大泥球分成两部分.重复直到完成...

    推荐答案

    永远不要尝试大爆炸".它几乎总是打在你的脸上,因为当其他一切都失败时,这是一种高风险、绝望的措施.

    Never attempt "Big Bang". It almost always blows in your face, since it's a high-risk, desperate measure when everything else has failed.

    分而治之:这很有效……如果你的世界只有两个方面.在真正的软件中,你必须同时征服这么多条战线,你很少能生活在黑白幻想中.

    Divide and conquer: This works well ... if your world has only two sides. In real software, you have to conquer so many fronts at the same time, you can rarely afford to live in a black-white fantasy.

    我想在我职业生涯的大部分时间里,我一直在使用诸如扼杀"之类的东西:逐渐将糟糕的旧代码转变为闪亮的新代码.这是我的食谱:

    I guess I've been using something like "Strangling" for most of my career: Gradually morphing bad old code into shiny new code. Here is my recipe:

    从某个地方开始,从哪里开始并不重要.编写一些单元测试以查看代码的实际行为.找出它做你认为它做的事情的频率,以及它不做的频率.使用您的 IDE 重构代码,以便您可以对其进行测试.

    Start somewhere, it doesn't really matter where. Write a few unit tests to see how to the code really behaves. Find out how often it does what you think it does and how often it doesn't. Use your IDE to refactor the code so you can test it.

    在第一天之后,猜一猜你是否从正确的地方开始拆开这个怪物.如果是这样,请继续.如果没有,那就找个新地方重新开始.

    After the first day, make a guess whether you've started at the right place to take this monster apart. If so, go on. If not, find a new place and start over.

    这种策略的优点:它以小步骤工作,因此可以控制风险,如果出现问题,如果必须在上周您一直在处理的代码中.

    Advantages of this strategy: It works in small steps, so the risk can be kept in check and if something breaks, if has to be in the code you've been working on last week.

    缺点:这需要很多时间,你会感到沮丧,因为通常情况下,进展看起来很慢,直到结"突然弹出,一切都开始变得像魔法一样.

    Disadvantage: It takes a whole lot of time and you will feel frustrated because often, progress will just seem so slow until the "knot" pops and suddenly, everything starts fall into place as if by magic.

    这篇关于大规模重构的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆