Skip to main content

Writing Good JavaScript

Free2015-08-30#JS#Mind#Front-End#JavaScript代码风格#Maintainable-JavaScript#可维护的JavaScript#JavaScript编程常识#JavaScript自动化

Good code is self-explanatory, code speaks for itself. This article details JavaScript code style, programming common sense, and automation tools

Preface

Code is a programmer's business card. There's ordinary code, and there's clever, meticulous, eye-catching code. Some code looks comfortable to read, some code even the author themselves can't bear to look at...

I. JavaScript Code Style

Code style is the clothing of code. Clean and tidy naturally looks comfortable. Every space, every line break, every comment should be for better readability.

Code is written for people to read, with the side benefit of being able to run on machines

Code style directly affects readability and maintainability. The simpler and more readable the code, the easier it is to maintain.

Keep It Simple Stupid

Obscure and brief code is completely unnecessary. For example, nested ternary operators save a few bytes at the cost of every future reader spending 1 minute to understand it.

Code style includes:

  • Basic formatting (indentation, line width, naming conventions, etc.)

  • Comment formatting

  • Statements and expressions

  • Variables, functions, operators

For detailed content, please see [AnYuQingYang: JS Code Style Guide](/articles/js 代码风格指南/), with examples included.

II. JavaScript Programming Common Sense

To write more maintainable code, knowing code style is not enough. You also need to keep these programming common sense in mind:

  • Ensure loose coupling of UI layer

  • Control global variables

  • Add event handling logic

  • Detect data types

  • Separate configuration data

  • Throw custom errors

  • Respect object ownership

  • Perform browser detection

For detailed content, please see [AnYuQingYang: JS Programming Common Sense](/articles/js 编程常识/)

III. JavaScript Automation

JS automation is a microcosm of frontend engineering. Using automation tools can also improve maintainability. Some error-prone tedious work can be confidently handed over to tools.

When doing JavaScript automation, you should clarify:

  • Pros and cons of automation

  • File directory structure

  • Automation steps

For detailed content, please see [AnYuQingYang: JS Automation](/articles/js 自动化/)

Book Review

[caption id="attachment_742" align="alignnone" width="318"]Maintainable JavaScript Maintainable_JavaScript[/caption]

"Maintainable JavaScript", a thin 200 pages, a very good book

Comments

No comments yet. Be the first to share your thoughts.

Leave a comment