tags:

views:

196

answers:

6

Ok, I was rooting around in one of our company apps which is done in VB.net. I'm not familiar with VB.net (I do stuff in C#) so I'm asking this question: Does the code after the clean up comment execute?

Public Function DoesUserHavePermission(ByVal UserID As Integer, ByVal ActionID As Integer) As Boolean
    ' some extra code re: getting data


     Return UserHasPermission

     '-Clean Up-
     MySqlCommand.Dispose()
     MySqlConnection.Dispose()
     RowCount = Nothing


End Function

It is my understanding once you say return, you give the calling function control again. Is this a VB.Net oddity which I have to accept or a giant WTF?

+4  A: 

The statements after the Clean up comment will not execute. This is a candidate for enclosure within Try/Catch/Finally.

RandomNoob
or just Try/Finally
RandomNoob
That's what I thought. VB.net is just odd enough to where I had to ask the question.
KeithA
or `Using` if the variables are local
MarkJ
Keith, VB.Net isn't odd. It behaves in exactly the same way as C#, and suffers the exact same problems as C# when programmed by a drivelling buffoon.
Jules
+1  A: 

Not unless there's some control logic you omitted in your example

Ian Jacobs
A: 

The code should be (the clean up that is) wrapped inside of a finally statement by using a try catch exception:

pseudo:

try
  'make conn
catch exception
finally
mysqlCmd.Dispose
....
end try

Is it possible it will still run..possibly...I used to write VB.net and it has been quite some time but I do remember oddities like that. I can't give you a sure answer as this is / was very bad practice. What you can do is clean it up and set some break points in y our code and debug. See if the code comes back to it...

JonH
A: 

That to me is a giant WTF and a very difficult thing to miss, normally peer code review can catch that, I do not understand why a return from the function is placed earlier on prior to the cleanup. That could have been done during the testing of the code where the programmer was wanting to test the function first and decided to ignore the cleanup code by returning from it quickly enough, I suspect the cleanup code could be lengthy, ie. maybe it is throwing an exception that is not properly caught and wanted to ignore it hence a need to return immediately...That is an (un) intentional side effect of introducing a leak as the resource is not cleaned up properly thereby masking the real problem...that's my take on it.

Hope this helps, Best regards, Tom.

tommieb75
A: 

Short answer: The code below the return will never execute.

That looks like translated code. Like someone took a C# snippet from the web and tried to write it in VB for VS 2003 (before VB supported the USING statement, but while C# did).

where the MySqlConnection and MySqlCommand are new'd up should be put in USING blocks and the Dispose() lines turned into END USING.

Where possible, use USING over TRY/FINALLY to ensure cleanup of IDisposable objects.

using mySqlConnection as New SqlConnection(connectionString)
    mySqlConnection.Open
    using mySqlCommand as New SqlCommand(commandString, mySqlConnection)
       'do something that may fail'

       return UserHasPermission

    end using 'disposes mySqlCommand'
end using 'closes/disposes mySqlConnection'

You can use this pattern for SqlTransactions too. (Place after mySqlConnection.Open)

using myTerribleVariableName as SqlTransaction = mySqlConnection.BeginTransaction
    'do something that may fail - multiple SqlCommands maybe'
    'be sure to reference the transaction context in the SqlCommand'

    myTerribleVariableName.Commit
end using 'disposes. rollsback if not commited'

Oh, and you can delete the RowCount = Nothing statement.

Hafthor
A: 

The code as presented will not do anything after the return statement. VB.NET and C# are similar in that fashion.

Its possible that this code was written by a VB6 programmer, trusting in old paradigms, or possibly this was the work of the upgrade tool, porting code from VB6 to VB.NET.

Jonathan Bates
Is that a VB6 feature? Nope, this is new code base. But the programmer responsible is from a VB6 background.
KeithA
Yeah, you could get away with that in VB6. I don't think it was a good idea then either.
Jonathan Bates
@Jonathan You couldn't get away with that in VB6. Unreachable code does not execute. Although VB6 has deterministic finalisation, so there's sometimes not the same need to dispose of resources.
MarkJ