Move Over OOP & Functional, Think about State?Machines
Someone quoted this ??
If debugging is the process of removing bugs, then programming must be the process of putting them in.
A brilliant statement which sums up the reasons behind origin of programming paradigms like Object Oriented and Functional.
These paradigms are intended to make sure that we reduce the chances of creating bugs, while writing the code. The success of which can be debated, but in a nutshell all of them are useful in writing relatively better code, but, there is a catch…!!!
One has to be more artificial than natural to think about objects, functions and their relationships. It’s not Intuitive.
Don’t get me wrong, but we don’t deal with day to day life challenges and solve the problems by considering them in the domain of objects, functions, inheritance etc.?
Instead, we try to access the state any situation and determine the possible behaviours (options) out of it. It’s the most natural way of solving the problems and it doesn’t need any training
As a human, we’re always in a particular state and our behaviour is determined by the state we are?in.
We can never be in the happy and sad at the same time, there are two distinct states of humans and you can expect a different set of behaviours from individuals based on the state they are in. Many things which are okay in Happy State (like humour) are not okay in sad state.
A human can do a lot of things, but is not in a position to do everything all the time.?
In a nutshell, the permissible action one can perform depends upon the state of the humans.
The State of a Software?Program
Just like humans,
A software can do a lot of things, but can’t do everything at all?times.
A software needs to be in a particular state (e.g. user input state) to be able to accept user input and will be not able to do so in the execution or error state.
The idea of having acceptable and unacceptable functionalities of a software in a particular state closely emulates the human behaviour and is intuitive.
Hence, instead of focussing on things like objects and relationships, we need to focus on states and what all a software can do in a particular state.
This allows our software to be written as if we’re solving the problems intuitively in real time and this will help us in writing better solutions.
The meaning of a?state?
Let me make an statement about my interaction with an individual
“I don’t know what state he was in, he just didn’t listen to me”
The conclusion we can derive from this statement is that the person didn’t listen to me because (s)he was in a totally different state (or mood), and this generally doesn’t happen when the person is in the normal state.
A state is identified by a set of acceptable functionalities and behaviours at any given point of time (i.e. in a given?state).
e.g.?—?An airplane can take-off only and only if it is on the runway.?
e.g.?—?An airplane can land only and only if it’s in the air.
领英推荐
Transitioning from one state to?another
Just like a human being, a software can’t be in one state all the time. Some action(s) will trigger the transition from one state to another state.?
As a simple example, let’s assume that a person is in Happy State, and (s)he lost money. The person will transition to Sad State. Similarly, if a person is in Sad State and (s)he receives money, the person will transition back to Happy State.
The action or activity which triggers the transition from one state to another is called an “EVENT”
In the example above, Money is Lost and Money is gained is an Event. An event may of may not make a transition to another state. For example, receiving more money in the Happy State, will not change the state and the state will remain Happy.
The idea of a State?Machine
Its indeed intuitive to create multiple states, events and transitions to create the desired software solutions, but at the core of it we need something else which manages these entities and it’s called as “State Machine”
A state machine is a software program / framework which enables the creation and storage of multiple states, events and transitions. It also keeps an active state and decides what all events can be received in a state and what all state transitions are possible within a particular state.
The state machine is a critical part of the solution and one has to use a built in framework or create a new one. For example, in the case of happy state and sad state, here is how the state machine will work
Benefits of using State Machine Based Programming
Apart from being intuitive, there are many other benefits of using state machine based programming, some of them are
What about OOP & Functional Programming??
State machine based programming has no relation to Object Oriented Programming and/or functional programming.?
It’s possible to implement states with Object Oriented as well as by using functional or for that matter procedural programming.
State Machine based programming doesn’t contradict the constructs of OOP or functional, it just changes our thought process to focus more on states and acceptable behaviour in a state than to objects and functions.
I do hope and believe that you’ll try State Machine based Programming in your code and it will be extremely helpful to you as well as to your organisations
Thanks for Reading…!!!
Daksh
(If you like my work, Please don’t forget to clap, like, share and follow ??)
Lecturer - Networks, Web Development, Distributed Systems at Macquarie University
5 个月Whole response here: https://medium.com/@ianjoyner/some-computing-fundamentals-89f3e2ed4a53
Lecturer - Networks, Web Development, Distributed Systems at Macquarie University
5 个月From the second comment I put on Medium. Since there is a very good relationship between OO and state machines, but state machines are limited compared to OO or FP. It is explained here: https://medium.com/@ianjoyner/do-you-have-a-non-paid-for-form-7567d8c594b3