PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Make Your Code Easier to Read

Make Your Code Easier to Read

Jul 02 2018, 5:12 PM PST 2 min

Whitespace is a type of character used to make code easier to read. Spaces, tabs, and newlines are considered whitespace characters. Whitespace characters are, for the most part, ignored by the Lua compiler. One uses whitespace to help improve the readability of their code. White space can be used to:

  • create a visual separation between the end of one function and the beginning of the next.
  • help identify code contained by if statements, while loops, and other code block constructs.
  • make it easier to scan a list of variables

Just to name a few. Let’s look at some examples:

Spaces

Without:

number=476
disgusting_food="fried chicken"

With whitespace added:

number          = 476
disgusting_food = "fried chicken"

Tabs

Tabs are used to organize scripts after:

  • do
  • then
  • repeat

Without tabs:

if 1 == 1 then
while true do
print("One is equal to one!")
wait()
end
else
print("OMGHAX.")
print("A paradox is coming!")
end

With tabs added:

if 1 == 1 then
  while true do
    print("One is equal to one!")
    wait()
  end
else
  print("OMGHAX.")
  print("A paradox is coming!")
end

Newlines

Lua will also ignore new lines in some parts of your code. However, this can lead to ambiguity problems (see below).

Without newlines:

print("test")

With newlines:

print(
"test"
)

Both output the same thing: “test”.

When whitespace isn’t ignored

If you put extra whitespace in a string, the whitespace won’t be ignored.

codeprint("Hello world!") 
print("Hello            world!")
print("Hello world!" == "Hello            world!") 

Will produce:

Hello world!
Hello            world!
false

Ambiguities

Because Lua ignores newlines, this can lead to certain problems because the compiler is unsure about what you’re trying to do, so it will warn you when you’re about to run into trouble.

Tags:
  • \