2014-06-16 64 views
0

我正在研究一個非常大的函數中的一些舊代碼,並且我需要編寫一個新函數以便從舊函數中調用幾次。這個新功能將提供有關在舊功能早期是否需要Return的信息。重構一個可能返回函數的函數

我的問題是什麼是更直接或更好的方式來完成下面的內容?我如何重構這個?

我想另一種方式要問是什麼更好的方式Return a Return

Public Class ExampleClass 

''' <summary> 
''' This function calls another function 
''' </summary> 
''' <returns></returns> 
Protected Overridable Function FunctionOne() As Boolean 
    FunctionOne = False 
    Dim lobjOne, lobjTwo, lobjThree As Object 
    Dim lblnExit As Boolean = False 
    ' 
    ' Some logic here (manipulates/gets objects) 
    ' 
    lblnExit = FunctionTwo(lobjOne, lobjTwo) 
    If lblnExit Then 
     Return lblnExit 
    ElseIf lobjOne.This.That > 2 Then 
     Return lblnExit 
    End If 
    ' 
    ' Some more logic here (manipulates objects) 
    ' 
    lblnExit = FunctionTwo(lobjOne, lobjTwo) 
    If lblnExit Then 
     Return lblnExit 
    ElseIf lobjOne.This.That > 2 Then 
     Return lblnExit 
    End If 
    ' 
    ' Performing some final actions 
    ' 
End Function 


''' <summary> 
''' This function is called by FunctionOne Multiple Times 
''' </summary> 
''' <returns></returns> 
Protected Overridable Function FunctionTwo(ByVal pobjOne As Object, ByVal pobjTwo As Object) As Boolean 
    FunctionTwo = False 
    ' 
    ' Performing some long complicated checking that either Returns true or exits 
    ' 
End Function 

End Class 
+0

也許這個問題屬於這裏http://codereview.stackexchange.com/? – HengChin

回答

1

你可以通過封裝FunctionTwo呼叫避免重複的條件邏輯和導致lambda表達式檢查:

Protected Overridable Function FunctionOne() As Boolean 
    FunctionOne = False 
    Dim lobjOne, lobjTwo, lobjThree As Object 
    Dim lblnExit As Boolean = False 
    Dim functionTwoEx = 
     Function() 
      lblnExit = FunctionTwo(lobjOne, lobjTwo) 
      Return lblnExit OrElse lobjOne.This.That > 2 
     End Function 
    ' 
    ' Some logic here (manipulates/gets objects) 
    ' 
    If functionTwoEx() Then 
     Return lblnExit 
    End If 
    ' 
    ' Some more logic here (manipulates objects) 
    ' 
    If functionTwoEx() Then 
     Return lblnExit 
    End If 
    ' 
    ' Performing some final actions 
    ' 
End Function 

這是否是更好的將取決於你的實際的檢查是多麼複雜,以及如何有意義的你可以使functionTwoEx的名字。

1

您可以簡化一下條件邏輯。

lblnExit = FunctionTwo(lobjOne, lobjTwo) 
If lblnExit Or lobjOne.This.That > 2 Then 
    Return lblnExit 
End If