Refactoring improving the design of existing code (1)

来源:互联网 发布:苹果手机数据流量开关 编辑:程序博客网 时间:2024/06/05 11:23

When you find you have to add a feature to a program, and the program’s code is not structured in a convenient way to add the feature, first refactor the program to make it easy to add the feature, then add the feature.

The First Step in Refactoring
Whenerver I do refactoring , the first step is always the same. I need to build a solid set of tests for that section of code.

Before you start refactoring , check that you have a solid suite of tests. These tests must be self-checking.

Extract Method
For adding HTML statement method. I am looking to decompose the method into smaller pieces. 
When I extract a method , as in any refactoring, I need to know what can go wrong. So I’ve written down the safe steps in the catalog. Pay attention to switch statement or other logical block.
First I need to look in the fragment for any variables that are local in scope to the method we are looking at, the local variables and parameters. Which variables are modified and others not. Any non-modified variable I can pass in as a parameter. Modified variables need more care.

Method is on the wrong object. In most cases a method should be on the object whose data it uses.

Extracting method for header, footer, and detail line are one route I could take. Form Template Method.

原创粉丝点击