Author: Milos Protic
Translator: Front-end wisdom
Source: blog. Risingstack
The more you know, the more you don’t know
Like it and see. Make it a habit
GitHub: github.com/qq449245884… Has included more categories of previous articles, as well as a lot of my documentation and tutorial material. Welcome Star and Perfect, you can refer to the examination points for review in the interview, I hope we can have something together.
In order to ensure readability, this paper adopts free translation rather than literal translation.
Introduction to the
If we focus on the structure and readability of the code, rather than whether it works, then we can write code at a certain level. Professional developers will write code for their future selves and “others,” not just code that can handle current work.
From there, readable code can be defined as code that is self-explanatory, easy to understand, and easy to change or extend.
The following list of some good writing methods, only for reference, of course, if you have a better way, welcome to leave a message.
The following method only provides an Angle, does not mean that it is necessary to do so, please do not carry
1. Strong type checking
with= = =
Instead of= =
2. The variable
Name variables in such a way that you know what they mean, so that when you see them again, you can get a sense of what they mean
Do not add extra words to variable names that are not needed
Do not abbreviate variable context
Don’t add unnecessary context.
3. The function
Use long, descriptive names. Given that the function represents some behavior, the function name should be a verb or phrase that describes the intent behind it and the intent of the parameters. The names of the functions should indicate what they do.
Avoid using a large number of arguments. Ideally, functions should specify two or fewer arguments. The fewer arguments, the easier it is to test the function, and the more arguments you can use objects.
Use the default parameters instead||
operation
A function should only do one thing; do not perform multiple operations in a function
useObject.assign
Sets the default value of the object
Do not use flags as arguments, because they tell the function to do more than it should
Do not pollute global variables. If you need to extend an existing object, useES6
Classes and inheritance, rather than creating functions on a prototype chain of native objects
Conditions of 4.
Avoid negative conditions
Using conditional shorthand, use this method only for booleans, and if you are sure that the value will not beundefined
或null
, this method is used
Avoid conditionals whenever possible and use polymorphism and inheritance instead
Class 5.
class
Is a new syntax sugar in JS that works just like the old prototype but is much easier to understand than the prototype
Using links, many libraries (e.gjQuery
andLodash
) all use this pattern. In a class, you only need to return at the end of each functionthis
You can link more methods of that class to it.
conclusion
This is just a small part of improving the code. In general life, the principles mentioned here are the principles that people don’t usually follow. They try to do it, but for one reason or another, they don’t stick to it. The code may be neat at the start of the project, but when it comes to meeting deadlines, these principles are often ignored and relegated to the “TODO” or “REFACTOR” section. At this point, your client would rather you met a deadline than write clean code.
The possible bugs in editing can not be known in real time. In order to solve these bugs after the event, I spent a lot of time on log debugging. By the way, I recommend a good bug monitoring tool for youFundebug.
Original text: blog.risingstack.com/javascript-…
communication
This article is updated every week, you can search wechat “big move the world” for the first time to read and urge more (one or two earlier than the blog hey), this article GitHub github.com/qq449245884… It has been included and sorted out a lot of my documents. Welcome Star and perfect. You can refer to the examination points for review in the interview.