|
-
Sep 22nd, 2009, 07:37 AM
#1
[RESOLVED] Using function name as return value
I've never done it like this, but I do see code that does pretty often. It seems like it would be bad practice, but also that it would be faster and not cause any problems.
Example:
vb Code:
'Example #1 Private Function GenerateString(ByVal Length As Long) As String Dim l As Long For l = 1 To Length GenerateString = GenerateString & Chr$(Int(Rnd * 255) + 1) Next l End Function 'Example #2 Private Function GenerateString(ByVal Length As Long) As String Dim l As Long GenerateString = Space$(Length) For l = 1 To Length Mid$(GenerateString, l, 1) = Chr$(Int(Rnd * 255) + 1) Next l End Function
vs.
vb Code:
Private Function GenerateString(ByVal Length As Long) As String Dim l As Long, strReturn As String For l = 1 To Length strReturn = strReturn & Chr$(Int(Rnd * 255) + 1) Next l GenerateString = strReturn End Function
Last edited by DigiRev; Sep 22nd, 2009 at 07:40 AM.
-
Sep 22nd, 2009, 08:41 AM
#2
Re: Using function name as return value
your third example is the way I do it... but that's related to my background and training. The first two examples have always bothered me. Not because they are prone to problems, but because it seems unnatural (and harder to read.)
-tg
-
Sep 22nd, 2009, 11:29 AM
#3
Re: Using function name as return value
Why create the extra String variable when for all intents and purposes GenerateString is already a String?
-
Sep 22nd, 2009, 10:53 PM
#4
Re: Using function name as return value
 Originally Posted by MartinLiss
Why create the extra String variable when for all intents and purposes GenerateString is already a String?
That's what I wondered.
VB functions don't return to the caller until the code is finished executing, encounters an error, or encounters an Exit Function statement, so I don't really see a problem. It just seemed like bad practice but I think I might start doing it like that anyway.
-
Sep 23rd, 2009, 08:15 AM
#5
Re: [RESOLVED] Using function name as return value
DigiRev, I personally have used both but tend to using the function name directly. I also do some other stuff also which may seem unnatural, borrowing techgnome's terminology.
For example, why create the variable l when you can reuse Length since it is ByVal? 
Code:
Private Function GenerateString(ByVal Length As Long) As String
' Dim l As Long
GenerateString = Space$(Length)
For Length = 1 To Length
Mid$(GenerateString, Length, 1) = Chr$(Int(Rnd * 255) + 1)
Next Length
End Function
Last edited by LaVolpe; Sep 23rd, 2009 at 08:35 AM.
-
Sep 23rd, 2009, 09:38 AM
#6
Re: [RESOLVED] Using function name as return value
 Originally Posted by LaVolpe
DigiRev, I personally have used both but tend to using the function name directly. I also do some other stuff also which may seem unnatural, borrowing techgnome's terminology.
For example, why create the variable l when you can reuse Length since it is ByVal?
Code:
Private Function GenerateString(ByVal Length As Long) As String
' Dim l As Long
GenerateString = Space$(Length)
For Length = 1 To Length
Mid$(GenerateString, Length, 1) = Chr$(Int(Rnd * 255) + 1)
Next Length
End Function
 Originally Posted by DigiRev
That's what I wondered.
VB functions don't return to the caller until the code is finished executing, encounters an error, or encounters an Exit Function statement, so I don't really see a problem. It just seemed like bad practice but I think I might start doing it like that anyway.
Wouldn't that make your code hard to understand?
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Sep 23rd, 2009, 09:52 AM
#7
Re: [RESOLVED] Using function name as return value
I personally find it hard to follow. Plus, I've also been bitten by this when an error happens... what state is your return value? I'd rather control & build/set/calc/whatever a local variable, then have one exit point where it is returned. I've never liked the VB6 method of returning values from functions anyways. But it might be based on my background.
-tg
-
Sep 23rd, 2009, 10:01 AM
#8
Re: [RESOLVED] Using function name as return value
Harder, easier to follow? Proper, not proper?
Personal preferences. The key question is: "Is it correct syntax and does it work"? It does.
However, if one is part of a coding team/company, then the team/company's standards should always be adhered to. Else you are only bound by your own whims.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|