Hi, maybe this question has been asked already, and it's not necesarily a problem, maybe a "philosophical caveat".
As the title implies, what performance issues may the excessive use of "if" conditionals pose?, i know that from a maintenance standpoint it might complicate things a bit, and i always try to splice things a bit by using things like recursion or switch/match and such, but somehow, i always go back to using "if"s, and i'm worried that it not only looks unprofessional, but it might also cause performance issues for a finished game
Using "if" excessively
If is one of the most commonly used control structures in most languages. Nothing unprofessional about it per se. Also generally no reason to worry about performance (at least not if you write something as high level as GDScript or C#).
Of course if you use a lot of repetitive if statements that might be an issue (like any other form of repetitive code can be an issue). But without seeing some code sample it is hard to tell what you mean by excessive if.
- Edited
Xrayleader cause performance issues
Yes.
If it's two, three or even four ifs, it doesn't matter. If it's more you should use match.
What you evaluate is also important. A simple bool or a single logical evaluation like ==
or <
is ok, but testing if a string is contained in another, or multiple evaluations in the same line will have a greater performance impact.
The most important part is WHEN this happens. If it's done ONCE there will be no problems, but done on multiple nodes or in the process function it WILL affect performance.
Xrayleader i always go back to using "if"s
You should study some programming patterns.
I sometimes see ppl posting code that’s bogging down the game: it’s a ton of ifs written in the _process or _physics_process functions. Optimize code that’s running every frame. If statements are slow because the computer doesn’t know which instructions to send to the CPU for timely execution.
If you’re just doing a little math inside an if conditional, it’s often more efficient to just calculate the math every time and ignore the if altogether.
Premature optimization is the root of all evil. Don't worry about it, until and if the profiler is telling you that you should worry about it. Until then it is more important to write clean and maintainable code.
Xrayleader if is useful, but it's one sided logic. Think of "if" like a switch. It only go two ways. One way fails, the other does not. The more "ifs" you get, the higher the probability of an error, because, for instance, if you need three conditionals to make something happen, chances are you can muck it up easily if one conditional (if) is wrong. Usually a state machine is the way to go. (Which works like a match system as @Jesusemora mentioned and I will second his advice.)
"If" will always be useful, but it's a small tool out of hundreds. It's right for small, easy things but when you get to bigger things, you will want to get more efficient in your coding so you don't get frustrated.
However, what I say is make sure your code is "working" first before optimizing. Once you know how it works and why, it is much easier to streamline.
Just as an extra: It does make a difference whether you use ifs in your game logic code (GDScript/C#) or in your shader code. The gpu doesn't really "like" branching, as its architecture is "not built around that". In general it's better to use a calculation instead of branching code for shaders.
However sometimes it can be useful simplify your game logic code with a proper calculation as well. Just depends on how complex the calculation vs the branching of if statements is.
- Edited
Xrayleader but it might also cause performance issues for a finished game
Not directly. If
per se is very efficient. It maps directly to an atomic cpu instruction. However, calculating various conditions may end up being inefficient depending on what and how you do it. Also, having excessive if
s in the code may indicate that your algorithm or its implementation is not well though out.
That all being said, you should never worry about code efficiency in advance. When the performance become unsatisfactory, only then try to look for ways to speed it up. Start by finding actual bottlenecks using the profiler.
SnapCracklins My goodness, i know what a state machine is and it never ocurred to me that "Switch"es or "Match"es could be used as such!, thanks XD XD XD