Android Tutorial Fancy Code

Discussion in 'Tutorials & Examples' started by wonder, May 14, 2015.

  1. wonder

    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? ;)
    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 = 2As Boolean
    Code:
    'Ugly:
         a = a + 1
         
    If a >= 100 then a = 100

    'Elegant:
         a = min(a + 1100)
    Code:
    'Ugly:
         a = a - 1
         
    If a <= 0 then a = 0

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


    [​IMG]
     
    ilan and Cableguy like this.
  2. RandomCoder

    RandomCoder Well-Known Member Licensed User

    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)
     
    Peter Simpson, Cableguy and wonder like this.
  3. MikeH

    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.
     
    RandomCoder and wonder like this.
  4. thedesolatesoul

    thedesolatesoul Expert Licensed User

    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.
     
    Peter Simpson and wonder like this.
  5. wonder

    wonder Expert Licensed User

    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
    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. :rolleyes:
     
    Cableguy and thedesolatesoul like this.
  6. ivan.tellez

    ivan.tellez Active Member Licensed User

    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? o_O


    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 += 
    20000return "";

    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.
     
    wonder and thedesolatesoul like this.
  7. Peter Simpson

    Peter Simpson Expert Licensed User

    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...
     
  8. RandomCoder

    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! ;)
     
    Cableguy likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice