VolumeofCylinder() function, you used the constant 3.14 for the value of
Pi. Ideally, you should define a constant for this. Highlight
right-click, and select Refactor -> Introduce Constant (see Figure 9).
Figure 9. Introducing a constant
Refactor will use a default name (in this
DBL_, based on the data type of the
constant) for the newly added constant (see Figure 10). You can change the
constant to a more meaningful name. In this case, change it to
Figure 10. You can change the name of the constant
Move Declaration Near Reference
Sometimes you won't use a variable that you have declared until several lines after the declaration. To improve readability of your code, it is always better to declare the variables near where they are used.
For example, in the following code segment, the variable
radius is not
immediately used after its declaration. You might want to move the declaration
closer to where it is being used.
Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Dim radius As Single = 3.5 Dim height As Single = 5 '---Volume of Cylinder Dim volume As Single = VolumeOfCylinder(height, radius) End Sub
You can right-click on the declaration statement and then select Refactor -> Move Declaration Near Reference (see Figure 11).
Figure 11. Moving the declaration near the reference
radius variable declaration is now moved to the line before where it is first
Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Dim height As Single = 5 '---Volume of Cylinder Dim radius As Single = 3.5 Dim volume As Single = VolumeOfCylinder(height, radius) End Sub
Split Initialization From Declaration
There may be times where you want to split an initialization statement from declaration (VB2005 allows you to declare and initialize a variable at the same time). To do so, simply right-click on the relevant variable and select Refactor -> Split Initialization From Declaration (see Figure 12).
Figure 12. Splitting initialization from declaration
The code will now become:
Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Dim height As Single = 5 '---Volume of Cylinder Dim radius As Single radius = 3.5 Dim volume As Single = VolumeOfCylinder(height, radius) End Sub
Move Initialization to Declaration
This option is the inverse of the previous option. Instead of splitting the initialization from the declaration, it now moves the initialization into the declaration. Simply right-click on the relevant variable and select Refactor -> Move Initialization To Declaration (see Figure 13).
Figure 13. Moving the initialization to the declaration
Sometimes you have code that looks like this:
If Me.Size.Width > 200 Then ... End If
Me.Size.Width expression is both lengthy and makes debugging difficult if
it is used in more than one place. In this case, it is better to use a local
variable to replace it. To do so, right-click on the variable and select
Refactor -> Introduce Local (see Figure 14).
Figure 14. Introducing a local variable
Your code will now look like this:
Dim lSizeWidth As Integer = Me.Size.Width If lSizeWidth > 200 Then ... End If