Author Topic: ASSERT  (Read 1435 times)

0 Members and 1 Guest are viewing this topic.

[NL]Pur

  • Guest
ASSERT
« 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 ?

PtokaX forum

ASSERT
« on: 03 May, 2004, 23:53:49 »

Offline NotRabidWombat

  • Lord
  • ***
  • Posts: 384
  • Karma: +2/-0
(No subject)
« Reply #1 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:
Code: [Select]
      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:
Code: [Select]
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


I like childish behavior. Maybe this post will be deleted next.

[NL]Pur

  • Guest
(No subject)
« Reply #2 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 ;)

PtokaX forum

(No subject)
« Reply #2 on: 04 May, 2004, 11:38:28 »