3

I write a lot of VB.net web services that utilize Entity Framework & LINQ to return JSON data. Many times for shorthand I just return the data directly from inside the Using statement.

I can't help but wonder, is there any advantage to storing the return value and returning it outside of the Using statement?

The MSDN documentation on Using states:

" the Using block guarantees disposal of the resources, no matter how you exit the block. "

So I think this is just as acceptable, but I just want to make sure I'm not missing any potential problems.

Sample Code:

<WebMethod(), ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Function GetListOfRows()
    Try
        Using CTX As New [EFContext]
            Return (
                From R In CTX.[Rows]
                ).ToList()
        End Using
    Catch Ex As Exception
        'Capture & Report Error
    End Try
End Function
Tom Halladay
  • 5,451
  • 5
  • 44
  • 63
  • `ToList()` is a function not a property. Also, I always assign to variable and return it outside my `Using`s/`Try`s, make life easier at debug time. – Walter Stabosz Oct 24 '13 at 15:22
  • This is pretty much the reason for a USING statement. BUT, jumping out in the middle of your code is considered bad programming because it is hard to find the exit point in your code, especially when you have a bunch of `select case` and `if` statements and multiple returns in various places. – Steve Oct 24 '13 at 16:47
  • 1
    @Steve Technically yes, exiting a routine in the middle is usually bad practice, but for short and simple methods I personally don't consider it a problem. Just my two cents. – Tekito Oct 24 '13 at 17:25
  • 1
    @Steve - where have you read "that jumping out in the middle of your code is considered bad programming". There are many that would disagree - e.g. have a look at http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – Joe Ratzer Oct 25 '13 at 08:08
  • When you work with lots of developers that all code differently and you help one of them almost every day figure out why their code is not working as expected, just to find out they either don’t have an ELSE for their IF or SELECT CASE statements or they jumped out when they had more code to execute, you will see that with a few coding changes, your code can be more reliable. Not to say you should NEVER do it, but you should have some logic behind it. Typically there are 3 exit points: After Validation, After Success, After Error. Not in the middle of an SELECT CASE. – Steve Oct 25 '13 at 14:14
  • @Steve - if your colleague's code has many methods with a lot of if/else and case statements then that is the fundamental problem, not the return statements... It sounds like the code needs refactoring. As Kent Beck says a single exit point "was to prevent the confusion possible when jumping into and out of many locations in the same routine. It made good sense when applied to FORTRAN or assembly language programs written with lots of global data where even understanding which statements were executed was hard work ... with small methods and mostly local data, it is needlessly conservative." – Joe Ratzer Oct 29 '13 at 12:41

2 Answers2

1

I would say that that returning within the using statement is acceptable. Remember that using is a try-catch-finally where after code runs in the using it will call the dispose method like a finally would. This would be the same as your code:

Try
    Return (From R In CTX.[Rows]).ToList
Catch Ex As Exception
    'Capture & Report Error
Finally
    'dispose
End Try
Jared Reeves
  • 1,382
  • 1
  • 15
  • 29
  • A using block is literally just syntactic sugar for a try-finally block that automatically calls Dispose() on an object - the IL code generated from the two is the same. – Tyler Gill Oct 24 '13 at 23:56
0

is there any advantage to storing the return value and returning it outside of the Using statement?

No I don't think there is.

However, there is a massive advantage in adding code in your exception block - do something with the exception so it's a known problem...

Joe Ratzer
  • 16,789
  • 3
  • 34
  • 49
  • Thanks, but I just put a comment there because that wasn't the focus of the question – Tom Halladay Oct 24 '13 at 15:24
  • Ok, good stuff. Just thought it was worth mentioning while answering the question. – Joe Ratzer Oct 24 '13 at 15:29
  • 1
    Agree there should be no problem with this, but if you want to test it you can create your own `IDisposable` object, then step through the debugger to see how the `Dispose` is called when exiting from a `Using` block. – Tekito Oct 24 '13 at 17:23