' if ' technique

Started by habi, Apr 03, 2020, 02:39 AM

Previous topic - Next topic

habi

Consider
                     if( player && player.IsSpawned )    // line 97

I always feared that this throws an error in the case player is null ( i.e. by that time player quits or something )
So null.IsSpawned and error happens. So i used to put two if lines: one for checking player is not null and second for checking player.IsSpawned.

But today after reading about the order of execution of if statement and some testing, i think line 97 is perfectly okay. Because it says the second condition is evaluated only if the first condition is true, so player is not null.

So no need of two if statements! One will do.
Hope it helps. 8)

umar4911

It's because you are using the AND operator,  not OR. It's the purpose of AND operator to make sure that both are true. if(player) means to check if player is not null.
RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter

Xmair

Quote from: umar4911 on Apr 03, 2020, 06:18 AMIt's because you are using the AND operator,  not OR. It's the purpose of AND operator to make sure that both are true. if(player) means to check if player is not null.
You clearly don't get what he's saying.

This is pretty basic but yeah it's surely useful for some people.

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD