Jonathan.
Jonathan.

Reputation: 55544

Why not to use a 'Return' statement in VB.NET?

I've been given some code to go through and find problems and things that could be improved and changed (it's a homework task, but this question is unrelated to the task itself), part of the code is:

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

I can't understand why the InSameCell is variable is created, when it can just be assigned to the function name CheckIfSameCell?

Or just use return statements as in the following?

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

I can understand not returning the expression in the If statement directly, to increase readability.

I know that assigning a return value to the Function name doesn't exit the function, whereas Return does, but is it just a person's style, or is there any advantage to the first version (IMO, the second is more readable)?

Upvotes: 1

Views: 12072

Answers (6)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112342

Assigning the result to the function name is an old style used in VB6 and should not be used any more in VB.NET. Use Return value!

Personally I dislike statements in the style

If condition Then
    Return True
Else
    Return False
End If

They are just stupid, since condition already yields the return value! Better:

Return condition

It is also the solution chosen by GSerg.


Nobody would write

If x + y = 0 Then
    Return 0
ElseIf x + y = 1 Then
    Return 1
ElseIf x + y = 2 Then
    Return 2
ElseIf x + y = 3 Then
    Return 3
...

But some people are constantly doing it when the expression is of type Boolean. I think that they do not realize that conditions are equivalent to arithmetical expressions. They are just arithmetic with Booleans instead of arithmetic with numbers.

Another misconception is that an If-statement requires some comparison like If x > 0 Then. If they have a Boolean variable b they write If b = True Then. But all the If-statement needs is a Boolean value given by a Boolean expression. This expression can be as simple as querying a variable: If b Then.

Why does this work? Because if b is True then b = True yields True and if b is False then b = True yields False. So, b = True is very much like saying x * 1. Of course, this is the same as just x.

Upvotes: 4

David Wilson
David Wilson

Reputation: 4439

In short - Yes your last example is quite valid.

However, most examples used in homework are either used to show other teaching examples. The code in the homework sheet merely shows the basics of using functions in the traditional way and your 2nd example shows the next learning step and is the most compact way of achieving the desired result.

Also, the 1st example could also be used to re-enforce lessons learned earlier - e.g. about assigning variables, use of booleans etc.

One of the best ways to improve your coding skills is to repeatedly practice what you have learned.

Upvotes: 0

Dwayne Roberts
Dwayne Roberts

Reputation: 1

You might use a variable if for some reason it needs to appear on the right-hand side of an assignment, and you don't want to cause a recursion:

Dim Function F() As Boolean
    F = True
    If a = b Then 
        F = Not F()
    End If
End Function

Upvotes: 0

pkoszka
pkoszka

Reputation: 104

There is one important thing with return and assigning value the the function name. If you (for whatever twisted reason) would like to write something like that

Public Function TestFunct() as Boolean
  Dim testVar as Boolean = True
  If testVar then
   TestFunct = True
  Else
   TestFunct = False
  EndIf
  'do more stuff here 
  ...
  TestFunct = False
End Function

It will always return false. If you use returns instead it the execution will stop and the function will return correct value.

Upvotes: 0

Matt
Matt

Reputation: 14531

The second method is more readable, I concur. It also happens to be my preference for returning out of methods. I really cannot think of a single downside to the latter in comparision, but can for the former. What happens if the method gets longer and someone forgets to set a Boolean flag? A subtle bug would be born. Additionally, it takes more code to write as well. In the latter approach, the code won't compile if it is missing a return, and it also will be shorter.

The only time you need local variables for the return type is when the routine needs to do some other work after the return value is first determined. In the example you post, this is not the case.

Code Complete, 2nd Edition agrees on page 391:

Use a return when it enhances readability In certain routines, once you know the answer, you want to return it to the calling routine immediately. If the routine is defined in such a way that it doesn’t require any further cleanup once it detects an error, not returning immediately means that you have to write more code.


NOTE: As other answers [1,2] have mentioned, you can reduce the method to a single code statement. Also using AndAlso should help speed up the evaluation by short-circuiting the logical expression early if the first part is false:

Return FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth 
    AndAlso FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast

Upvotes: 2

GSerg
GSerg

Reputation: 78165

Maybe there used to be more checks, where value of InSameCell could change several times and only then get returned. Using return then would change behaviour.

Maybe the author wanted to avoid the tedious renaiming. You know, when you want to rename a function, and you use that function's name many times within its own body, then you have many places to replace, whereas when you introduce a variable you will only have one place to change the name in. (I know the IDE will properly do that for you; but that was not the case in VB6, and habits are difficult to break.)

Maybe the author was much more familiar with VB6 that didn't have return.

Maybe it was a matter of style or policy.

Anyway, I would write it as:

Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
    Return FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth AndAlso FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast
End Function

Upvotes: 5

Related Questions