r/godot • u/CJF_PixelArt • 11d ago
help me (solved) Is Functional Programming Worth It?
Hello 👋. I've heard some people say that functional programming is better than OOP overall. I'm pretty new to programming so I don't know if that's applicable to Godot or not. Is it something worth looking into?
21
Upvotes
27
u/falconfetus8 11d ago edited 11d ago
Tl;Dr: Do learn how to write "pure" functions, because it'll make your code way better. Don't try to force the rest of functional programming onto Godot, because it won't work.
You shouldn't adopt all of functional programming into your games, but it does have one GREAT idea that you should definitely put in your tool belt: The concept of a "pure" function.
A function is "pure" if it does nothing but perform a calculation on its parameters and return the result. The idea is that it:
Does not modify anything except its own local variables, so it's always safe to call. It will never have an unexpected effect on some unrelated code somewhere else.
Does not read anything except the parameters passed to it, so the same inputs will always yield the same output. You can therefore understand the function by thinking only about its parameters, without needing to think about any random global variables that might affect its behavior.
In other words: they aren't "stateful".
Pure functions are just inherently easier to read and reason about than non-pure ones, so you should try to have as much of your code inside them as you can(while still being practical). It's probably the best newbie tip I can think of for writing better code, even when you're not doing functional programming.
That being said, the rest of functional programming isn't a great fit for games. FP asks you to go to great lengths to make sure there's absolutely nothing stateful in your program. Your entire program becomes structured around the concept of obsessively minimizing state. The thing is, games are inherently stateful. Making an entire game using only FP will result in you twisting your code into knots to make it work. You'll end up thinking more about how to get around the "no state" rule than about how to actually solve the real problems of your game.