Android Tutorial Fancy Code

wonder

Expert
Licensed User
Just like the clothes you wear, the code you write will also reflect your personal style.
Let's get fancy, shall we? ;)
B4X:
'Ugly:
     Dim validation As Boolean
     Dim sum = 1 + 1 As Int
     If sum = 2 Then validation = True

'Elegant:
     Dim sum = 1 + 1 As Int
     Dim validation = (sum = 2) As Boolean
B4X:
'Ugly:
     a = a + 1
     If a >= 100 then a = 100

'Elegant:
     a = min(a + 1, 100)
B4X:
'Ugly:
     a = a - 1
     If a <= 0 then a = 0

'Elegant:
     a = max(a - 1, 0)
Do you have some more fancy examples of elegant B4X code?
Share it with us!


 

RandomCoder

Well-Known Member
Licensed User
B4X:
'Elegant:
     Dim sum = 1 + 1 As Int
     Dim validation = (sum = 2) As Boolean
I struggle to get my head around this. I think it's because in other languages I program in a single "=" is assigning a value whereas a"==" is comparing values.

I like your other examples too. The only basic one I use regularly is the flip flop...
B4X:
Toggle=Not(Toggle)
 

MikeH

Well-Known Member
Licensed User
I use the toggle one a lot and also, instead of:

if (whatever) = true/false then...

I use

if whatever then... or.... if not(whatever) then...

and they complement each other better than using true/false.

My coding isn`t done until I`ve looked at every line and tried to reduce it down to its simplest form.
 

wonder

Expert
Licensed User
Normally I always declare the type first so I would write it as:

B4X:
Dim sum        as Int      = 1 + 1
Dim validation as Boolean  = (sum = 2)
I'm not sure what difference that makes.
It should be completely ambiguous.

When coding, I like pretend that I'm a Roman Emperor and follow the sentence structure which seems more natural to me. ;) :D
B4X:
Dim sum = 1 + 1 As Int 'Hereby I declare "sum", the container of "1+1", as an integer.
Dim sum As Int = 1 + 1 'Hereby I declare "sum", as an integer, the container of "1+1".
Then again, English isn't my native language. :rolleyes:
 

ivan.tellez

Active Member
Licensed User
I'm not sure what difference that makes.
I allways do the same as you. Didnt know the type could be placed last LOL.

ABout the difference, you can check the generated java code:


B4X:
Dim sum        as Int      = 1 + 1
Or

B4X:
Dim sum  =    1 + 1       As Int
Are translated to:

B4X:
byte byte0 = 0;
byte0 = 2;
Why byte if I declared Int? o_O


But if you chage it for this:
B4X:
Dim sum        as Int      = 1 + 1
Sum = Sum + 20000
Or

B4X:
Dim sum  =    1 + 1       As Int
Sum = Sum + 20000
Are translated to:

B4X:
int i = 0;
i = 2;
i += 20000; return "";

So there are some strange compiler optimizations. It analize the use of the vars, changes the Type and skips completly the operation (1+1), :eek:


But, the point is, you can use the type before or after, its just the same result.
 

RandomCoder

Well-Known Member
Licensed User
I'm disappointed that @Erel hasn't shared some of his 'fancy' code.
He must have plenty under the hood of B4X! ;)
 
Top