Page 2 of 2

Re: Having *unique* objects on screen

Posted: Wed Aug 03, 2016 4:23 am
by Positive07
I usually write my code as libraries so it lets me do testing specs and reuse lots of it later on.

I usually do the check by myself so I can provide correct errors (This is for a utf library that complements utf8):

Code: Select all

utf.sub = function (str, a, b)
	-- Default for finish is the end of the string
	local start, finish = tonumber(a), tonumber(b or -1)

	-- Type checking
	if type(str) ~= "string" then
		error("bad argument #1 to 'sub' (string expected got "..type(str)..")", 2)
	elseif type(start) ~= "number" then
		error("bad argument #2 to 'sub' (number expected got "..type(a)..")", 2)
	elseif type(finish) ~= "number" then
		error("bad argument #3 to 'sub' (number expected got "..type(b)..")", 2)
	end

	local len = u.len(str)

	-- Negative values count from the end
	-- Minimum value for start is 1 and maximum value for finish is len
	start  = math.max(start  < 0 and len + 1 + start  or start,	1)
	finish = math.min(finish < 0 and len + 1 + finish or finish,	len)

	-- Start comes after finish
	if start > finish then return "" end

	-- This lines fixes the position so that they are in bytes for string.sub
	return string.sub(str, u.offset(str, start), finish == len and -1 or u.offset(str, finish + 1) - 1) or ""
end
This error are pretty much the same as Lua errors, they handle default values and valid numbers (to Lua 10 and "10" are both valid numbers, except when the argument accepts a string in which case the value is interpreted as string and not converted to a number), you can also check multiple types and perform modifications to the data before performing the check and the thing I like the most is that 2 passed as second argument to error.

Asserts make the function error and the error report points to the line where the assertion is, so if you want to see what called that function you have to check the stack overflow, the 2 in the error function actually tells Lua to pop 1 from the stack and instead of saying that the error is there, it tells you that the error is where you called the parent function

Also note that this is my coding style for functions, functions do one thing and one thing only and as I said before, files are modules, each handle one thing and can be used, pretty much like libraries (except most of the time they depend on other modules)

Re: Having *unique* objects on screen

Posted: Wed Aug 03, 2016 8:15 am
by zorg
Positive07 wrote:(to Lua 10 and "10" are both valid numbers, except when the argument accepts a string in which case the value is interpreted as string and not converted to a number)
To be a bit more precise, lua's not a statically typed language, so the function, nor the program will ever know what type of parameters you want to be passed into the function; as Positive07 wrote in the code snippet, he tests for types. It also won't implicitly convert from string to numbers (or vice versa even), he uses the tonumber() function, so that strings that may represent correct numbers will be converted to numbers.
Positive07 wrote:if you want to see what called that function you have to check the stack overflow.
Or just check the traceback in the error message, and don't bother the people on that site with trivial things :3

Oh, and "correct errors" are a bit of an oxymoron, i'd call it "accurate errors", but i digress.

Re: Having *unique* objects on screen

Posted: Wed Aug 03, 2016 12:50 pm
by Sulunia
To each their own style huh.
I usually just want to avoid the "galaxy traveling nil" as i call it (when the error that happens somewhere originated on a whole other place).

Still, nice usage of the "error()" function, i actually didn't know that was possible.

Re: Having *unique* objects on screen

Posted: Sat Aug 06, 2016 4:00 am
by Positive07
zorg wrote:
Positive07 wrote:if you want to see what called that function you have to check the stack overflow.
Or just check the traceback in the error message, and don't bother the people on that site with trivial things :3
I prefere to keep my stack traceback clean (plus I have some code snippets in my setup that let me go to the exact file and line the error reports so pointing to the right place helps!)

I should have used accurate you are right, didn't find the word at that time.

And yes, great description of what the function is doing, entirely right! Thanks