Optional parameters

NeoTechni

Well-Known Member
Licensed User
Longtime User
Thank you for stopping that.

Here's another example.

B4X:
Sub GetTextHeight(BG As Canvas, DesiredHeight As Int, Text As String, theTypeface As Typeface ) As Int 
   Dim temp As Int,CurrentHeight As Int 
   Do Until temp >=  DesiredHeight
      CurrentHeight=CurrentHeight+1
      temp = BG.MeasureStringHeight(Text, theTypeface, CurrentHeight)
   Loop
   If temp>DesiredHeight Then CurrentHeight=CurrentHeight-1
   Return CurrentHeight
End Sub

I've been using this to find the maximum textheight needed to fill a certain amount of vertical pixels.
Now I want to add the ability to check horizontally, without having to change all the times I call this sub.

I could use overloading, but it's stupid and no better than just making a second sub for it. Thus, useless. I want to reuse my code.

So now I have to reuse the DesiredHeight parameter, and just check for a negative value. If it's negative, I assume it's on the other axis.

B4X:
Sub GetTextHeight(BG As Canvas, DesiredHeight As Int, Text As String, theTypeface As Typeface ) As Int 
   Dim temp As Int,CurrentHeight As Int 
   Do Until temp >=  DesiredHeight
      CurrentHeight=CurrentHeight+1
      If DesiredHeight>0 Then
         temp = BG.MeasureStringHeight(Text, theTypeface, CurrentHeight)
      Else
         temp = BG.MeasureStringwidth(Text, theTypeface, CurrentHeight)
      End If
   Loop
   If temp>Abs(DesiredHeight) Then CurrentHeight=CurrentHeight-1
   Return CurrentHeight
End Sub

Kind of messy, and no longer self-evident to anyone else who is using it.

Whereas:

B4X:
Sub GetTextHeight(BG As Canvas, DesiredHeight As Int, Text As String, theTypeface As Typeface, IsYaxis as boolean = true ) As Int
   Dim temp As Int,CurrentHeight As Int 
   Do Until temp >=  DesiredHeight
      CurrentHeight=CurrentHeight+1
      If IsYaxis Then
         temp = BG.MeasureStringHeight(Text, theTypeface, CurrentHeight)
      Else
         temp = BG.MeasureStringwidth(Text, theTypeface, CurrentHeight)
      End If
   Loop
   If temp>Abs(DesiredHeight) Then CurrentHeight=CurrentHeight-1
   Return CurrentHeight
End Sub

Is more user-friendly.
Plus, in a lot of cases you don't have variables that can be re-used.
 

Vader

Well-Known Member
Licensed User
Longtime User
Firstly let me start by saying that if I offend anyone by asking for this here, I apologise in advance.

I searched for Overloading and found a thread that had been closed. In that thread there was a link to this one.

So, here I am :)

Erel, please add my opinion of +1 to the idea of Overloading. I am not going to get into any arguments around overloading vs optional arguments, but I regularly use Overloading to make my coding easier.

My reasoning for this is simple: It makes the code easier to read. In my opinion, adding a numeral to a Function name (ie Sub), is describing a version change, not necessarily a functional change. For example, for backward compatibility (and to not break existing applications), you would add Public Sub FunctionName2() or Public Sub FunctionName3() if either the functionality significantly changed, or there was a change to the method signature. If the change was internal only, there would be no need for either overloading or adding a numeral. (The .NET Compilers actually prevent you from overloading if the number and types of arguments do not change, so we would be following that convention at least)

(At this point, I was going to give a few examples from Windows to add weight to my request, however that isn't really going to change anyone's mind.)

Thanks for reading.

Dave
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Overloading is no different than making a new version of the sub for every possible combination of missing parameters. So for a sub with 8 parameters, you'd need 256 overloaded subs. That is by far, a horrible solution. And I have subs with more parameters than that. That's why I need optional parameters, you'd write the sub once and it'd handle all those combinations.
 

Vader

Well-Known Member
Licensed User
Longtime User
Overloading is no different than making a new version of the sub for every possible combination of missing parameters. So for a sub with 8 parameters, you'd need 256 overloaded subs. That is by far, a horrible solution. And I have subs with more parameters than that. That's why I need optional parameters, you'd write the sub once and it'd handle all those combinations.

Your reasoning is sound. I just wanted to say that I would like Overloading. I never indicated that people didn't need Optional parameters.
 
Top