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

Why Is It Usefull To Use Spaces In Front Of Code?

Started by Zambonie, 24 January 2013 - 12:28 PM
Zambonie #1
Posted 24 January 2013 - 01:28 PM
Most of the "famouse programer"(Like people who made an good os or something(like NFD-OS))Always use spaces in front of code.What is it used for?
Henness #2
Posted 24 January 2013 - 01:32 PM
Its so you can easily see were a section of code starts and ends, it's a standard in programing. You can easily get lost in your code when its thousands of lines long
theoriginalbit #3
Posted 24 January 2013 - 01:33 PM
whitespace actually has no functional use in modern day programming languages as the compiler just ignores it. however I ask you this, which of the following is easier to read

No whitespace:
Spoiler


local function startup()
local sw, sh = term.getSize()
clear()
if http then
updateClient()
else
drawImage( logo, 0, -2 )
setColors( colors.white, colors.red )
centerWrite( "Unable to connect to the internet", sh - 5 )
centerWrite( "ensure the http api is enabled", sh - 4 )
setColors ( colors.white, colors.black )
local timeout = os.startTimer( 1 )
local count = 10
while count ~= 0 do
centerWrite( "Press enter to quit", sh - 3 )
clearLine( sh - 2 )
centerWrite( "Continuing in "..count, sh - 2 )
local e, p = os.pullEventRaw()
if e == "timer" and p == timeout then
count = count - 1
timeout = os.startTimer( 1 )
elseif e == "key" and p == 28 then
thankYouQuit()
clear( colors.black )
error()
end
end
end
mainLoop()
end

With whitespace:
Spoiler

local function startup()
  local sw, sh = term.getSize()
  clear()

  if http then
	updateClient()
  else
	drawImage( logo, 0, -2 )

	setColors( colors.white, colors.red )

	centerWrite( "Unable to connect to the internet", sh - 5 )
	centerWrite( "ensure the http api is enabled", sh - 4 )
	setColors ( colors.white, colors.black )

	local timeout = os.startTimer( 1 )
	local count = 10
	while count ~= 0 do
	  centerWrite( "Press enter to quit", sh - 3 )
	  clearLine( sh - 2 )
	  centerWrite( "Continuing in "..count, sh - 2 )

	  local e, p = os.pullEventRaw()

	  if e == "timer" and p == timeout then
		count = count - 1
		timeout = os.startTimer( 1 )
	  elseif e == "key" and p == 28 then
		 thankYouQuit()
		 clear( colors.black )
		 error()
	  end
	 end
   end

  mainLoop()
end

EDIT: It also makes it easier to tell when you have forgot to put an end for a block.
Edited on 24 January 2013 - 12:35 PM
Zambonie #4
Posted 24 January 2013 - 01:36 PM
Is there a specific way to use whitespace or everyone has there own way of using it?
theoriginalbit #5
Posted 24 January 2013 - 01:37 PM
everyone normally varies… some use 2 space tabs, some use 4 space tabs, etc.

also there are some languages, such as Python that require correct indenting to delimit functions

but convention says that indentation is done in a block, i.e. if, while, repeats, functions etc

read this http://lua-users.org/wiki/LuaStyleGuide
Zambonie #6
Posted 24 January 2013 - 01:50 PM
So is this maybe a good example?: (Im using my death o meter in my sign.)

print("Is P4 awsome?")
  local input = read()
    if input == "Yes." then
	  print("Well good!Ill give you a diamond out of that dispenser!")
	    sleep(1.5)
		  redstone.setOutput("right", true)
		    sleep(1)
			  redstone.setOutput("right", false)
			    else
				  print("Well,Il atleast give you something..")
				    sleep(3)
					  print("LAAAAAAAAAAVAAAAAAAA!!!!")
					    redstone.setOutput("top", true)
						  sleep(7)
						    redstone.setOutput("top", false)
					   end
						 sleep(10)
						   term.clear()
							 term.setCursorPos(1,1)
							  print("Thank you for using P4's Death-O-Meter!")
Kingdaro #7
Posted 24 January 2013 - 02:30 PM
I'll be honest, that's probably worse. The general rule in tabbing is to tab after an if/for/while/repeat statement, and remove a tab on an end.


if condition then
  while condition do
    for i=1, 19 do
      repeat
        stuff()
      until condition
    end
  end
end

More information on commonly accepted tabbing styles (the Formatting section): http://lua-users.org...i/LuaStyleGuide
Cloudy #8
Posted 24 January 2013 - 02:32 PM
snip

*SCREAMS*
theoriginalbit #9
Posted 24 January 2013 - 02:48 PM
More information on commonly accepted tabbing styles (the Formatting section): http://lua-users.org...i/LuaStyleGuide
Haha same link I suggested :P/>


Couldn't agree more!
dissy #10
Posted 24 January 2013 - 03:59 PM
P4isHere:

Great example!
I took your program and its spacing, and copy/pasted it 10 times.
Here it is: http://pastebin.com/wahjfhj0

I've changed one "end" command in just one of those 10 copies. It should take you no more than 5 seconds to find it.

Reply back with which "end" line number I changed, and how long it took you to find it. Remember, it should take no more than 5 seconds to do this!
Every second and minute longer than the first 5 seconds is the answer to your question "why is it useful" :}