Why doesn't the compiler complain about functions that don't return a result?

  • Thread starter Thread starter Andrew J. Marshall
  • Start date Start date
* "Andrew J. Marshall said:
It was dumb in VB6 and it's still dumb today ... or am I the dumb one?

The datatype's default value will be returned, that's why no compile
time error is shown.
 
But don't you think it SHOULD be an (optional) error (or at least a warning)
to protect poor slobs like me who sometimes forget to set the return value?
 
* "Andrew J. Marshall said:
But don't you think it SHOULD be an (optional) error (or at least a warning)
to protect poor slobs like me who sometimes forget to set the return value?

I would prefer an otpional warning.
 
I go back to my comments yesterday on this one.

Every language has its own "reasons" for doing what it does. This one is
VB, whereas if you wanted to really check that you could use C++. But then
again, with C++ you will spend days writing what I can do in minutes in
VB...

-CJ

Andrew J. Marshall said:
But don't you think it SHOULD be an (optional) error (or at least a warning)
to protect poor slobs like me who sometimes forget to set the return value?
 
Thanks Andrew,

I see what you mean... I was thinking you didn't specify a return
"datatype." And I agree it seems unlikely that anybody wants a default
value returned and it should at least fire off a warning.

Purely guesswork on my part but since a function's return value can be
returned through the function "name" I'm going to guess that a hidden
variable is inserted by the compiler and initialized to a default value. As
far as the compiler is concerned a proper return value was defined so there
is no error.

It's a side-effect and I'll guess won't be changed.

Tom
 
Hi Andrew

I agree with you in principle, but I can see a can of worms looming.

Using your example,

Public Class Incomplete
Public Function NotCoded(Value As Integer) as Integer

If Value = 4 Then
Return 99
End If

End Function
End Class

Now we are into code analysis. Would this generate a warning? It could be
made much more complicated, and at what point would the compiler give up and
say "I can't work out whether a value is being returned in all cases"?

HTH

Charles
 
Andrew,
I agree, there should be a "warning" that you are allowing a value to be
implicitly returned via a hidden variable that matches the function name.

I find this kind of warning is easily handled by a Code Critic or Code
Analyzer.

A Code Critic is a utility that looks at your code and gives you feedback on
what you should change to improve it.

Such as
http://www.fmsinc.com/reviews/tnanalyzer/sd1202.htm

Hope this helps
Jay
 
Back
Top