4 Mistakes You Make in Code Refactoring

文章指出在重构过程中常见的错误,如无测试更改、将重构混淆为重组、大块重构以及不使用合适的工具。建议始终从测试开始,进行小型、可控的重构,如重命名函数或提取功能,并在当前任务中集成重构。同时,推荐使用强大的IDE,如JetBrains产品,利用其内置的重构工具提高效率。

Over the years, I’ve found many people either complaining the atmosphere they’re living in is not ideal for applying to refactoring or they don’t believe refactoring looks all good in the book but is not practical when you try to make it work in their codebase — because it’s too complicated.

However, I would say you might be doing something wrong when you say refactoring. I understand these challenges, to be honest, it’s not easy to make changes in any setup. And if you avoid the mistakes I’ll put in the article, you should be able to see the benefits of refactoring quickly.

1. Make changes without tests

This is the most common mistake I’ve seen programmers keep making. Maybe it is because you’re making “small” changes which don’t worth testing, or maybe the test setup is difficult, and you’re taking shortcuts to skip the tests.

But the problem is that without it, even tiny changes could lead to some unexpected effects on other parts of the software.

I always start with tests. If there are existing tests, I will read them through to understand how to use the public API of the code I’m going to change, how to prepare the data, what to expect, and so on. If there aren’t any, I’ll add a few to the area I’m going to touch, so I’ll know if anything goes wrong.

2. Restructure instead of refactoring

Just like many terms in software development, refactoring becomes a word that could have many different meanings. People use refactoring when they mean something else, like restructuring, replacing underlying libraries etc.

But if you’ve read Refactoring: Improving the Design of Existing Code, you’ll probably be surprised by how small each refactoring is. For example, Rename VariableRename Function or Extract Function may be something you’ve done on a daily basis.

//before export const convert = (str) => { return str .split("") .map((c) => { const index = letters.indexOf(c.toUpperCase()); return index === -1 ? c : getCharByIndex(index); }) .join(""); };

One possible action here is to extract a function to do the mapping and then call it in map:

//after const translate = (c) => { const index = letters.indexOf(c.toUpperCase()); return index === -1 ? c : getCharByIndex(index); } export const convert = (str) => { return str .split("") .map(translate) .join(""); };

The critical point here is SMALL. Only when it’s small enough, you’re unlikely to make serious mistakes by doing it, and it also means you keep the software in the broken state as short as possible.

Even in the worst case that things are out of control, you can easily revert your changes and back to the working state without too much effort.

3. Dedicated Refactoring Tasks

It’s not uncommon to see something like “Clean up async code for product API” or “Refactor business logic of estimated delivery time” on your story wall or kanban board. And to me, it is not a good sign, I consider it bad because:

  1. It could mean you’re under an unhealthy delivery pressure

  2. It could mean there is some tech debt that keeps adding to the backlog

  3. It could also mean (in the worst case) refactoring the whole amount of code is super risky and can be extremely challenging (without breaking the existing functions)

So instead of having these dedicated refactoring, I suggest you should do it in the task on hand. You can make small refactoring like Rename Function or Extract Function without distracting too much in a task. Because you’re doing it regularly, in the long run, the code will be much easier to modify and add new features.

4. Not using the right tools

I used to try different editors, IDEs and anything in between, and ten years ago, when I started to use IntelliJ (and get familiar with its keymap and so on), I never look back. Before that, I was a big fan of vim, and I have 20+ plugins installed and configured. Whenever I set up a developer environment, I use visual code for causal projects and demos.

But when it comes to serious work, I always choose JetBrains products, whether Interlij, WebStorm or Pycharm, and they are the best. It’s not cheap, but it's worth every cent if you use it correctly (keyboard shortcuts and all the automation). The built-in refactoring tool is smart enough to understand what you want to do and will do that for you. 99% of the time without any issue: it updates all the references in the project/workspace, sync files in the file system, and you just focus on what is important to you as a developer: think!

 https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1a074b8-c106-4ee7-9759-f9ebffbf157e_2314x1586.png 

To make the most of it, you may also want to remember the keymap so applying these well-known refactorings will be effortless. After a few years of practice, I do many refactorings without thinking, and they’re literally on my figure tips.

Summary

So instead of having dedicated time for refactoring, do them when you write normal code, and make it a normal routine. Start from small and try to establish your pace, and run your tests.

这个是完整源码 python实现 Django 【python毕业设计】基于Python的天气预报(天气预测分析)(Django+sklearn机器学习+selenium爬虫)可视化系统.zip 源码+论文+sql脚本 完整版 数据库是mysql 本研究旨在开发一个基于Python的天气预报可视化系统,该系统结合了Django框架、sklearn机器学习库和Selenium爬虫技术,实现对天气数据的收集、分析和可视化。首先,我们使用Selenium爬虫技术从多个天气数据网站实时抓取气象数据,包括温度、湿度、气压、风速等多项指标。这些数据经过清洗和预处理后本研究旨在开发一个基于Python的天气预报可视化系统,该系统结合了Django框架、sklearn机器学习库和Selenium爬虫技术,实现对天气数据的收集、分析和可视化。首先,我们使用Selenium爬虫技术从多个天气数据网站实时抓取气象数据,包括温度、湿度、气压、风速等多项指标。这些数据经过清洗和预处理后,将其存储在后端数据库中,以供后续分析。 其次,采用s,将其存储在后端数据库中,以供后续分析。 其次,采用sklearn机器学习库构建预测模型,通过时间序列分析和回归方法,对未来天气情况进行预测。我们利用以往的数据训练模型,以提高预测的准确性。通过交叉验证和超参数优化等技术手段,我们优化了模型性能,确保其在实际应用中的有效性和可靠性。 最后,基于Django框架开发前端展示系统,实现天气预报的可视化。用户可以通过友好的界面查询实时天气信息和未来几天内的天气预测。系统还提供多种图表类型,包括折线图和柱状图,帮助用户直观理解天气变化趋势。 本研究的成果为天气预报领域提供了一种新的技术解决方案,不仅增强了数据获取和处理的效率,还提升了用户体验。未来,该系统能够扩展至其他气象相关的应用场景,为大众提供更加准确和及时的气象服务。
在E-R图中,常见的错误包括但不限于以下几种: ### 实体和属性的混淆 将本应作为实体的对象错误地定义成了属性,或者反之。例如,在一个学校管理系统中,如果将“学生”作为“班级”的一个属性,而不是一个独立的实体,这就会导致数据建模的不准确。因为学生有自己独立的属性(如姓名、学号等)和行为,应该作为一个实体来处理。 ### 关系定义错误 - **关系类型错误**:没有正确区分一对一、一对多和多对多的关系。比如,在一个图书馆系统中,一本书可能有多个作者,这是一个多对多的关系,如果错误地定义成了一对多关系,就无法准确表示实际情况。 - **关系缺失或多余**:遗漏了实体之间必要的关系,或者定义了不必要的关系。例如,在一个员工管理系统中,遗漏了员工和项目之间的参与关系,就无法准确记录员工参与项目的情况;而如果定义了员工和员工之间的“无关”关系,就是多余的。 ### 参与约束错误 没有正确设置实体在关系中的参与约束(即最小基数约束)。例如,在一个订单系统中,一个订单必须有至少一个商品,这是一个强制性的参与约束。如果错误地设置为可选参与,就会导致数据不一致的问题。 ### 键的定义错误 - **主键选择错误**:没有选择合适的属性作为实体的主键。主键应该是唯一标识实体的属性或属性组合。例如,在一个学生信息系统中,如果选择学生的姓名作为主键,就可能会出现重名的问题,导致无法唯一标识学生。 - **外键使用错误**:在建立关系时,外键的使用不正确。外键应该引用另一个实体的主键,以建立实体之间的关联。如果外键引用了错误的属性,就会导致关系无法正确建立。 ### 图形表示错误 在绘制E-R图时,图形符号使用错误或不规范。例如,没有正确使用矩形表示实体、椭圆形表示属性、菱形表示关系等。或者图形之间的连接不清晰,导致E-R图难以理解。 ```python # 以下是一个简单的Python示例,用于模拟E-R图中的实体和关系 class Entity: def __init__(self, name, attributes): self.name = name self.attributes = attributes class Relationship: def __init__(self, name, entities): self.name = name self.entities = entities # 创建实体 student = Entity("Student", ["Name", "ID", "Age"]) course = Entity("Course", ["CourseName", "CourseID"]) # 创建关系 enrollment = Relationship("Enrollment", [student, course]) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值