2

I have a bit of dumbed down code here

Async Function GetCoolNumber() As Task(Of Double)
    Dim n = Await GetNumberFromSomeAsyncSource()
    If IsCool(n) Then
        Return n
    End If
    Return Await GetCoolNumber()
End Function

Private Function GetNumberFromSomeAsyncSource() As Task(Of Double)

Private Function IsCool(n As Object) As Boolean

Now the recursion works fine and I get the result I want. My real code is more complex of course. However is this considered bad practice?

What if I did infinite recursion this way? Would something blow? My gut feeling is that I would keep dumping tasks onto the heap rather than blowing my stack.

bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217

1 Answers1

1

I tend to avoid normal recursion in .NET; there's some variance of support for tail-call optimization, so I just steer clear as a general rule.

With async, you may be OK due to the rewriting; you can check for sure using dotPeek / JustDecompile.

IMO it's best to avoid dependencies on undocumented functionality. So, if the rewriting is officially documented (e.g., the VB.NET language standard) then I would say it's fine. Otherwise, I'd write it iteratively.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • I tried JustDecompile but it is too clever. It shows me the exact code I wrote including the ASYNC / AWAIT keywords :( – bradgonesurfing Oct 11 '12 at 07:32
  • I haven't used JustDecompile. The old Reflector had a "how clever" option - e.g., you could set it to an older version and it would pretend not to understand newer language syntax. – Stephen Cleary Oct 11 '12 at 10:38