Jump to content

Complex If statement processing


Recommended Posts

With a bunch of complex AND logical tests in a single If statement, does the papyrus script engine bail on the first test fail, or process the whole line, so better to nest them ?

 

Neat for code maintenance:

 

If (Simple logical test A) && (Simple logical test B) && (Simple logical test C) && (Complex array lookup logical test D)

;do stuff

EndIf

 

Better for performance ?

 

If (Simple logical test A)

If (Simple logical test B)

if (Simple logical test C)

If (Complex array lookup logical test D)

;do stuff

Endif

Endif

Endif

Endif

 

 

Or halfway split simple and complex ?

 

If (Simple logical test A) && (Simple logical test B) && (Simple logical test C)

If (Complex array lookup logical test D)

;do stuff

Endif

Endif

 

Because performance is a virtue.

Link to comment
Share on other sites

I'm pretty sure the compiler takes care of that - if the first condition fails the others are not evaluated. There is a Papyrus performance page on the CK Wiki: https://www.creationkit.com/fallout4/index.php?title=Performance_(Papyrus)#Fail_fast.2C_fail_early

It's not the compiler, but the interpreter/vm/whatever you want to call the part which actually runs the .pex files </nitpick>

 

I think it can be safely assumed that most programming languages are able to do this sort of short-circuiting, but thank you for confirming it nonetheless.

 

That said, I sometimes make several ifs with return statements instead of one single one for the sake of readability. But also if I need a variable for one of the checks:

function doStuff(ObjectReference someRef)
    
    if(!someRef)
        return
    endif
    
    ObjectReference player = Game.GetPlayer()
    if(someFunction(player, someRef) = 0) 
        return
    endif
    
    ...


endfunction
Link to comment
Share on other sites

Thanks Foxy, with the lack of proper sdk specs and docs, optimisations are rather opaque. I shall find some time to setup a benchmark for this, a useful investment in a (probably) sunset platform ...

 

@pra I try and avoid using return as that just leads to me creating complex nested chains of recursive functions that stack and crash.

Link to comment
Share on other sites

 

@pra I try and avoid using return as that just leads to me creating complex nested chains of recursive functions that stack and crash.

Wat? How?

 

I always try to return as soon as possible. If I know that the function can't do anything sensible anymore, I make it return right away. The alternative would be to create complex nested if/else constructs, and having an extra variable for the return value, if any. Which I find incredibly ugly and hate Pascal (fo4edit scripting language) for trying to enforce it.

Link to comment
Share on other sites

I'm talking papyrus. not using return forces me to chunk my functions and use events rather than create thousand line monster functions. its probably philosophical ...

Link to comment
Share on other sites

I personally find the halfway one to get real confusing when it's more than 2 && conditions, with each check being a condition in itself(A > B && A < D && C > D etc..). If chains I can see a "staircase" clearly and not get lost. If there were switches though, I'd probably never need a long if chain. Maybe in the next game..

Edited by Rasikko
Link to comment
Share on other sites

 

I'm pretty sure the compiler takes care of that - if the first condition fails the others are not evaluated. There is a Papyrus performance page on the CK Wiki: https://www.creationkit.com/fallout4/index.php?title=Performance_(Papyrus)#Fail_fast.2C_fail_early

It's not the compiler, but the interpreter/vm/whatever you want to call the part which actually runs the .pex files </nitpick>

 

True enough, since the compiler can't exactly foresee what the variables are going to be at runtime. :tongue:
Link to comment
Share on other sites

 

 

I'm pretty sure the compiler takes care of that - if the first condition fails the others are not evaluated. There is a Papyrus performance page on the CK Wiki: https://www.creationkit.com/fallout4/index.php?title=Performance_(Papyrus)#Fail_fast.2C_fail_early

It's not the compiler, but the interpreter/vm/whatever you want to call the part which actually runs the .pex files </nitpick>

 

True enough, since the compiler can't exactly foresee what the variables are going to be at runtime. :tongue:

 

It could do compile-time optimisation, though. For example, if you literally write "if (foobar && true)" it could optimize the second part away. A more advanced compiler could also understand something like "bool foo = true; if(foo)".

 

But I doubt this bethesda compiler could do anything like this.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...