The current browser does not support music or voice playback. Please play music in wechat or another browser Ambiguous Wong Fei – Di-Dar
Everyone may have different ways to improve their skills
It’s better to look at other people’s good code than to write more code yourself.
As a technical class of public number
Next, I will pay more attention to some relatively new content myself
It also focuses on how to write good code and master the basics, right
Let’s start with a scenario
For example, the back end returns a content
We then need to display the content based on the different values of the returned fields.
Let’s start with probably the easiest piece of code to write
Because there is only a single code change to display the content
We might also think of the following ways
There’s really not that much difference between the two forms, right
I’m usually in this situation
It reads like this
If the code returned does not have the corresponding array index
When you declare a variable, change it to an object
If it’s more complicated
Depending on the state, and then depending on the method, you can do this
I wrote a very complicated piece of logic earlier,
It’s incredibly complicated.
A total of more than ten states, each state is based on the combination of different fields.
All of this code was done by if else
Not that it’s hard to maintain if you don’t touch it anyway
But it always looks low.
Refactoring is done in that way.
You can feel it a little bit
My old backend leader told me about it
Writing code is like building pipes
A pipeline is always the easiest to maintain
So we’re going to put the unwanted stuff on top
Such as
These two ways
On clean and readable code
We should all choose the second form
The initial code we wrote was probably ugly
If time permits, you can take the time to change the logic of the previous complex code.
Maybe we’ll get more satisfaction than we thought we’d get.
Ps: Early to bed and early to rise, often do exercise, make more friends with the opposite sex ~