This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
robhol's profile picture

Tutorial: indentation

Started by robhol, 30 September 2012 - 06:16 PM
robhol #1
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.

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.
Orwell #2
Posted 05 October 2012 - 11:50 PM
My first thought on this was that identation is a bit of a simple subject to write a tutorial on. But afterwards I realised that it is indeed something that lots of people lack and that it helps a lot for both yourself and others to know this. So I wan't to be the first to say that you did a good job on writing this. : )
Cranium #3
Posted 06 October 2012 - 12:12 AM
Ha! I laugh at your silly notion of what is proper and established! I devise my own system that makes a mockery of yours!
I call it…..REVERSE INDENTATION!!!!!!

			    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
MWUAHAHAHAHA!!!!!!
Orwell #4
Posted 06 October 2012 - 12:15 AM
Ha! I laugh at your silly notion of what is proper and established! I devise my own system that makes a mockery of yours!
I call it…..REVERSE INDENTATION!!!!!!

				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
MWUAHAHAHAHA!!!!!!

Surprisingly, it seems to work for me. x'D
robhol #5
Posted 07 October 2012 - 01:46 PM
http://nooooooooooooooo.com/

Well, of course it works, it's just whitespace. It is, however, a crime against nature. :D/>/>

And yes, it is bloody simple, which is why it kind of pisses me off that people don't DO it - I mean, it's common courtesy to make your code reasonably readable.
Orwell #6
Posted 07 October 2012 - 02:22 PM
With 'it works' I mean that the inverted identation is still readable. You'll find that all my code on the forums has identation : )
CoolisTheName007 #7
Posted 09 October 2012 - 01:22 AM
Well, seems I kepp falling on Orwell posts!!
One reason for bad indentation in-game is that it gets stripped when copying stuff to servers (eg. your program, Orwell, which is the best thing I found to deal with it) if people don't take care with converting tabs to spaces.
I managed to do it now by using Notepad (plus) (plus).
ChaddJackson12 #8
Posted 09 October 2012 - 01:40 AM
Well. Not that people may lack it. But. What happens with me is when I paste my code with indents in the forums. The indents are taken out of it and Since my code can be long. I don't wanna go fix it. :3

So part of it is probably the forums.