[B4X] "Code Smells" - common mistakes and other tips
"Code smells" are common patterns that can indicate that there is a problem in the code. A problem doesn't mean that the code doesn't work, it might be that it will be difficult to maintain it or that there are more elegant ways to implement the same thing. Remember that not everything is clear...

B4X:
'bad
Private Sub Button1_Click
xui.MsgboxAsync("สวัสดี B4X ชาวไทยทุกคน", "B4X")
End Sub
Private Sub Button2_Click
xui.MsgboxAsync("สวัสดี B4X ชาวไทยทุกคน", "B4X")
End Sub
'โดยตั้งค่า Event Name ของแต่ละ Button เป็นชื่อส่วนกลาง เช่น ButtonX แล้วเขียนโค้ดจัดการร่วมกันได้
'แต่ถ้าต้องการตรวจสอบว่า Button อันไหนเป็นปุ่มที่ถูกกด ให้ตั้งค่า Tag เป็นชื่อของปุ่มนั้น เช่น Button1.tag="Button1" และ
'Button2.tag="Button2" แล้วใช้ตัวแปรชื่อว่า Sender เป็นตัวรับค่าไว้ก่อน ดูตามตัวอย่าง
B4X:
'good
Private Sub ButtonX_Click
Dim btn As Button=Sender
If btn.Tag="Button1" Then
'Do something specific for Button1
Else If btn.Tag="Button2" Then
'Do something specific for Button2
End If
End Sub
Last edited: