Benefits of stateful programming?

I wondered about the benefits of stateless programming and found the one who shared my question: Benefits of stateless programming?

as I read the answers, although it made me think about the opposite question. What are the benefits of stateful programming? It seems that lately a lot of attention has been paid to codes without apathy, but I am afraid of trends.

it seems that stateful programming (i.e. imperative) can respond to certain scenarios better than factorless (i.e. functional) programming, and I would like to better understand what problems can be solved using stateful programming.

+7
source share
3 answers

There are only a few cases where there are undeniable advantages for a programming model based on a mutable, shared state compared to an immutable stateless model. One area where variability can bring huge benefits is for algorithms to work in place. The haskell wiki has a great example of implementing quicksort: http://www.haskell.org/haskellwiki/Introduction#When_C_is_better

To summarize it, if you do not allow changes in the memory of lists, you need to create a sorted copy of it. The same is true for almost any other algorithm that modifies some data structure, for example. AVL tree.

In general, functional programming languages ​​tend to be more memory intensive than their imperative counterparts. Memory is cheap these days, but the bandwidth is critical, and the memory speed does not increase in proportion to the increase that we see in the processor power. It should be noted, however, that the Haskell runtime model allows the compiler to perform some nifty optimizations, also regarding memory usage and access patterns. To some extent, this can compensate for theoretical flaws.

+6
source

Clarity is the key. I like functional programming (I am currently in clojure binge), but state is how the world works. It is no coincidence that an object-oriented paradigm is more popular than functional or any other type of programming. OOP and procedural programming are the path of least resistance for new programmers. Most people intuitively understand the idea of ​​an object as something that changes state (it can move, change color, etc.), rather than Y-combinator, which helps with recursion.

+1
source

The state is important. Each bit in this universe has a state, it is the state that determines how the system behaves, the state makes the system dynamic and usable, but since the state is so important, it is also important that this state is accessed and manipulated. It would not be good if a person could manipulate another person’s state (a state as a content in a person’s brain). My view of the state is that it should be explicit and should not be something that is scattered around your code and it becomes very implied that it is difficult to understand what state the system is in and what part of the system is responsible for that part state. The state should be controlled in such a way that you can easily say that this part of the system state is processed by this module and only this module.

In any real FP program, there will always be two parts, one of which is not stateless, and this will be the main algorithm, etc. and the other part that will maintain the state of the program.

+1
source

All Articles