JavaScript originated from the early days of the Internet. It started as a scripting language and has now evolved into a fully fledged programming language supporting server-side execution.
Modern web applications rely heavily on JavaScript, especially single-page applications (SPA). With emerging frameworks like React, AngularJS, and Vue.js, web applications are primarily built using JavaScript.
Scaling these applications (the front end is equivalent to the back end) can be quite tricky. With a mediocre setup, you'll end up hitting limitations and getting lost in a sea of confusion. I want to share some tips to help you write clean code in an efficient way.
This article is suitable for JavaScript developers of any skill level. However, developers with at least intermediate knowledge of JavaScript will benefit the most from these tips.
1. Isolate code
The most important thing I can suggest to keep your codebase clean and readable is to separate specific logic by topic Block (usually a function). If you write a function, it should default to having only one purpose and should not perform multiple operations at once.
Also, causing side effects should be avoided, which means that in most cases, anything declared outside a function should not be changed. Receive data into a function with parameters; everything else should not be accessed. If you want to get something from the function, return a new value.
2. Modularity
Of course, you can combine multiple functions if they are used in similar ways or do similar things Grouped into a module (and/or class, if you prefer). For example, if you have many different calculations to do, you can split them into independent steps (functions) that can be chained.
However, these functions can be declared in a file (module). Here is a JavaScript example:
function add(a, b) { return a + b } function subtract(a, b) { return a - b } module.exports = { add, subtract } const { add, subtract } = require('./calculations') console.log(subtract(5, add(3, 2))
If you are writing front-end JavaScript, be sure to use default exports for your most important projects and named exports for less important projects.
3. Prefer multiple parameters to a single object parameter
When declaring a function, multiple parameters should always be preferred instead of Not a parameter that requires an object:
// GOOD function displayUser(firstName, lastName, age) { console.log(`This is ${firstName} ${lastName}. She is ${age} years old.`) } // BAD function displayUser(user) { console.log(`This is ${user.firstName} ${user.lastName}. She is ${user.age} years old.`) }
The reason behind this is that when you look at the first line of the function declaration, you know exactly what needs to be passed to the function.
Although the size of the function should be limited (do only one job), the size of the function can become larger. It will take more time to find the variables that need to be passed in the function body (nested in the object). Sometimes it seems easier to use the whole object and pass it to the function, but to scale the application, this setup will definitely help.
At some point, it doesn't make sense to declare specific parameters. For me, it's more than four or five function parameters. If your function gets that big, you should use object parameters.
The main reason here is that the parameters need to be passed in a specific order. If there are optional parameters, you need to pass undefined
or null
. Using object parameters you can simply pass the entire object where order and undefined values don't matter.
4. Destructuring
Destructuring is a good tool introduced in ES6. It allows you to get a specific field from an object and assign it to a variable immediately. You can use it with any type of object or module.
// EXAMPLE FOR MODULES const { add, subtract } = require('./calculations')
It makes sense to import only the functions that need to be used in the file, rather than the entire module and then access specific functions from it. Similarly, Destructuring can also be used when you are sure that you really need an object as a function argument. This will still give you an overview of what you need inside the function:
function logCountry({name, code, language, currency, population, continent}) { let msg = `The official language of ${name} ` if(code) msg += `(${code}) ` msg += `is ${language}. ${population} inhabitants pay in ${currency}.` if(contintent) msg += ` The country is located in ${continent}` } logCountry({ name: 'Germany', code: 'DE', language 'german', currency: 'Euro', population: '82 Million', }) logCountry({ name: 'China', language 'mandarin', currency: 'Renminbi', population: '1.4 Billion', continent: 'Asia', })
5, using default values
destructured default values or even basic Function parameters are very useful. First, they give you an example of the values you can pass to the function. Second, you indicate which values are required and which are not. Using the previous example, the complete setup of the function might look like this:
function logCountry({ name = 'United States', code, language = 'English', currency = 'USD', population = '327 Million', continent, }) { let msg = `The official language of ${name} ` if(code) msg += `(${code}) ` msg += `is ${language}. ${population} inhabitants pay in ${currency}.` if(contintent) msg += ` The country is located in ${continent}` } logCountry({ name: 'Germany', code: 'DE', language 'german', currency: 'Euro', population: '82 Million', }) logCountry({ name: 'China', language 'mandarin', currency: 'Renminbi', population: '1.4 Billion', continent: 'Asia', })
Obviously, sometimes you may not want to use a default value and instead throw an error when no value is passed. However, this is often a convenient trick.
6. Data Scarcity
The previous tips let us come to a conclusion: do not pass unnecessary data. Here, again, this may mean more work when setting up functions, but it will definitely give you a more readable code base in the long run. Knowing exactly which values were used at a particular location is extremely valuable.
7. Line and indentation limits
I have seen large files—very large files. In fact, there are over 3,000 lines of code. It is difficult to find logical blocks in these files.
因此,您应该将文件大小限制为一定数量的行。我倾向于将文件保存在100行代码以下有时,很难分解文件,它们将增长到200-300行,在极少数情况下,最多可达400行。
超过此阈值,文件将变得混乱且难以维护。随意创建新的模块和文件夹。您的项目应该看起来像一个森林,由树(模块部分)和分支(模块和模块文件的组)组成。
相比之下,您的实际文件应该看起来像shire,到处都有一些小山(小凹痕),但所有文件都相对平坦。尽量使缩进水平保持在4以下。
8、使用更漂亮
在团队中工作需要清晰的样式指南和格式。ESLint提供了一个巨大的规则集,您可以根据需要进行自定义还有eslint--fix,它纠正了一些错误,但不是全部。
相反,我建议使用Prettier来格式化代码。这样,开发人员就不必担心代码格式化,而只需编写高质量的代码。外观将是一致的,格式自动。
9、使用有意义的变量名
理想情况下,应该根据变量的内容为其命名。下面是一些指导原则,可以帮助您声明有意义的变量名。
功能
函数通常执行某种操作。为了解释这一点,人类使用动词——例如转换或显示。最好在函数名的开头使用动词,例如convertCurrency
或displayUserName
。
数组
它们通常包含一个项目列表;因此,在变量名后面加上s
。例如:
const students = ['Eddie', 'Julia', 'Nathan', 'Theresa']
布尔值
简单地从“是”或“必须”接近自然语言开始。你可以这样问,“那个人是老师吗?”→“是”或“不是”。“类似:
const isTeacher = true // OR false
数组函数
forEach
、map
、reduce
、filter
等都是很好的原生JavaScript函数,可以处理数组并执行一些操作。我看到很多人只是将el
或element
作为参数传递给回调函数。虽然这既简单又快捷,但是您也应该根据它们的值来命名它们。例如:
const cities = ['Berlin', 'San Francisco', 'Tel Aviv', 'Seoul'] cities.forEach(function(city) { ... })
id
通常,您必须跟踪特定数据集和对象的id
。当id
被嵌套时,简单地将它保留为id
。在这里,我喜欢在将对象返回到前端之前将MongoDB _id
映射为简单的id
。当从对象中提取id
时,在前面加上对象的类型。例如:
const studentId = student.id // OR const { id: studentId } = student // destructuring with renaming
该规则的一个例外是模型中的MongoDB引用。在这里,只需在引用模型之后命名该字段。这将在填充引用文档时保持清晰:
const StudentSchema = new Schema({ teacher: { type: Schema.Types.ObjectId, ref: 'Teacher', required: true, }, name: String, ... })
10、尽可能使用异步/等待
在可读性方面,回调是最糟糕的,尤其是在嵌套时承诺是一个很好的改进,但在我看来,async/await
具有最好的可读性即使对于初学者,或者来自其他语言的人,这也会有很大帮助。但是,要确保你理解它背后的概念,不要漫不经心地到处使用它。
11、模块导入顺序
正如我们在技巧1和2中看到的,保持逻辑在正确的位置是可维护性的关键同样,如何导入不同的模块可以减少文件中的混乱。导入不同模块时,我遵循一个简单的结构:
// 3rd party packages import React from 'react' import styled from 'styled-components' // Stores import Store from '~/Store' // reusable components import Button from '~/components/Button' // utility functions import { add, subtract } from '~/utils/calculate' // submodules import Intro from './Intro' import Selector from './Selector'
我在这里使用react组件作为示例,因为有更多类型的导入。你应该能够适应你的特定用例。
12、摆脱控制台
console.log
是调试 - 的一种很好的方法,非常简单、快速,而且可以完成任务显然,有更复杂的工具,但我认为每个开发人员仍然在使用它。如果你忘了清理日志,你的主机最终会变得一团糟。然后有一些日志实际上要保存在代码库中;例如,警告和错误。
为了解决这个问题,出于调试的原因,您仍然可以使用console.log
,但是对于持久的日志,可以使用loglevel
或winston
这样的库。另外,您可以使用ESLint警告控制台语句。这样你就可以轻松地在全球范围内寻找控制台…并删除这些语句。
遵循这些准则确实帮助我保持代码库的干净和可伸缩性。有什么特别有用的建议吗在评论中让我们知道你将在你的编码工作流程中包括什么,并且请分享你使用的任何其他技巧来帮助代码结构!
原文地址:https://blog.logrocket.com/12-tips-for-writing-clean-and-scalable-javascript-3ffe30abfe20/
In order to ensure readability, this article uses free translation rather than literal translation.
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of 12 Tips for Writing Clean and Scalable JavaScript Code. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
