473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

New kind of warning

I have a very simple function I'm working on.

Function CarSpeed(ByVal kph As Integer)
If kph < 0 Then
_quote = "the car is in reverse"
_actualspeed = kph
ElseIf kph = 0 Then
_quote = "the car is parked."
_actualspeed = kph
ElseIf kph > 0 And kph <= 10 Then
_quote = "the car is moving slowly."
_actualspeed = kph
Else
_quote = "the car is moving quickly."
_actualspeed = kph
End If

'New for VS 2005 apparently...
Return kph

End Function

Originally, I built this using Visual Studio .NET 2003. I just downloaded
the Visual Studio .NET 2005 express and when I initially typed in the old
code I didn't have the line "Return kph" on the original version, and I got a
warning saying, "Function CarSpeed doesn't return a value on all code paths.
A null reference exception could occur at run time when the result is used."

So why do I need to include the line "Return kph" for VS 2005?
Jan 11 '06 #1
7 2934

Military Smurf wrote:
I have a very simple function I'm working on.

Function CarSpeed(ByVal kph As Integer)
If kph < 0 Then
_quote = "the car is in reverse"
_actualspeed = kph
ElseIf kph = 0 Then
_quote = "the car is parked."
_actualspeed = kph
ElseIf kph > 0 And kph <= 10 Then
_quote = "the car is moving slowly."
_actualspeed = kph
Else
_quote = "the car is moving quickly."
_actualspeed = kph
End If

'New for VS 2005 apparently...
Return kph

End Function

Originally, I built this using Visual Studio .NET 2003. I just downloaded
the Visual Studio .NET 2005 express and when I initially typed in the old
code I didn't have the line "Return kph" on the original version, and I got a
warning saying, "Function CarSpeed doesn't return a value on all code paths.
A null reference exception could occur at run time when the result is used."

So why do I need to include the line "Return kph" for VS 2005?


Since you don't explicitly declare a type, this function returns an
Object.

If a function that returns an Object never sets its return value
(either by the older FunctionName= syntax or with Return), then it will
return a null reference. Callers attempting to do anything with the
return value of the function will then most likely cause a null
reference exception.

VS2005 notices this and therefore suggests that the return value not be
Nothing, which your Return statement duly does.

That's the simple facts, now the commentary:

- VS2003 with Option Strict On doesn't like your original code, which
tells me you are running without Option Strict On - you may want to
consider making this an absolute standard in the future. The time for
additional typing sometimes required is more than compensated by the
debugging time savings produced by working with strongly-typed code.

- It's bad style to have 'side effects' in procedures, and worse to
have functions that consist _only_ of side effects! A call to this
function should be replaced with something like

_actualspeed = kph
_quote = DescribeSpeed(kph)

where DescribeSpeed takes an Integer and returns a String.

--
Larry Lard
Replies to group please

Jan 11 '06 #2


"Larry Lard" wrote:

Military Smurf wrote:
I have a very simple function I'm working on.

Function CarSpeed(ByVal kph As Integer)
If kph < 0 Then
_quote = "the car is in reverse"
_actualspeed = kph
ElseIf kph = 0 Then
_quote = "the car is parked."
_actualspeed = kph
ElseIf kph > 0 And kph <= 10 Then
_quote = "the car is moving slowly."
_actualspeed = kph
Else
_quote = "the car is moving quickly."
_actualspeed = kph
End If

'New for VS 2005 apparently...
Return kph

End Function

Originally, I built this using Visual Studio .NET 2003. I just downloaded
the Visual Studio .NET 2005 express and when I initially typed in the old
code I didn't have the line "Return kph" on the original version, and I got a
warning saying, "Function CarSpeed doesn't return a value on all code paths.
A null reference exception could occur at run time when the result is used."

So why do I need to include the line "Return kph" for VS 2005?


Since you don't explicitly declare a type, this function returns an
Object.

If a function that returns an Object never sets its return value
(either by the older FunctionName= syntax or with Return), then it will
return a null reference. Callers attempting to do anything with the
return value of the function will then most likely cause a null
reference exception.

VS2005 notices this and therefore suggests that the return value not be
Nothing, which your Return statement duly does.

That's the simple facts, now the commentary:

- VS2003 with Option Strict On doesn't like your original code, which
tells me you are running without Option Strict On - you may want to
consider making this an absolute standard in the future. The time for
additional typing sometimes required is more than compensated by the
debugging time savings produced by working with strongly-typed code.

- It's bad style to have 'side effects' in procedures, and worse to
have functions that consist _only_ of side effects! A call to this
function should be replaced with something like

_actualspeed = kph
_quote = DescribeSpeed(kph)

where DescribeSpeed takes an Integer and returns a String.

--
Larry Lard
Replies to group please


Thanks for that, and while I understand a lot of what you are saying, what
are these "side effects" I need to be aware of?
Jan 11 '06 #3
If you didn't have the Return statement in your function before, I
presume that you were not calling it as a function. In that case, you
should make the method a Sub instead of a function.

Jan 11 '06 #4
"Military Smurf" <Mi***********@discussions.microsoft.com> schrieb:
Function CarSpeed(ByVal kph As Integer)
If kph < 0 Then
_quote = "the car is in reverse"
_actualspeed = kph
ElseIf kph = 0 Then
_quote = "the car is parked."
_actualspeed = kph
ElseIf kph > 0 And kph <= 10 Then
_quote = "the car is moving slowly."
_actualspeed = kph
Else
_quote = "the car is moving quickly."
_actualspeed = kph
End If

'New for VS 2005 apparently...
Return kph

End Function

Originally, I built this using Visual Studio .NET 2003. I just downloaded
the Visual Studio .NET 2005 express and when I initially typed in the old
code I didn't have the line "Return kph" on the original version, and I
got a
warning saying, "Function CarSpeed doesn't return a value on all code
paths.
A null reference exception could occur at run time when the result is
used."


Use 'Sub' instead of 'Function' if 'CarSpeed' is not intended to return a
value.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jan 11 '06 #5
As addition to Herfried because he obvious missed that.

to make it even less troubleful use "AndAlso" instead of "And"

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:uJ***************@TK2MSFTNGP10.phx.gbl...
"Military Smurf" <Mi***********@discussions.microsoft.com> schrieb:
Function CarSpeed(ByVal kph As Integer)
If kph < 0 Then
_quote = "the car is in reverse"
_actualspeed = kph
ElseIf kph = 0 Then
_quote = "the car is parked."
_actualspeed = kph
ElseIf kph > 0 And kph <= 10 Then
_quote = "the car is moving slowly."
_actualspeed = kph
Else
_quote = "the car is moving quickly."
_actualspeed = kph
End If

'New for VS 2005 apparently...
Return kph

End Function

Originally, I built this using Visual Studio .NET 2003. I just
downloaded
the Visual Studio .NET 2005 express and when I initially typed in the old
code I didn't have the line "Return kph" on the original version, and I
got a
warning saying, "Function CarSpeed doesn't return a value on all code
paths.
A null reference exception could occur at run time when the result is
used."


Use 'Sub' instead of 'Function' if 'CarSpeed' is not intended to return a
value.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jan 11 '06 #6
if andalso orelse = new to you then

read the The Ballad of AndAlso and OrElse

http://www.panopticoncentral.net/arc...08/18/179.aspx

end if

:-)

Michel Posseth [MCP]
"Cor Ligthert [MVP]" <no************@planet.nl> schreef in bericht
news:uE*************@TK2MSFTNGP14.phx.gbl...
As addition to Herfried because he obvious missed that.

to make it even less troubleful use "AndAlso" instead of "And"

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:uJ***************@TK2MSFTNGP10.phx.gbl...
"Military Smurf" <Mi***********@discussions.microsoft.com> schrieb:
Function CarSpeed(ByVal kph As Integer)
If kph < 0 Then
_quote = "the car is in reverse"
_actualspeed = kph
ElseIf kph = 0 Then
_quote = "the car is parked."
_actualspeed = kph
ElseIf kph > 0 And kph <= 10 Then
_quote = "the car is moving slowly."
_actualspeed = kph
Else
_quote = "the car is moving quickly."
_actualspeed = kph
End If

'New for VS 2005 apparently...
Return kph

End Function

Originally, I built this using Visual Studio .NET 2003. I just
downloaded
the Visual Studio .NET 2005 express and when I initially typed in the
old
code I didn't have the line "Return kph" on the original version, and I
got a
warning saying, "Function CarSpeed doesn't return a value on all code
paths.
A null reference exception could occur at run time when the result is
used."


Use 'Sub' instead of 'Function' if 'CarSpeed' is not intended to return a
value.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Jan 11 '06 #7

Military Smurf wrote:
"Larry Lard" wrote:
- It's bad style to have 'side effects' in procedures, and worse to
have functions that consist _only_ of side effects! A call to this
function should be replaced with something like

_actualspeed = kph
_quote = DescribeSpeed(kph)

where DescribeSpeed takes an Integer and returns a String.


Thanks for that, and while I understand a lot of what you are saying, what
are these "side effects" I need to be aware of?


You can read more about side effects here:
<http://en.wikipedia.org/wiki/Side-effect_%28computer_science%29> but
in short, a side effect is when a function changes something that it
isn't obvious that it changes, typically a variable at some broader
scope. An example:

'module level
Dim a as integer

Function Negate(n as integer) as integer
Return -n
End Function

Function NegateWithSideEffect(n as integer) as integer
a = a + 1
Return -n
End Function

Sub Example()
Dim b as integer
b = 3
b = Negate(b)
'now b = -3 and no surprises

a = 2
b = NegateWithSideEffect(b)
'now b = 3 as expected
'BUT a has also changed!
End Sub

In this example, there is nothing to tell a caller of
NegateWithSideEffect that calling this function will also change a.
Real world examples don't have such 'giveaway' names! :)

There's a guideline for coding called the Principle of Least
Astonishment (more at
<http://en.wikipedia.org/wiki/Principle_of_least_astonishment>) which
basically says: don't do anything surprising. In this example changing
a might well come as a surprise, and an unpleasant one, to an unwitting
user of NegateWithSideEffect.

Like all guidelines and principles, how rigorously to stick to them
depends on a number of factors - throwaway programs for personal
consumption, you can get away with anything you like; flying a space
shuttle, get ten people to check every line. But in general it is
better to get into the habit of doing things 'the right way', so that
everything one does is better than it needs to be.

--
Larry Lard
Replies to group please

Jan 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Holly | last post by:
I'm trying to validate my code and I can't figure out what kind of doctype I have. The validator can't tell me anything because it can't move beyond the doctype declaration. ...
11
by: Steven T. Hatton | last post by:
There are probably a zillion ways to do this, but I'm wondering of there's a C++ convention. I want to have a fixed mapping between keys and values. It's basically the same thing as a std::map<>,...
3
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released...
3
by: Bill Burris | last post by:
How do I find what is causing this warning from the Linker? If I use /NODEFAULTLIB I get hundreds of undefined symbols. LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other...
3
by: DJTN | last post by:
I'm getting the following error when I try to compile my setup project in VS 2002. I have re-installed the .net framework 1.1 and it didnt solve the problem. WARNING: Unable to find dependency...
0
by: tony | last post by:
Hello!! I use VS 2003 and C# for all class library except MeltPracCommon.dll which is C++.NET The problem is that I get these warnings when building the exe file and use my class libraries....
1
by: Ian | last post by:
I've just discovered the msclr::lock class in the C++ Support Library online documentation. This seems like a much cleaner way to implement thread protection than using...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
4
by: cody | last post by:
It is possible to declare and use/instantiate a class with a uninitialized readonly field without even a compiler warning. Why don't I get warnings? public class Stuff { public readonly int a;...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.