Why functional programming is not so often used to write games?
Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants aleksandr barakin , Nick Volynkin ♦ 20 Apr '16 at 14:12 .
The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .
- 2What is functional programming often used for? Not many people even know what it is. - Sergey
- Debugging, multithreading, for example. - user81737
- With debugging, and so the order, and multithreading is not needed on GP in games very rarely. - D-side
- They are high enough, understandable and simple. - user81737
- oneBecause most examples and technologies use the historically established imperative style to which everyone is accustomed. Therefore, for functional programming, you will have to get used to both styles, translate existing examples into AF, find good tools supporting AF and find a community that can still help with all this. Too many conditions for switching to FP and too little feedback from such steps. - Alex Krass
1 answer
tl; dr: it’s not popular, it’s hard to find specialists, there’s not enough materials.
There are several reasons, mainly historical and temporary .
Historically, the imperative style began to be applied earlier, since there are fewer transformations from programs in this style to a machine-readable understanding. Therefore, the main share of games was written in this style. The region developed, acquired architecture, being built on the achievements of predecessors, which did not use the functional style, as a rule.
Not without exception, of course. Abuse (1995) and some Naughty Dog games are written with a lot of Lisp . How much "functional" is used there is questionable, but if not, it is not clear why there was any use of lisp at all, right?
Now the use of functional programming in the entire software development industry (including games) is quite rare. “Big igrostroy” refuses it, because the specialists are expensive : it happened with Naughty Dog when they were bought by Sony, they were forced to refuse, saying that “you will not work otherwise with ours”. The same applies to the players-modelers - "functionals" among them very few.
In many ways, this relies on how programmers become in our time . It's not a secret that many have become programmers from the desire to engage in the development of games (I am one of them), and if you have the desire, you begin to search for materials yourself. And come across those materials that are most in the district. And usually they are not about functional programming.
Functional programming is cool, but “nobody uses it in practice” because there are no specialists (who are profitable to hire), and they do not appear because there is no demand for them. The situation looks like a vicious circle that can be broken, gradually teaching professionals to work in a functional style, which requires appropriate technologies that can be introduced gradually (to develop a motive), and then training materials (to support the motive).
But if you forget about all this, you need to understand that the functional area is not a silver bullet that can do anything. And it is appropriate far from everywhere. It is perfect for describing the strict logic of a small volume , where performance will not soar much.
Performance basically kills allocations , which are very typical for functional style because of the preference for persistent data structures , if there are not many of them, there will be no problems.
The situation may change if someone implements a powerful embedded functional language that optimizes working with memory. Theoretically, this could be a prediction of where it will be released and maximum reuse of what already is, simple algorithms generated by the compiler right on the spot.
In order for the game to work at 60 frames per second, there is only about 16 milliseconds per frame, so the basis of the game is better to write on what works well and works smartly : if it is predictable, then the code in the functional style will work fine and the performance will not be upset will be. "Functional core, imperative shell" ( Gary Bernhardt, Boundaries )
In the work, this combination can be seen, for example, in re-natal , the bright React Native, connected to the functional ClojureScript. But even the popularity of React Native itself is not transcendental, people do not believe that pushing the whole JS runtime into the application will be of benefit to it.
Some interesting reports with ClojureTV on the topic:
- Yes, those projects about which I wrote earlier were most likely written in clojure, and yes, nevertheless, as an alternative to lua, it seemed to me a good option. - user81737
- 2@Naumov okay, indicate where I confused them, because I don’t have the word "procedural" in general at all. - D-side
- one@vas (a) and (c) are directly related to the FP, and I explained to you an hour ago what :) Perhaps you meant the
ifandletconstructions from a particular language, in this case the language was worth pointing out. Context is important. - D-side - 2@vas and I'm not talking about JS, the question is much more general, the author did not connect it with JS :) And the author, as you see, arranged the answer. JS really has almost nothing to do with OP. And about local variables, mind you, I did not say anything, it was you who came up with it. Binding is not a variable, it is set at the time of creation and then it exists only for reading. Local variables in the theory can be treated in the same way, it is a feature of the functional style, but since the language allows you to change them, you cannot rely on it. - D-side
- one@vas is already a question for the author, why he mentioned JS and why he accepted my answer, despite the absence of references to it. And
letI understand the meaning used in Haskell and Clojure: in them an attempt to associate another value with an already associated name can only be when entering a new scope (recursive call,letexpression or something else), and when leaving it, will come back "(the binding will act more earlier), the state cannot be stored in it. In JS, this is just a local variable with a couple of additional rules. We are just talking about different things. - D-side