The thing is…
Yesterday I wrote a front end why to use JS brush algorithm comprehension article, and then pidian pidian to algorithm group for attention, for the point like, and then the big guy asked me the above question, this actually also really considered, so I came.
Careful partners will find that my topic is why I don’t use TS brush algorithm, rather than not recommended to use TS brush algorithm, this is because, with TS brush algorithm is good or bad I can’t decide, and in different stages may interpret different, so I can only talk about why I don’t use TS brush algorithm.
I don’t use TS for my own reasons
With the TS brush algorithm, that needs to be compiled
I had a dream that THE dream LC would let me test the second off, but that was just a dream; So I have no money to rush members, often in VSCode directly local test, Maozi can be much faster; It is said that some plugins can be implemented, but if I am lazy, I will only use native and then run it in Node. The problem is that if I write it in TS, I have to use TSC first and then execute it. emm… This is a little difficult for me.
Write the type in TS, it’s distracting
As a person who may suffer from ADHD, the cultivation of attention is still a difficult problem, and the inability to be distracted is still a pain. For example, I envy Xiao Xin next door who can write papers while watching TV with high efficiency.
The single rule is probably the only way I can stay focused, which is to do one thing with as few distractions as possible, otherwise I’ll lose focus and get distracted. So when I tried to write the algorithm with TS, I found that my judgment on the return value in the writing of some input parameters would distract me from solving the problem with the algorithm.
Before, I could just read the problem, understand it, look at the sample, and then analyze the boundary; Now it’s time to look at the problem, think about what the input is, and think about the format of the return value — that’s fine for simple problems, but for problems like LRU cache, it’s a little tiring to write TS.
What seems to be the benefit of the TS writing algorithm?
When I am going to rewrite the algorithm with TS, I am good fool oneself: a time, can not only learn the algorithm, can learn TS, but also use TS to do the limit can reduce the error of the algorithm, a time to meet two wishes, it is not sweet?
Then I realized THAT I had misunderstood TS;
- The first thing I need to do is the algorithm, so the first thing I need to do is to get through the problem, otherwise I will forget everything, and writing with TS first increases my workload to complete the algorithm
- Secondly, one of the main functions of TS is to restrict variable attributes to prevent different types of apis from being used incorrectly and give corresponding prompts. The most typical one is undefined equivalent error. However, in the algorithm, at least in LC algorithm, these conditions are generally given. It’s clear and it’s not usually confused in a problem, so this feature is rarely used in the algorithm
- Again, TS can use the defined interface to prompt when a variable is used. For example, if it is an array, it will prompt you for the method mounted on the array. But when you’re writing algorithms, you either don’t use the API directly (for example, sort), or you just want to get familiar with the API (you don’t want it to give you a TS hint when you’re writing on a whiteboard). Therefore, the type attribute hint function of TS does not feel useful.
- Finally, if we want to improve TS, we may define only a few interfaces in the algorithm, and not many advanced uses such as generics are used. Therefore, the upgrade effect of TS cannot be achieved, but it may be superfluous
Is using TS a bonus in an interview?
As a cook who can only brag, I don’t know whether the interviewer will score points, but I think it is a waste of time to write with TS.
In the last article, I said that writing an algorithm in JS can use some JS features or apis that interviewers want to know, but are not good to ask questions alone, such as Reduce array, weakMap, etc. Etc., according to the truth, TS can also achieve extra points effect.
But don’t forget that JS is an implementation language, and TS is a superset of JS. If you want to examine TS, you will not be given extra points for defining a few simple interfaces or types. However, writing with TS will obviously increase the time cost, and everyone wants to go back to rest early
To sum up, I think using TS to write the algorithm is inefficient, the cost of developing users is not good (also have to compile or add plug-ins), and the marginal benefit is not high, so I give up temporarily.
With all the bad stuff, can you say something good
Of course, if you’re familiar with TS and don’t use it for TS purposes, IT’s still useful. For example, I often use the Array method for strings, and TS will remind you.
Secondly, for me to write the complex type problem LRU, I/O can be clearly understood with TS, which can also achieve the effect of showing skills and reviewing TS.
The last
TS is a superset of JS, which helps us to limit some runtime errors. When doing algorithms, we are generally required to consider all the margins to prevent errors. This time is a mental exercise, of course, we can use the help of tools, but this will reduce the effect of training.
But for some skilled questions, use TS to write may be better for the review effect, may be used before the implicit conversion of JS to fool, now equivalent to adding sandbag running, can further strengthen the cognition of the topic, is a kind of intensive training.
So, for the front end of the brush at the beginning, or is not recommended to use TS brush, it is likely that both ends do not please, both ends did not strengthen; Or undivided attention with JS brush algorithm.
The above point is still from an article XXX by a great god. It is not recommended to learn TS when learning React. I forgot the name, but I could not find it when writing. The main idea is not to use TS version CRA when learning React at the beginning, thinking that you can learn TS and React together, because both of them are relatively complex, you need to study with all your heart, and if they are mixed together, it will disturb your mind, but make the code more complex and obscure, and increase our marginal cost of learning. And may give up learning.
Finally, no matter what brush algorithm is used, in fact, the important brush, the important is continuous; I adhere to the 144 days, the principle is the principle of minimum consumption, as far as possible with their own comfortable consumption to cultivate their own habits, let it become a daily necessity and will not cost too much energy, so that into the investment can be increased, return will not have nothing; So the stock needs to be careful, coin copying ruined life, if to win inflation, the fund price is higher; May the roof never need to be locked again. Amen…