Android Question code optimized

Almora

Active Member
Licensed User
B4X:
Sub Activity_Create(FirstTime As Boolean)

   If student.k1=0 Then
        m1=0       
        Label1.Text="xx "

    Else If student.k1=1 Then
        m1=1       
        Label1.Text="ss "

    Else If student.k1=2 Then
        m1=2       
        Label1.Text="dd "

    Else If student.k1=3 Then
        m1=3       
        Label1.Text="ff "

    Else If student.k1=4 Then
        m1=4       
        Label1.Text="gg "

    Else If student.k1=5 Then
        m1=5       
        Label1.Text="hh "

    Else If student.k1=6 Then
        m1=6       
        Label1.Text="jj "
      

End If
End Sub




Sub Button1_Click 
 
    If m1=0 Then
        StartActivity(k)
    Else If m1=1 Then
        StartActivity(k)
    Else If m1=2 Then
        StartActivity(k)
    Else If m1=3 Then
        StartActivity(k)
    Else If m1=4 Then
        StartActivity(k)
    Else If m1=5 Then
        StartActivity(k)
    Else If m1=6 Then
        StartActivity(k)

      
    End If
End Sub
 
Last edited:

udg

Expert
Licensed User
"Select" avoids all those nested IFs.
Assuming "k" changes among those m1 values..
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Given that you haven't specified what "k" is, I'm assuming it's static.

B4X:
    If m1 >= 0 And m1 <= 19 Then StartActivity(k)

If "k" isn't static, then you could set up an array & use m1 as an index - eg:

B4X:
Private activityToStart() as Activity = Array as Activity(Activity0, Activity1, Activity2, Activity3....Activity19)

Sub button_Click
 StartActivity(activityToStart(m1))
End Sub


- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
#Computersmith64
corrected in the code

there is one activity
startactivity(k)

What are you trying to optimize? What is "k"?

Per my previous answer, you could use an array to optimize your Activity_Create:

B4X:
Sub Globals
    Private textString() as String = Array as String("xx", "ss", "dd", "ff", "gg", "hh", "jj")
End Sub

Sub Activity_Create(FirstTime as Boolean)
    m1 = student.k1
    Label1.Text = textString(m1)
End Sub

- Colin.
 
Upvote 0

Almora

Active Member
Licensed User
B4X:
Sub Button1_Click
If m1 >= 0 And m1 <= 19 Then StartActivity(k)
End Sub

B4X:
Sub Button1_Click
 
    If m1=0 Then
        StartActivity(k)
    Else If m1=1 Then
        StartActivity(k)
    Else If m1=2 Then
        StartActivity(k)
    Else If m1=3 Then
        StartActivity(k)
    Else If m1=4 Then
        StartActivity(k)
    Else If m1=5 Then
        StartActivity(k)
    Else If m1=6 Then
        StartActivity(k)

      
    End If
End Sub

Does the difference between the two codes benefit the performance?

Or just to avoid the crowd of code?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Does the difference between the two codes benefit the performance?

Or just to avoid the crowd of code?

It's not going to make any difference in terms of execution. Unless you're running multiple iterations of "heavy" processing, you generally won't need to worry about optimizing your code for execution speed. There are always multiple ways to write a piece of code to perform a specific function, so whatever works for you is probably fine.

My preference is always to try & write code that will be easy to understand & maintain.

- Colin.
 
Upvote 0
Top