为什么不在VB.NET中使用'Return'语句?

Jon*_*an. 1 vb.net return

我已经获得了一些代码来查找可以改进和更改的问题和事情(这是一项家庭作业,但这个问题与任务本身无关),部分代码是:

Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
    Dim InSameCell As Boolean
    InSameCell = False
    If FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth And FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast Then
        InSameCell = True
    End If
    CheckIfSameCell = InSameCell
End Function
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么InSameCell创建变量,只能将其赋值给函数名CheckIfSameCell

或者只使用如下所示的return语句?

Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
    If FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth And FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast Then
        Return True
    End If
    Return False
End Function
Run Code Online (Sandbox Code Playgroud)

我可以理解不If直接在语句中返回表达式,以提高可读性.

我知道为函数名称分配返回值并不会退出函数,而Return会这样做,但它只是一个人的风格,还是第一个版本有任何优势(IMO,第二个版本更具可读性)?

GSe*_*erg 5

也许曾经有过更多的检查,其价值InSameCell可能会改变几次,然后才会被退回.return然后使用会改变行为.

也许作者想要避免繁琐的重复.你知道,当你想重命名一个函数,并且你在它自己的体内多次使用该函数的名字时,你有很多地方要替换,而当你引入一个变量时,你只需要一个地方来改变它的名字. (我知道IDE会为你正确地做到这一点;但在VB6中并非如此,习惯很难打破.)

也许作者对VB6更熟悉了return.

也许这是风格或政策的问题.

无论如何,我会把它写成:

Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
    Return FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth AndAlso FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast
End Function
Run Code Online (Sandbox Code Playgroud)