Android Question Something similar a ternary operator

max123

Well-Known Member
Licensed User
Longtime User
Hi developers,

my question is simple, is this line:
B4X:
ButtonA(i).Toggle = (A_Array(i).Mode = TOGGLE_ON)
.... the same of this code ?
B4X:
If A_Array(i).Mode = TOGGLE_ON Then
    ButtonA(i).Toggle = True
Else
    ButtonA(i).Toggle = False              
End If

Is that a correct way ?
Better way to do it ?

Thanks
 

emexes

Expert
Licensed User
is this line:.... the same of this code ?

Looks same to me. You might not even need the brackets, but personally I'm with you: they clarify the code here.

Is that a correct way ?

Yes.

Better way to do it ?

Depends who's going to be reading (maintaining) the code later. A just-in-case explanatory comment rarely hurts, and won't slow down the program run time any. ?
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
I like a lot this syntax, is similar to C ternary operator that works this way:
C:
(A_Array(i).Mode == TOGGLE_ON) ? ButtonA(i).Toggle = true : ButtonA(i).Toggle = false;

but always in C is even more readable this:
C:
ButtonA(i).Toggle = (A_Array(i).Mode == TOGGLE_ON);

So you intend this way without brackets ? This is a bit confusing, but even seem that put all 3 values the same value:
B4X:
ButtonA(i).Toggle = A_Array(i).Mode = TOGGLE_ON

This way (with or without brackets) is good because exchange the same C syntax apart the = that in C is == as comparator.
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Great.

Luca, I never used it, it is in the B4X Core. Yes, sounds like a ternary operator.
Thank you.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
This is another possible way:
B4X:
ButtonA(i).Toggle = IIF(A_Array(i).Mode = TOGGLE_ON, True, False)

That is unlike you to encourage tautology ? in that the following are same:

B4X:
IIF(condition, True, False)
B4X:
condition

although you might sometimes need brackets around the latter
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Sorry but I'm no so stupid boys?

I know you asked @aeric , so no problems.

TOGGLE_ON is not Boolean, it is a Global Byte (or Int, now I do not remember) variable, in this case TOGGLE_OFF is 0 and TOGGLE_ON is 1, but because I need to add more I do not just use a Boolean, I plained this when started a project. There are a couple of these.

Even A_Array(i).Mode that I compare to is Byte or Int, both the same type.

As it is a good pratice I always follow it and put costants in UPPER CASE.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Sorry but I'm no so stupid boys?

I know you asked @aeric , so no problems.

TOGGLE_ON is not Boolean, it is a Global Byte (or Int, now I do not remember) variable, in this case TOGGLE_OFF is 0 and TOGGLE_ON is 1, but because I need to add more I do not just use a Boolean, I plained this when started a project. There are a couple of these.

Even A_Array(i).Mode that I compare to is Byte or Int, both the same type.

As it is a good pratice I always follow it and put costants in UPPER CASE.
I have different opinion which I think I don't want to argue. My advice is to refractor the code to use appropriate data types.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
An integer numeric type seems entirely appropriate here. ☮️

Especially if B4X Select Case is likely to be used, or indexing into an array, or a state machine, or any bitfield templating.
If I know button toggle only has 2 possibilities then I would use Boolean data type.
If Int is used, then we may use Select Case or If Else. Ternary is suitable for 2 possibilities and if you use nested ternary then the code is hard to read..
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Yes @emexes you are right, I cannot use Boolean, I need to use at least Byte, I've 3 and more options here and Boolean cannot handle, the right is Byte, I even set -1 on some to know that are unused, like a function that return -1.

I do large use of Select...Case...
 
Upvote 0

emexes

Expert
Licensed User
I even set -1 on some to know that are unused, like a function that return -1.

Probably a bit late now, but I feel 0 is usually a better choice to represent unused, because it is the default value of new numeric variables.

Although if you're using -1 to mean "this is consciously unused, I haven't just forgotten about it" then... good stuff! ?

But even then, I'd still "reserve" 0 as meaning uninitialised / forgotten, and start my state numbering from 1.

Lol it's like I don't trust myself ? a nicer description is "defensive programming" ?
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
My state should start from zero, I've a lots of arrays that need to be zero based, even this app send midi data and I manage that in a custom type and it is zero based. For me is not a bad pratice use -1 as unused state, but any developer can do different way, B4X and Java too are zero based.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Yes @emexes you are right, I cannot use Boolean, I need to use at least Byte, I've 3 and more options here and Boolean cannot handle, the right is Byte, I even set -1 on some to know that are unused, like a function that return -1.

I do large use of Select...Case...
Select Case is my favourite. Maybe because my habit of procrastinate a lot and I always leave many empty cases half way. ?
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
@aeric , ternary operator is not good to read but simplifies a lot the code instead of using a lots of Ifs.

I know that is not super to read but make the code shorter.

The other good option is Select or switch in C.

But I found that this is good too...
B4X:
var1 = (var2 = var3)
but only in case var1 is Boolean.
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
B4X:
Select customtype.xxx
   Case aaa
     Do something...
   Case bbb
     Do something...
   Case ccc
     Do something...
   Case Else
     Wrong selection
End Select
 
Upvote 0
Top