Thank you for stopping that.
Here's another example.
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.
Kind of messy, and no longer self-evident to anyone else who is using it.
Whereas:
Is more user-friendly.
Plus, in a lot of cases you don't have variables that can be re-used.
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.