AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [ SOLVED ] Difference between the codes? (https://forums.alliedmods.net/showthread.php?t=274625)

CrazY. 11-11-2015 15:45

[ SOLVED ] Difference between the codes?
 
What is the difference between the codes?

Code:
public fw_TakeDamage(inflictor, victim, attacker, Float:damage) {     if(is_user_alive(attacker) && g_has_weapon[attacker] && get_user_weapon(attacker) == CSW_WPN)     {         SetHamParamFloat(bla bla bla);     } }

Code:
public fw_TakeDamage(inflictor, victim, attacker, Float:damage) {     if(!is_user_alive(attacker) || !g_has_weapon[attacker] || get_user_weapon(attacker) != CSW_WPN)         return HAM_IGNORED;     SetHamParamFloat(bla bla bla);     return; }

redivcram 11-11-2015 16:10

Re: [ HELP ] Difference between the codes?
 
No difference

They both do sethamparamfloat only if the statements are true

zmd94 11-11-2015 18:15

Re: [ HELP ] Difference between the codes?
 
The main difference is at below part:
Code:

if(is_user_alive(attacker) && g_has_weapon[attacker] && get_user_weapon(attacker) == CSW_WPN)
Code:

if(!is_user_alive(attacker) || !g_has_weapon[attacker] || get_user_weapon(attacker) != CSW_WPN)
The first code will make sure the attacker to fullfill all of checks to continue to next event while the second code will allow attacker to pass if he fulfill one of the checks.

CrazY. 11-11-2015 20:02

Re: [ HELP ] Difference between the codes?
 
Which one is most recommended?

wickedd 11-11-2015 20:07

Re: [ HELP ] Difference between the codes?
 
We can't recommend anything unless you tell us what you're trying to do.

Bugsy 11-11-2015 21:18

Re: [ HELP ] Difference between the codes?
 
I would do the first one because it's cleaner and more logical.

Code 1
PHP Code:

if ( all of these things are true )
   do 
this stuff 

Code 2
PHP Code:

if ( all of these things are true 
    exit 

do 
this stuff 

Sometimes code 2 makes sense depending on what kind of stuff is going on. But for a basic 2-3 line function, definitely code #1. There is no better from a performance perspective, it's really about taste.

CrazY. 11-12-2015 06:20

Re: [ HELP ] Difference between the codes?
 
Thanks for the help :fox:

zmd94 11-12-2015 08:11

Re: [ SOLVED ] Difference between the codes?
 
Nevermind. ;)


All times are GMT -4. The time now is 17:55.

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