官术网_书友最值得收藏!

When Should I Refactor?

It's already been mentioned that refactoring is best done in smaller chunks rather than in big sweeping changes.

Again, this avoids riskier and error-prone changes. You can also ship your product to your customers in a more iterative way and get feedback from your users earlier.

Then, the question arises: When should I refactor?

The Boy Scout Rule

There's a programming principle called The Boy Scout Rule (http://www.informit.com/articles/article.aspx?p=1235624&seqNum=6) which states:

"Leave the code cleaner than you found it."

This applies whether you are fixing a bug, adding a new feature, and so on. Just do something that improves the code you are working with.

Note

It could be as simple as adjusting the name of an ambiguous variable name.

It could be reorganizing the structure of your code's files.

It could be introducing design patterns to solve specific issues.

Repetitive Work

Ever run into a situation where you need to write code that you've already written before? Maybe the same code already exists somewhere else in the system?

If you find that you've had to write the same (exact) code over and over, then you might want to consider consolidating that code into a shareable resource.

That way, you don't have to change 12 different files to fix a bug or add a new piece of logic.

Difficulty Adding Features

As we mentioned previously, sometimes, you are faced with the task of adding new features to your code. However, perhaps the existing code is making it really difficult to just add that new feature or behavior.

In these cases, refactoring can help you mold the existing code into a place where it is easy to add new behaviors!

Note

This also applies to architectural issues. However, this book will focus on more code-based issues.

In the End

In the end, refactoring should be a continuous and habitual activity that you have chosen to engage in.

Martin Fowler, on the topic of refactoring (https://www.refactoring.com/), said:

"Refactoring isn't a special task that would show up in a project plan. Done well, it's a regular part of programming activity."

You shouldn't have to ask to refactor. Just do it when it makes sense.

主站蜘蛛池模板: 鹤山市| 兴国县| 土默特左旗| 银川市| 博湖县| 山丹县| 美姑县| 偃师市| 望奎县| 乌拉特中旗| 邢台县| 尼玛县| 奉贤区| 西安市| 安乡县| 梨树县| 上高县| 江华| 赤壁市| 晴隆县| 东山县| 济源市| 贡嘎县| 库尔勒市| 军事| 安徽省| 三亚市| 昌平区| 岳池县| 那曲县| 拉萨市| 武隆县| 沙坪坝区| 伊通| 陕西省| 叙永县| 九龙县| 德保县| 卫辉市| 土默特右旗| 肇东市|