Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Are there some rules or tips of formatting code?

It's definitely not the first time I've heard; "Bro, your formatting is terrible", also like a 1/2 of my tutorials on; LUA LEARNING by boatbomber got declined because lack of formatting;

I don't know why It's so important, would someone explain why It's important and some rules and tips?

1 answer

✓ Accepted answer

Formatting helps you read your code better and helps you skim. It helps you learn where to place your ends and also commonly helps you know if there's an end missing, as any block that creates a new end keyword also requires an extra 8-width tab.

Think of it like this, right:

function doThisCode(...)
if ... then
print("yes")
end
if otherthing then
print("no")
Character:SetPrimaryPartCFrame(
CFrame.new(
0,
15,
0
)
)
end
end

looks fairly awful, doesn't it? Now, let's look at it with indentation:

function doThisCode(...)
	if ... then
		print("yes")
	end
	if otherthing then
		print("no")
		Character:SetPrimaryPartCFrame(
			CFrame.new(
					0,
					15,
					0
				  )
		)
	end
end

Looks better. Now, let's say you have screwy indenting and you're, let's say, missing an end on a connection:

PlayerAdded:Connect(function(Player)
function doThisCode(...)
pcall(function()
if ... then
print("yes")
end
if otherthing then
print("no")
Character:SetPrimaryPartCFrame(
CFrame.new(
0,
15,
0
)
)
end
end
end
Player.CharacterAdded:Connect(function()
doThisCode()
end)
end)

Where are you going to find the error without compiling? It looks terrible!

Now once we visualize the code in a better-indented form:

PlayerAdded:Connect(function(Player)
	function doThisCode(...)
		pcall(function()
			if ... then
				print("yes")
			end
			if otherthing then
				print("no")
				Character:SetPrimaryPartCFrame(
					CFrame.new(
							0,
							15,
							0
						  )
				)
			end
		end
	end
	Player.CharacterAdded:Connect(function()
		doThisCode()
	end)
end)

Oh wowie, we can tell that the pcall function isn't closed because we know that I gave it an anonymous function but we didn't close the parenthesis to supply it as an argument! We can tell because the ends line up very well, but there's no parenthesis meaning that it would error.

tl;dr it's just better to read and it helps debugging become alot quicker

1 comment

0

Wow! That must of took a lot of effort, thanks!!

This discussion is preserved as it was. New answers, comments, and votes are closed.