This is the fourth day of my participation in Gwen Challenge.
I don’t know if you’ve ever run into a situation where the logic of your code is really complicated, and you need a lot of if and else, and each case has its own logic. When there are too many if else’s like this, it becomes difficult to read the code and continue iterating. If you come across code like this, how do you optimize it?
This article provides an idea for simplifying complex if else code logic using state machines.
After reading this article, you will know:
- What is a state machine
- What is a state automata
- How does typescript source take advantage of state machines to make processes clearer
- State machines in lexical analysis
- How are state machines used in business code
What is a state machine
When a large number of cases are processed, we encapsulate the processing logic for each case into a state, and the transitions between different cases become transitions of states. This form of code organization is called a state machine.
When each state knows which state it will turn to when it inputs a certain content, it will automatically transfer the state and process different states within a cycle, which is called state automata (AUTOMATION). If a state has only one subsequent state under one input, it is called deterministic finite state automata (DFA).
The flow between states can be represented by a state transition diagram.
State machines in typescript source code
Typescript Compiler uses state machines to organize the compilation process:
First TSC divides up a number of states, each of which handles a logic. Such as:
- CreateProgram parses source code into an AST
- SyntaxDiagnostics handles syntax errors
- SemanticDiagnostics handles semantic errors
- Emit generates object code
Typescript uses these state changes to allow for the flow of different processing logic, with the end state being the end of the process.
In this way, the overall process can be easily expanded and modified. For example, if you want to expand a stage, you only need to add a state; if you want to modify the processing logic of a certain state, you only need to modify the state turning of the state machine. Rather than a lot of if else jumbled up and difficult to extend and modify.
As you can see, the state machine allows the typescript compilation steps to be flexibly extended and modified.
State machines in lexical analysis
In fact, the most common use of state machines is for lexical analysis, because each token is a processing case and there are naturally many if else.
It is also possible to use if else as follows, which is wenyan’s lexical analysis logic, but the code is difficult to maintain.
A better approach is to use a state machine (DFA) for word segmentation, encapsulating the processing of each token into a state. State flow is made by judging boundary conditions. For example, a WXML Parser divides these states:
Each state handles the token recognition of a situation:
Drive the flow of processing logic through changes in state:
This is a continuous flow between states, and by the time you get to the end of the string, all the segmentation is complete.
State machines in business code
Business code can also be optimized with state machines when encountering various if and else judgments. Each situation is encapsulated into a state, the flow of the state is triggered by a certain condition, and then different state processing logic is selected in the state machine for processing.
Whether it is different processing logic for different states in a game, or different rendering for different states in a UI project, when the code logic is complex, there will inevitably be a lot of if and else, at this time, you can use the idea of state machine to optimize.
This makes it much simpler and clearer when you extend the processing logic later and modify the processing logic under different conditions.
conclusion
We first define the concept of state machine: encapsulate the processing logic of different situations through different states, and complete the flow between processing logic through the modification of states.
If each state knows what the next state is, a state machine that automatically completes the state flow in a cycle is a state automata, and when there are a finite number of states, it is a finite state automata (DFA).
Typescript Compiler works with stateful automata, encapsulating multiple states, each of which knows what the next state will be until it reaches the terminating state, which ends compilation.
In lexical analysis, finite state automata (DFA) is generally used for processing. Different tokens are processed with different states. State flow is performed through different input characters, and word segmentation is completed after processing strings.
Business code also often has different cases for different processing, these cases under certain conditions will make transition scenarios, such as start, pause, end, restart and so on. This kind of code is very good for state machine optimization, otherwise there will be a lot of if else.
In a word, when the logic can be divided into different cases and various cases will be converted to each other, the state machine can be used to optimize, which can eliminate a large number of if and else, and the readability, extensibility and maintainability of the code will be greatly improved.
Hopefully this article has given you an idea of what a state machine is, when to use it, what improvements it can bring, and how to actually use it in your code.