Android Tutorial [B4X] "Code Smells" - common mistakes and other tips

Status
Not open for further replies.

LucaMs

Expert
Licensed User
Longtime User
Few minutes ago ?:
https://www.b4x.com/android/forum/threads/b4x-lmb4xcombobox-v-1-01.116767/post-729765
 

Jorge M A

Well-Known Member
Licensed User
Longtime User

It's very interesting @LucaMs Ms! I'm sure I'will try.
Thank you.

But I would like to see an answer from the perspective of Map, the deprecated methods .GetKeyAt, .GetValueAt, and of B4XOrderedMap.
 

OliverA

Expert
Licensed User
Longtime User
Is it possible to rectify cbxEngineType_SelectedIndexChangeden in the code?
(Project Attached)
Thank you.
Flip the map and use the Value portion of SelectedIndexChanged. See attached.
 

Attachments

  • MyComboMappedFlipped.zip
    2.6 KB · Views: 567

Jorge M A

Well-Known Member
Licensed User
Longtime User
Thank you, Erel.
For those interested:
B4X:
Sub ComboBox1_SelectedIndexChanged(Index As Int, Value As Object)
    label1.Text=orderedMap.Keys.Get(Index)
    label2.Text=orderedMap.Values.Get(Index)
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is better to do it like this:
B4X:
Sub ComboBox1_SelectedIndexChanged(Index As Int, Value As Object)
    Dim Key As String = orderedMap.Keys.Get(Index)
    label1.Text = key
    label2.Text = orderedMap.Get(key)
End Sub
The "natural" way to get values is with the keys. Each call to OrderedMap.Values will create a new list for the values.
 

Jorge M A

Well-Known Member
Licensed User
Longtime User
I get "null" value with this.
My mistake. in my case the key is Int.
Works Fine!
B4X:
Sub ComboBox1_SelectedIndexChanged(Index As Int, Value As Object)
    Dim Key As Int = orderedMap.Keys.Get(Index)
    label1.Text = key
    label2.Text = orderedMap.Get(key)
End Sub
 

peacemaker

Expert
Licensed User
Longtime User
Point 12, of booleans - long non-elegant syntax is mostly for debug. How to debug without it (when result to be returned is not calculated yet) ?
 

techknight

Well-Known Member
Licensed User
Longtime User
I am a very verbose person when it comes to programming, so number 8 rubs me the wrong way. I tend to gravitate to the first statement as thats the method that I am familiar with, easier for me to understand, and the only way i will ever do it. IMHO. No right or wrong way there in my opinion.

BTW this is why I cant stand C or any of those other curlybrace languages.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I tend to gravitate to the first statement as thats the method that I am familiar with, easier for me to understand, and the only way i will ever do it.
Things evolve and mostly for good reasons. As I see it, developers should be open to learning new things. You spend a few minutes learning a new thing and you quickly get used to it.
 

LucaMs

Expert
Licensed User
Longtime User
I gave a Like for the first part of your comment, the "verbosity", on which I agree. Smart strings, however, do not make the code less readable, quite the opposite; they are really useful and very simple to use.
 

jnjft

Member
Licensed User
Longtime User
Having read this thread and reviewed some of my code I come to the conclusion that I'm in possession of a very insensible nose...

Thank you, I'm learning a lot from this!
 

Alexander Stolte

Expert
Licensed User
Longtime User
That's a statement I wish I'd known sooner.
I wondered why with "SetLayoutAnimated" the label inside the layout is pulled so strangely during the animation.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…