PtokaX forum

Development Section => Your Developing Problems => Topic started by: [NL]Pur on 03 May, 2004, 23:53:49

Title: ASSERT
Post by: [NL]Pur on 03 May, 2004, 23:53:49
My question(s) is about Assert,

Is it wise to assert a other script in the NewUserConnected event or other very often triggerd event?

What exactly happens when u use assert.

does assert uses alot of CPU ?
Title:
Post by: NotRabidWombat on 04 May, 2004, 05:09:08
It is wise to always use assert in major functions while a script is in development.

The lua manual describes assert as:
      function assert (v, m)
         if not v then
           m = m or ""
           error("assertion failed!  " .. m)
         end
       end
As you can see, it's nothing more than an if statement, requiring very little resources.

It is good practice to add asserts to any useful code snippets that you make.

On a side note, do not use string concatation with assert such as:
assert( num1 == num2, num1 .. " does not equal " num2);
String concatation occurs _BEFORE_ the assert statement is evaluated, so this operation will happen even if the assertion is succeeds.

You should use instead:
function fast_assert(condition, ...)
    if not condition then
        if getn(arg) > 0 then
            assert(condition, call(format, arg))
        else
            assert(condition)
        end
    end
end
Read more about fast_assert and other code optimization at: http://lua-users.org/wiki/OptimisationCodingTips

-NotRabidWombat
Title:
Post by: [NL]Pur on 04 May, 2004, 11:38:28
tx , for info

wasn't totally sure about assert before you posted,

Currently i'm trying to put every function in sepperate lua file, and trying to use tables as classes.

To get more of a programming language idea then a scripting language ;)