Esmond Hart asks:
I code a lot of VB.NET and the bug I make most often is forgetting to return the result of a function call. Daft but true. How I would welcome an option that flagged each End Function statement without a Return statement immediately preceding it. Option EvenStricter maybe.
Well, we'll actually be doing better than this in Whidbey! One of the major features that we're implementing in the compiler for Whidbey is “flow of control” analysis. This allows us to track at compile-time the ways in which a method can execute and then give the user warnings based on that analysis. For example, take the following code snippet: (I realize it's a nonsense function, but my brain is working slowly this morning and I can't think of anything better.)
Function Test(ByVal Value As Integer) As Integer
Dim c,d As Collection
If Value > 0 Then
c = New Collection()
End If
c.Add(Value)
If Value < 0 Then
Return Value
End If
End Function
In Whidbey the compiler will be able to walk through the function and figure out the ways in which it might be executed, determining the following things:
- It's possible that the variable c will be used without having assigned it a value.
- It's possible that the function will not explicitly return a value.
- The local variable d was never used.
Once we've determined these things, we'll give warnings on them. Pretty cool, huh? (Yes, I know, compilers for the C family of languages [and many, many others, I'm sure] have had this for forever, but it's nice to join the party.)