# 揭示常见的重构误区

| 作者 Danijel Arsenovski 0 他的粉丝 ，译者 张逸 12 他的粉丝 发布于 2008年11月5日. 估计阅读时间: 35 分钟 | CNUTCon 了解国内外一线大厂50+智能运维最新实践案例。

## 重构会导致性能低下

Option Explicit On
Option Strict On

Imports System.Diagnostics
Imports System.Data.SqlClient

Namespace RefactoringInVb.Chapter9

Structure Point
Public X As Double
Public Y As Double
End Structure

Module CircleCircumferenceLength

Sub Main()
Dim center As Point
Dim pointOnCircumference As Point
Console.WriteLine("Enter X coordinate" + _
"of circle center")
Console.WriteLine("Enter X coordinate" + _
"of circle center")
'read some point on circumference coordinates
Console.WriteLine("Enter X coordinate" + _
"of some point on circumference")
Console.WriteLine("Enter X coordinate" + _
"of some point on circumference")
'calculate and display the length of circumference
Console.WriteLine("The lenght of circle" + _
"circumference is:")
'calculate the length of circumference
Dim lengthOfCircumference As Double
Dim i As Integer
'use stopWatch to measure transcurred time
Dim stopWatch As New Stopwatch()
stopWatch.Start()
'repeat calculation for more precise measurement
For i = 1 To 10000
Dim connection As IDbConnection = New SqlConnection( _
"Data Source=TESLATEAM;" + _
"Initial Catalog=RENTAWHEELS;" + _
connection.Open()
Dim command As IDbCommand = New SqlCommand( _
"SELECT GETDATE()")
command.Connection = connection
connection.Close()
radius = ((pointOnCircumference.X - center.X) ^ 2 + _
(pointOnCircumference.Y - center.Y) ^ 2) ^ (1 / 2)
lengthOfCircumference = 2 * 3.1415 * radius
Next
stopWatch.Stop()
Console.WriteLine(stopWatch.Elapsed)
Console.WriteLine(lengthOfCircumference)
End Sub
End Module
End Namespace

Option Explicit On
Option Strict On

Imports System.Data.SqlClient

Namespace RefactoringInVb.Chapter11
Public Structure Point
Public X As Double
Public Y As Double
End Structure

Module CircleCircumferenceLength

Sub Main()
Dim circle As Circle = New Circle
circle.Center = InputPoint("circle center")
circle.PointOnCircumference = InputPoint( _
"point on circumference")
Console.WriteLine("The length of circle " + _
"circumference is:")
Dim circumference As Double
Dim i As Integer
'use stopWatch to measure transcurred time
Dim stopWatch As New Stopwatch()
stopWatch.Start()
'repeat calculation for more precise measurement
For i = 1 To 10000
circumference = circle.CalculateCircumferenceLength()
Next
stopWatch.Stop()
Console.WriteLine(stopWatch.Elapsed)
Console.WriteLine(circumference)
WaitForUserToClose()
End Sub

Public Function InputPoint(ByVal pointName As String) As Point
Dim point As Point
Console.WriteLine("Enter X coordinate " + _
"of " + pointName)
Console.WriteLine("Enter Y coordinate " + _
"of " + pointName)
Return point
End Function

Private Sub WaitForUserToClose()
End Sub
End Module
Public Class Circle
Private centerValue As Point
Private pointOnCircumferenceValue As Point
Public Property Center() As Point
Get
Return centerValue
End Get
Set(ByVal value As Point)
centerValue = value
End Set
End Property
Public Property PointOnCircumference() As Point
Get
Return pointOnCircumferenceValue
End Get
Set(ByVal value As Point)
pointOnCircumferenceValue = value
End Set
End Property

Public Function CalculateCircumferenceLength() As Double
QueryDatabase()
Return 2 * 3.1415 * CalculateRadius()
End Function

Return ((Me.PointOnCircumference.X - Me.Center.X) ^ 2 + _
(Me.PointOnCircumference.Y - Me.Center.Y) ^ 2) ^ (1 / 2)
End Function

Private Sub QueryDatabase()
Dim connection As IDbConnection = New SqlConnection( _
"Data Source=TESLATEAM;" + _
"Initial Catalog=RENTAWHEELS;" + _
connection.Open()
Dim command As IDbCommand = New SqlCommand( _
"SELECT GETDATE()")
command.Connection = connection
connection.Close()
End Sub
End Class
End Namespace

## 关于作者

Danijel Arsenovski是Wrox出版的《Professional Refactoring in Visual Basic》一书的作者。目前，他就职于Excelsys S.A（该公司为地区内的大量客户设计网上银行解决方案），担任产品和解决方案架构师。他对于重构的最初体验来自于对大型银行系统的整改， 从此，他就迷上了重构。他首创了利用重构完成代码从VB 6到VB.NET的升级。Arsenovski还是多个主要出版商的丛书作者，拥有微软认证解决方案开发专家（MCSD，Microsoft Certified Solution Developer）证书，并在2005年被提名为Visual Basic MVP。你可以通过电子邮件danijel.arsenovski@empoweragile.com与他取得联系，他的博客是 http://blog.vbrefactoring.com

## 获得来自InfoQ的更多体验。

### 告诉我们您的想法

* 这部分功能能用的，有些用户正在使用。新的功能尽量不碰这里就行了。
* 现在用户要赶着上线，没时间重构
* 这个结构变了好多代码要修改，大家撑不住了
* 拼音缩写命名也可以嘛，多熟悉几天这个部分你就都看得懂了

Re: 好文章

Re: 重构之难 by X 1073

6 讨论