Jianghu Development Platform Code Specifications
120021. Why Standardize Code
Consequences of Not Standardizing Code
- Poor readability: Code without standards is difficult to read and understand, increasing the difficulty of maintenance and modification. Especially for team collaboration, a unified standard can enhance code readability.
- Maintenance challenges: The absence of standards can lead to chaotic code structure, inconsistent naming, and non-uniform styles, making code maintenance complex and difficult.
- Low code quality: Not adhering to standards may result in redundant, repetitive code that is hard to extend and test. The code may contain logical errors, performance issues, and security vulnerabilities.
- Poor maintainability: The lack of standards affects the maintainability of the code. As the codebase grows, the absence of standards will lead to more problems, causing maintenance costs to soar.
- Difficulties in team collaboration: The lack of a unified standard increases communication costs among team members, and differing coding styles may lead to conflicts.
- Poor portability: The absence of standards may make it difficult to port and reuse code across different environments.
- Security risks: Code standards can reduce certain security risks; for example, good naming conventions can help avoid issues caused by typographical errors.
Advantages of Code Standards
- Improved code readability: Code standards can unify the format, naming conventions, comments, etc., making the code easier to read and understand, thus enhancing code readability.
- Enhanced code quality: Code standards can enforce best practices, such as using meaningful names in the code, avoiding duplicate code, and optimizing code structure, thereby improving code quality.
- Facilitated collaborative development: Code standards can ensure consistency in coding styles among different developers, reducing communication and learning costs, thus improving development efficiency and code quality.
- Reduced maintenance costs: Code standards can make code easier to maintain; for instance, standard indentation and comments can make the code easier to format and understand, thereby lowering maintenance costs.
The above code standards are a set of conventions and rules established to make code more readable, maintainable, and conducive to collaborative development. Overall, not adhering to code standards can affect the quality, readability, maintainability, and scalability of the code, increasing development and maintenance costs, and lowering the overall quality and stability of software projects. Therefore, establishing and following appropriate code standards is crucial for software development projects.
2. Async Function
Using async/await to solve the problem of nested asynchronous callbacks. The async function is syntactic sugar provided at the language level. In an async function, we can use the await keyword to wait for a Promise to be resolved (or rejected, which will throw an exception). The current LTS version of Node.js (8.x) natively supports this.
const fn = async function () {
const user = await getUser();
const posts = await fetchPosts(user.id);
return { user, posts };
};
fn()
.then((res) => console.log(res))
.catch((err) => console.error(err.stack));3. HTML
- Page Code Specification - doUiAction
Based on "Unified Transfer Chain Operations," its core idea is to encapsulate different operations in a reusable manner, thereby improving code readability and maintainability. Methods can be executed in a chain using doUiAction("uiActionId", uiActionParamObj), which is easy to maintain and modify.
// Definition and implementation of doUiAction
methods: {
// Define doUiAction
async doUiAction(uiActionId, uiActionData) {
switch (uiActionId) {
case 'startCreateItem':
await this.prepareCreateFormData();
await this.openCreateDrawer();
break;
default:
console.error("[doUiAction] uiActionId not found", {uiActionId});
break;
}
},
//