Optional parameters

NeoTechni

Well-Known Member
Licensed User
Longtime User
Overloading is ugly. 3 declarations for the same sub rather than 1? That's unorganized. Especially if you have to make another for each possible combination of parameters.
I don't see how if-then is ugly. It's what programming is based off of

It wouldnt solve the problems I listed earlier, just go back and read them, I don't want to repost them.
This topic is about requesting optional parameters. Please make another topic if you want to request something else.
 

Jost aus Soest

Active Member
Licensed User
Longtime User
Overloading is ugly.

No, it's not. It's much more elegant and separates different algorithms from each other.

3 declarations for the same sub rather than 1? That's unorganized.

It's not the same sub, there are 3 subs with same name.
3 different algorithms in 3 different subs ==> that's what I call "very organized"!

It wouldnt solve the problems I listed earlier, just go back and read them, I don't want to repost them.

In this thread you have posted two examples:
B4X:
drawbutton(X,Y,Width, Height, ,,, vbBlue) '20 arguments?!?
B4X:
sub Functionname(optional value as int=-1)

The first one is a good example, what you should avoid in modern algorithms (hint: in this case maybe you can use user types), while the second one is very easy to "translate" using overloading, e. g.:
B4X:
Sub Functionname(Value as Int)
  'normal function call
  'action 1...
End Sub

Sub Functionname()
  'lazy' function call, so use normal call with given Parameter
  Functionname(-1)
  'or do something different...
  'action 2...
End Sub

But what do you do with your optional parameter, when there is no unused value like -1?

This topic is about requesting optional parameters. Please make another topic if you want to request something else.

It was the good idea of r2d4, not mine. And he showed an easy to implement alternative for optional parameters. So this discussion is completly "on topic".

And it's much better to have overloading than nothing, isn't it? ;)
 

thedesolatesoul

Expert
Licensed User
Longtime User
Jost,
You are wrong in assuming overloading is the same as optional parameters.
The basic difference is that in overloading you have TWO function definitions. You need to maintain double the amount of code.
In optional parameters, you need to maintain only 1 sub so you do not need to fix the same bugs twice.
That being said, implementing optional parameters with the current compiler is not too hard. Just pass Null in the unknown parameters and do a check in the sub if needed.
 

Jost aus Soest

Active Member
Licensed User
Longtime User
overloading > optional parameters

You are wrong in assuming overloading is the same as optional parameters.

I've never said something like that!?!

I know very well the differences - not only theoretical from university (I studied informatics with one focus on compiler and languages), but also by personal experience, using classic VB and Java for many professional years!
BTW: VB-Tec - Visual Basic - aus der Praxis für die Praxis (biggest non profit VB5-site in Germany)

The basic difference is that in overloading you have TWO function definitions.

As I've already written: That's in fact a very good thing, because you can yourself decide to put the algorithm in one sub, or - when it's necessary - to use two completly different subs! It's simply your choice!

You need to maintain double the amount of code.

That's wrong. Look again at my example:
B4X:
Sub Functionname(Value as Int)
  'normal function call:
  'your code here...  <-- CODE
End Sub

Sub Functionname()
  'lazy' function call, so use normal call with predefined Parameter:
  Functionname(-1)
End Sub
As you can see, you must write and maintain the same code only once!

That being said, implementing optional parameters with the current compiler is not too hard. Just pass Null in the unknown parameters and do a check in the sub if needed.

As I've already explained: Optional parameters are much harder to implement than overloading, 'cause Java already knows overloading.
BTW: You can't pass a null to an int-parameter etc. pp.!

Think again about my arguments...
It's much better to have overloading than nothing, isn't it? ;)
 

francoisg

Active Member
Licensed User
Longtime User
ANY additional functionality to the basic language is welcome, so you are BOTH correct (just a case of preference). Let's see if the developers will consider any one of these options and let's all be happy either way?

;)
 

Jost aus Soest

Active Member
Licensed User
Longtime User
Sorry?

Sorry desolatesoul, but are you not able to read, or do you just prefer to ignore facts that you don't like?

But if you already acknowledge that overloading <> optional parameters then why are you polluting this thread?

Maybe you can understand this:
overloading > optional parameters

The whole discussion began when r2d4 made the proposal for an useful and good alternative! So it's not "off topic"! :rolleyes:

If we can have both: Very nice! :icon_clap:
If we can have only one by choice: I would prefer overloading. :cool:
If we can have only overloading or nothing ('cause it's much easier to implement): Everyone takes overloading! ;)
 

kanaida

Active Member
Licensed User
Longtime User
I really think this is necessary.

-I'd cut down on redundant subs
For example, the 2 methods to get part of a string (I forget the name) could be combined into 1 which takes 2 parameters, and the second defaults to -1 (an invalid number) so it acts like the single parameter version instead. Using an invalid input as the default to detect when they aren't entered is very convenient

-I have some functions that take like 20 parameters, and most of the time I call it I have to use the default values.
-It also lets you change the default value without changing every time the sub is called

-It lets you add parameters later in the program without having to track down everytime it was called.

This is where i usually use classes in vb, on in b4a types/structures. Just create one object called TYPE_STARTREK_SOMEOBJ with all the fields it needs. Then use one single param in your sub. If you need to set defaults create a constructor sub to create an instance of that object or set default values for it. Your sub is already smart enough to check the values since they're optional so code changes are probably minimal. e.g MyType.SomeParam instead of SomeParam. This also avoids the need for overloads as well.

Having that many parameters is messy as you noticed, and although overloads are convenient sometimes, it's definately not convenient jumping through code and trying to determine wich sub to edit to update or change something, especially if they're not next to eachother in the code. It would probably make the ide more resource intensive as well comparing tons of parameters just to do checks, intellisense and compiling.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
No, it's not. It's much more elegant and separates different algorithms from each other.

The problem is, I'm not trying to do that.
They are the same sub

It's not the same sub, there are 3 subs with same name.
3 different algorithms in 3 different subs

I'm not trying to do that.

In this thread you have posted two examples:
B4X:
drawbutton(X,Y,Width, Height, ,,, vbBlue) '20 arguments?!?
B4X:
sub Functionname(optional value as int=-1)

The first one is a good example, what you should avoid in modern algorithms (hint: in this case maybe you can use user types),

I am actually using a user type.
But I try to avoid passing it in the way you're suggesting, as that removes it's ability to be reusable in the way I am using it.

The way you're using overloading, makes it absolutely, completely useless. I can do everything overloading does already by making multiple subs. Which is unorganized/redundant/wasteful/inelegant

I repeat myself, you're asking for something else. Make your own topic. This is not the place to tell me I should be asking for something I dont want.

But what do you do with your optional parameter, when there is no unused value like -1?

-1 is the default value so you can detect that it was missing.

And it's much better to have overloading than nothing, isn't it? ;)

Not in the slightest. Overloading is useless to me. It does nothing that I need of it.

Sorry desolatesoul, but are you not able to read, or do you just prefer to ignore facts that you don't like?

Insults aren't helpful at all, if you're going to do that, just leave the forum entirely.

Maybe you can understand this:
overloading > optional parameters

No they aren't. Overloading is something completely different.

If we can have only one by choice: I would prefer overloading. :cool:
If we can have only overloading or nothing ('cause it's much easier to implement): Everyone takes overloading! ;)

You're being extremely rude, coming in to my topic telling me I shouldn't be asking for a standard feature in other programming languages, then insulting the people who agree with me. Make your own topic and ask for it there. You're not being helpful in this one.

Overloading is not easier to implement.
All he has to do, is if a parameter is missing, obtain the default value from the declaration. Overloading would require a lot more work, especially in changing how his intellisense works.

What you're suggesting, is an exponential increase in coding.
For every optional parameter, thats another exponential increase in sub declarations.
With optional parameters, no matter how many parameters you have, its 1 declaration.
Your suggestion solves nothing, since it can be done already
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
This is where i usually use classes in vb, on in b4a types/structures. Just create one object called TYPE_STARTREK_SOMEOBJ with all the fields it needs.

That method is significantly less useful/reusable. I avoid it for that reason.
If I want the code to be reusable, I need another sub to construct the structure in a single line, which brings back the optional parameter issue.

Having that many parameters is messy as you noticed

It's also an EXAMPLE. You're not supposed to assume that's every single situation I use it in.

ANY time I add a parameter to any sub, I have to go back to everytime it's called and add default data to it. It is very inconvenient

It would probably make the ide more resource intensive as well comparing tons of parameters just to do checks, intellisense and compiling.

Not really. It would be extremely easy to add optional parameters. Easier that overloading.
 
Last edited:

naruto

Member
Licensed User
Longtime User
overloading / optional parameters

Personally I would like to see overloading to be supported. I wouldn't mind heading more towards an Object Oriented based direction. When overloading was mentioned my initial thought was class function overloading.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Personally I would like to see overloading to be supported.

Then you should make a topic asking for it
its both rude and pointless to go into a topic asking for something completely different than the topic.
 
Top