Posted 30 September 2012 - 08:16 PM
Indentation is the practice of using whitespace to format your code. This is usually done to make code easier to read, and to figure out which code block a line belongs to.
In most editors, you'll be able to insert a suitable space with the TAB key - this will insert a number of spaces or an actual tab character. People tend to prefer spaces, but any indentation is better than unindented code.
Where do you place indentation? The norm is to indent code on a higher level (in terms of blocks) further to the right. That is, every line inside of a block (functions, if, loops) goes one level deeper.
(Note: the indentation seems exaggerated here, that's an issue with the forum engine. In reality, each "tab" would usually be 4 spaces.)
In this code, almost all the lines are at least indented one level. That's because they're inside the function (a block). Inside that function, we're making an empty table and then starting a loop. These lines both belong to the function. Then, everything inside the loop is indented further - making it obvious what belongs where and how things are being executed. Each block is on the same level as the matching "end", so we can clearly see where it begins and ends.
You're probably thinking that this seems simple. Well, yes, it really is. Unfortunately, a lot of ComputerCraft-related code seems to miss proper indentation and, as a result, is pretty hard to read.
I just wanted to bring some attention to it.
In most editors, you'll be able to insert a suitable space with the TAB key - this will insert a number of spaces or an actual tab character. People tend to prefer spaces, but any indentation is better than unindented code.
Where do you place indentation? The norm is to indent code on a higher level (in terms of blocks) further to the right. That is, every line inside of a block (functions, if, loops) goes one level deeper.
function someFunction()
local grid={};
for x=1,10 do
grid[x] = {};
for y=1,10 do
if x == y then
rows[x][y] = x * x;
else
rows[x][y] = math.random(0,99);
end
end
end
end
(Note: the indentation seems exaggerated here, that's an issue with the forum engine. In reality, each "tab" would usually be 4 spaces.)
In this code, almost all the lines are at least indented one level. That's because they're inside the function (a block). Inside that function, we're making an empty table and then starting a loop. These lines both belong to the function. Then, everything inside the loop is indented further - making it obvious what belongs where and how things are being executed. Each block is on the same level as the matching "end", so we can clearly see where it begins and ends.
You're probably thinking that this seems simple. Well, yes, it really is. Unfortunately, a lot of ComputerCraft-related code seems to miss proper indentation and, as a result, is pretty hard to read.
I just wanted to bring some attention to it.