May 14, 2015 Replies: 7 Just like the clothes you wear, the code you write will also reflect your personal style. Let's get fancy, shall we? Code: '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 Code: 'Ugly: a = a + 1 If a >= 100 then a = 100 'Elegant: a = min(a + 1, 100) Code: '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! Click to expand...
Just like the clothes you wear, the code you write will also reflect your personal style. Let's get fancy, shall we? Code: '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 Code: 'Ugly: a = a + 1 If a >= 100 then a = 100 'Elegant: a = min(a + 1, 100) Code: '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 Longtime User May 14, 2015 #2 wonder said: Code: 'Elegant: Dim sum = 1 + 1 As Int Dim validation = (sum = 2) As Boolean Click to expand... 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... Code: Toggle=Not(Toggle)
wonder said: Code: 'Elegant: Dim sum = 1 + 1 As Int Dim validation = (sum = 2) As Boolean Click to expand... 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... Code: Toggle=Not(Toggle)
MikeH Well-Known Member Licensed User Longtime User May 14, 2015 #3 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.
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.
thedesolatesoul Expert Licensed User Longtime User May 15, 2015 #4 Normally I always declare the type first so I would write it as: Code: Dim sum as Int = 1 + 1 Dim validation as Boolean = (sum = 2) I'm not sure what difference that makes.
Normally I always declare the type first so I would write it as: Code: Dim sum as Int = 1 + 1 Dim validation as Boolean = (sum = 2) I'm not sure what difference that makes.
wonder Expert Licensed User Longtime User May 15, 2015 #5 thedesolatesoul said: Normally I always declare the type first so I would write it as: Code: Dim sum as Int = 1 + 1 Dim validation as Boolean = (sum = 2) I'm not sure what difference that makes. Click to expand... 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. Code: 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.
thedesolatesoul said: Normally I always declare the type first so I would write it as: Code: Dim sum as Int = 1 + 1 Dim validation as Boolean = (sum = 2) I'm not sure what difference that makes. Click to expand... 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. Code: 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.
ivan.tellez Active Member Licensed User Longtime User May 15, 2015 #6 thedesolatesoul said: I'm not sure what difference that makes. Click to expand... 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: Code: Dim sum as Int = 1 + 1 Or Code: Dim sum = 1 + 1 As Int Are translated to: Code: byte byte0 = 0; byte0 = 2; Why byte if I declared Int? But if you chage it for this: Code: Dim sum as Int = 1 + 1 Sum = Sum + 20000 Or Code: Dim sum = 1 + 1 As Int Sum = Sum + 20000 Are translated to: Code: 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), But, the point is, you can use the type before or after, its just the same result.
thedesolatesoul said: I'm not sure what difference that makes. Click to expand... 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: Code: Dim sum as Int = 1 + 1 Or Code: Dim sum = 1 + 1 As Int Are translated to: Code: byte byte0 = 0; byte0 = 2; Why byte if I declared Int? But if you chage it for this: Code: Dim sum as Int = 1 + 1 Sum = Sum + 20000 Or Code: Dim sum = 1 + 1 As Int Sum = Sum + 20000 Are translated to: Code: 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), But, the point is, you can use the type before or after, its just the same result.
Peter Simpson Expert Licensed User Longtime User May 20, 2015 #7 thedesolatesoul said: Normally I always declare the type first so I would write it as: Code: Dim sum as Int = 1 + 1 Dim validation as Boolean = (sum = 2) I'm not sure what difference that makes. Click to expand... I too declare 'Types' first as I believe in my case it's easier for me to read naturally without thinking about it, but that's just me...
thedesolatesoul said: Normally I always declare the type first so I would write it as: Code: Dim sum as Int = 1 + 1 Dim validation as Boolean = (sum = 2) I'm not sure what difference that makes. Click to expand... I too declare 'Types' first as I believe in my case it's easier for me to read naturally without thinking about it, but that's just me...
RandomCoder Well-Known Member Licensed User Longtime User May 20, 2015 #8 I'm disappointed that @Erel hasn't shared some of his 'fancy' code. He must have plenty under the hood of B4X!
I'm disappointed that @Erel hasn't shared some of his 'fancy' code. He must have plenty under the hood of B4X!