1

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)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • 2
    I thinks its just personal choice and not a rule and also some programmers follow pattern what they read from books or online. I also prefer second code (and i write my code in this style), it is easy to read and uses one less variable. – rs. May 01 '12 at 17:05
  • 1
    your suggestion is better and the proper way, by using `return` instead of using the function name. The previous function might have been written in VB6 and then converted to .Net as VB6 was coded like that – Nadeem_MK Aug 25 '14 at 10:18
  • Back when the switch was made to structured programming, one rule was that [each function should have a single exit point](http://c2.com/cgi/wiki?SingleFunctionExitPoint). Citing from the linked page, _"Now that structured programming has long since won the day, no one particularly cares about that anymore"_. – stakx - no longer contributing Aug 25 '14 at 10:20
  • See also these SO questions: [Why should a function have only one exit point?](http://stackoverflow.com/questions/4838828/why-should-a-function-have-only-one-exit-point) and [Should a function have only one return statement?](http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement). – stakx - no longer contributing Aug 25 '14 at 10:21
  • For anyone still finding this antique The only valid reason for that "mess" is someone used to VB6 (or code upgraded from it), `InSameCell `is not needed, `If` is not needed, should just return the value, also `And` should be replaced with `AndAlso`, and remove `ByVal` while at it – NiKiZe Jun 23 '20 at 02:04

6 Answers6

5

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
GSerg
  • 76,472
  • 17
  • 159
  • 346
4

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.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
2

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
Community
  • 1
  • 1
Matt
  • 14,353
  • 5
  • 53
  • 65
0

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.

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
pkoszka
  • 104
  • 2
0

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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.

David Wilson
  • 4,369
  • 3
  • 18
  • 31