Recently I’ve found myself in a position of reviewing the code of others and reading other peoples code to better my own, and I’ve come across two distinct “methods” of accomplishing a certain goal. I imagine there are no performance benefits/drawbacks to using either method above the other, but I raise the question: which is “better practice”, if one could even be considered better than the other.
It really depends on prefference and the situation.
it depends on if you’re going to require an else/elseif statement in there or if you need a function to yield if a condition is met.
There’s no “better practice” in this, you will just need to use both depending on your situation.
Also, it’s worth mentioning that if say for example, you’re running code at every stepped / heartbeat / renderstepped, then reducing the amount of operators would be the best optimization. So adding a “not” operator in the code is a tad bit more intensive as oppose to if condition then code.
again, You just need to find the right balance in both.
I’d imagine then in a situation where the intention is to have a callback if Value is false - the second method is the obvious choice, however that raises the question of why bother with the first method to begin with if the second method is inherently more versatile?
Further backed by your mention of the second one being more optimized.
returning can be more versatile in some cases, however it’s best to not over-do it, return should only be used if you want to have a callback value or if you really need a function to yield, Otherwise i would go for the latter option.
And again, in your example, you had to use a not operator, Again as mentioned, it’d be less performance intensive to use as little operators as you can (when you’re running code repetitively and really fast).
That’s a mistake some scripters make, over-using return.
This really comes down to which one you are most comfortable reading. Personally, I like to read the second one as I like all of my if statements to look exactly the same and I can easily read the code.
I highly doubt that either put very very little, if any drawbacks on performance.
Agreed, it’s really just a matter of syntatic sugar when it comes down to it - I’m sure there are some use cases that justify the direct use of one or the other but I can’t think of any directly - nor can I think of a scenario where it was detrimental for me to use one or the other.