How to indent properly.


  • I see this an issue for lots of people. They don't know how to indent properly or they're stubborn and choose not to. Studio auto indents and I am still confused.

    Indentation is adding spaces or tabs to shift line to the right.

    # Python example
    if True:
        print("Hi") # Indent example
    

    We indent so that code is readable. And in Lua, makes it almost impossible to miss an end.

    Increase indentation when:
    You are in a function() ... end block.

    (function()
        print("Hey people")
    end)()
    

    You are in a while condition do ... end block.

    while true do
        print("I am printing forever!")
    end
    

    You are in a repeat ... until condition block.

    repeat
        print("I also print forever!")
    until false
    

    You are in an if condition then ... end block.

    if 1 + 6 < 10 then
        print("I only print if 7 is smaller than 10!")
    end
    

    You are in a for ... do ... end block.

    for i = 1, 10 do
        print("I print 10 times.")
    end
    
    for k, v in pairs(t) do
        print(k, v)
    end
    

    You are in a do ... end block.

    do
        print("I am in a different scope!")
    end
    

    Decrease your indentation when you exit one of those blocks.

    Also another thing to note, do not increase/decrease indentation upon a function call or variable assignment

    foo()
        -- # code (bad indentation)
    ---------
    foo()
    -- # good
    
    local var = 8
        -- # bad indentation
    -- # good
    

    And if you ever have nested blocks

    -- # Good indentation 
    while true do
        if true then
            print(true)
        end
    end
    
    -- # bad indentation
    while true do
    if true then
    print(true)
    end
    end
    
    -- # bad
    while true do
        if true then
        print(true)
    end
    end
    -- # etc
    

  • Too bad the majority of the people who need this guide wont even look at the forums.


  • @DinozCreates Im here

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.