|
-
Nov 17th, 2000, 11:15 AM
#1
Thread Starter
Hyperactive Member
Why using Call when its not necessary?
Code:
'DoSomething is a sub
'Why this:
call DoSomething
'or this:
DoSomething
WP
-
Nov 17th, 2000, 11:23 AM
#2
Frenzied Member
Why :
Code:
'Why this
Dim hello as String
'or
Dim hello$
I think it's just a matter of preferences!
-
Nov 17th, 2000, 11:41 AM
#3
IS the same like, why using a pencil. if you can use a pen.
-
Nov 17th, 2000, 11:57 AM
#4
Frenzied Member
I think it's sometimes easier to see you're calling an outside function (hmm.. that's what I use it for ) and not just VB code, but as sebs said, it's just a matter of preferences.
and sebs, I use Dim str$ because it's shorter and I think it looks cool 
Why use a pencil if you can use a pen
'Cause you can make changes when you've done something wrong
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Nov 17th, 2000, 11:59 AM
#5
Lively Member
It aids readability.
Have you ever tried to debug/test/update/evaluate someone elses code....
nightmare.
These little niceties help loads
H.
 Just trying to muddle through...
-
Nov 17th, 2000, 12:23 PM
#6
Member
hmm, dont you need to use the call feature if using api.
I tried to do some functions w.out calling anything and it didnt work. Instead I used the Call and it worked fine.
I wasnt actually calling the function on the form, but writing an if then statement w. api calls.
-
Nov 17th, 2000, 03:28 PM
#7
Fanatic Member
Chemically Formulated As:
Dr. Nitro
-
Nov 17th, 2000, 03:53 PM
#8
Addicted Member
I agree with Jop & Hollie, it's just easer for another programmer (if your working in a team) to see that you are calling a sub or function and makes his life a bit simpler etc...
Don't forget is you use call you must inclose arguments in brackets "()"
-
Nov 17th, 2000, 05:04 PM
#9
I also agree with Hollie on the readability issue.
One of my more miserable tasks at work is maintaining other peoples code when it is unreadable and difficult to follow. Believe it or not some people don't even indent! I'm begging all you coders to put in comments so I know what you were thinking when you wrote the stuff. I do it and find it actually helps me nut out tricky algorithms
-
Nov 17th, 2000, 07:40 PM
#10
Basically, Call makes your source code more organized. When you use call, you enclose your arguments in brackets.
Code:
Call MyFunc(Arg1, 0, 0, 0, 0, &H54)
If you omit the Call keyword, you cannot enclose it in brackets, which in some people's opinion is sloppy coding.
Code:
MyFunc Arg1, 0, 0, 0, 0, &H54
-
Nov 17th, 2000, 08:06 PM
#11
Fanatic Member
Call uses less memory when calling a function if you don't need the return value (in the case of the api). This is because you don't need to allocate a variable to store the data, as Call discards it.
I'm actually pretty good at reading others' code. I indent all code before trying to figure out what it does. If you have a piece of code that is absolutly unreadable, I can usually decipher it. I decipher other people's code when I get bored with what ever project I'm working on.
-
Nov 18th, 2000, 09:19 AM
#12
Lively Member
I remember that in earlier versions of VB, before Option Explicit, I was never sure that a statement DoSomething was a call to a Sub or just an undeclared variable; whereas Call DoSomething is totally unambiguous.
Adrian.
-
Nov 18th, 2000, 09:29 AM
#13
Monday Morning Lunatic
agent - all the API functions return a value for a good reason. Technically, you should always check for an error code, though very few do.
My personal preferences:
Code:
Dim x As String
x = MyFunc(10, 10)
MySub 10, 10
I just don't like seeing Call around, since I've always been used to minimising code length for the sake of readability.
Adrian brought up a good point about Option Explicit. Does anyone here always use it? (I do)
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 18th, 2000, 09:39 AM
#14
Lively Member
Option Explicit
Best thing since sliced bread for me. I'm a classic for mis-typing variables. DOH!
Adrian.
-
Nov 18th, 2000, 09:47 AM
#15
Hyperactive Member
i'm with parksie
minimalist code makes for elegance.
td
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Nov 18th, 2000, 12:07 PM
#16
Monday Morning Lunatic
Not always, though. Quick C++ example (good for minimising code size):
Code:
if(i) {
y = 10;
} else {
y = 20;
}
...can be condensed to:
But that doesn't make it any more readable .
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 18th, 2000, 12:57 PM
#17
In my opinion, the Call statement makes code easier to understand.
Call MyFunc(1,2,3,4,5)
rather than:
MyFunc 1,2,3,4,5
Makes it easier to read and understand.
But both ways work the same.
-
Nov 18th, 2000, 01:37 PM
#18
Addicted Member
Hi all !!!
I agree with your statements about readable code from
another coder but in all your examples I'am missing that each function or sub call can also be written with named parameter for better reading e.g.
iRet = fktMyfunction (Para1:=1,Para2:=3)
DoSomething Para1:=1,Para2:=3
so in my opinion you didn't need the CALL Statement if you use the naming convention and named parameters.
-cu TheOnly
-
Nov 18th, 2000, 02:15 PM
#19
Frenzied Member
the trouble with that is that if the reader is unfarmiiar with the calling convention he/she may wonder what iRet is, it's definetley nowhere near as obvious as a big blue call sign, also it probably doesn't matter in most vb apps but if you are going to use a return value you have to declare it which makes the code a little slower, and if you want it to accept more than one data type you have to make it a variant or go throgh conversions, which can slow the code down quite considerably and even cause errors, if you ever need to look at iRet you should really give it a different name so the reader knows what it is. Although it's only a little sloppy as coding practices go if you're writing a time critical or recursive function iRet shouldn't be used. which can mean 2 seperate coding conventions in the same module. unless there is a serious advantage of iRet in your code there's no point using it and it is harmful. So Call is definatley the better option there.
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
|