AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Checking pcvar/cvar - what method is fastest / best (https://forums.alliedmods.net/showthread.php?t=61429)

mordi 09-30-2007 10:36

Checking pcvar/cvar - what method is fastest / best
 
When checking for a pcvar or a cvar in the start of a function, should i use:

Code:

public function()
{
    if(get_pcvar_num(pcvar))
    {
        something
    }
}



or...

Code:

public fuction()
{
    if(!get_pcvar_num(pcvar))
        return PLUGIN_HANDLED
       
    something
   
}

What is the most efficient way?

Lee 09-30-2007 11:33

Re: Checking pcvar/cvar - what method is fastest / best
 
I agree with Will Shipley.

Quote:

What you should really do is write "if" statements that check for improper conditions, and if you find them, bail. This cleans your code immensely, in two important ways: (a) the main, normal execution path is all at the top level, so if the programmer is just trying to get a feel for the routine, all she needs to read is the top level statements, instead of trying to trace through indention levels figuring out what the "normal" case is, and (b) it puts the "bail" code right next to the correctness check, which is good because the "bail" code is usually very short and belongs with the correctness check.

The performance difference will be negligible to say the least. It might be worth noting that you only need to return a value if it's actually going to be used.

Most importantly, ignore vl@d.

mordi 09-30-2007 12:23

Re: Checking pcvar/cvar - what method is fastest / best
 
Thanks Lee!
I'll use this then:
Code:

public fuction()
{
    if(!get_pcvar_num(pcvar))
        return PLUGIN_HANDLED
       
    something
   
}



All times are GMT -4. The time now is 16:09.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.