Maintainable JavaScript(编写可维护的JavaScript) PART I Style Guidelines

“Programs are meant to be read by humans and only incidentally( 顺便;偶然地;附带地) for computers to execute.” —Donald Knuth

When a team is brought together(被放在一起) for the first time, everyone brings with them their own ideas about how code should be written. After all, each team member comes from a different background. Some may come from one-man shops where they could do whatever they wanted; others may have been on different teams that had particular ways of doing things that they liked (or hated). Everyone has an opinion about how code should be written, and it usually falls in line with(fall in line with 同意) how that individual would personally write it. Establishing style guidelines should always come as early in the process as possible.

The terms “style guidelines” and “code conventions” are often used interchangeably. Style guidelines are a type of code convention aimed at the layout of code within a file. Code conventions can also include programming practices, file and directory layout, and commenting. This book is actually a collection and discussion of code conventions for JavaScript.

Why Style Guidelines?

Figuring out style guidelines is a process that typically takes longer than it should.Everyone has an opinion and, when you’re going to be spending eight hours a day writing code, all  programmers want to do so in a way that is comfortable to them. It takes some compromise within the team and a strong leader to move the conversation forward. Once established, style guidelines allow the team to work at a much higher level, because all code looks the same.

Having all code look the same is incredibly important on a team, because it allows:

• Any developer to work on any file regardless of(不管) who wrote it. There’s no need to spend time reformatting or deciphering the logic of the file, because it looks the same as everything else. If you’ve ever opened a file and immediately fixed all the indentation before starting your work, you can understand the time savings consistency provides when working on a large project.

• Errors become more obvious. If all code looks the same, and you come across some code that doesn’t, you’ve likely found a problem.

It’s no wonder that large companies around the world have published style guidelines either internally or publicly.

Style guidelines are a personal thing and must be developed within a team to be effective. This section of the book lists recommended focus areas for the development of your JavaScript code conventions. In some cases, it’s impossible to tell you that one guideline is better than another, because some are just a matter of preference. Rather than trying to force my preferences upon you, this chapter highlights important aspects that should be covered in your style guidelines. My personal code style guidelines for JavaScript are included in Appendix A.

时间: 2024-10-20 17:58:54

Maintainable JavaScript(编写可维护的JavaScript) PART I Style Guidelines的相关文章

编写可维护的JavaScript之简易模版

/* * 正则替换%s * @para arg1(text) 需要替换的模版 * @para arg2 替换第一处%s * @para arg3 替换第二处%s * 返回替换后的字符串 */ var sprintf = function (text) { var i = 1, args = arguments, len = args.length; return text.replace(/%s/g, function () { return (i < len) ? args[i++] : &quo

《编写可维护的javascript》读书笔记(上)

最近在读<编写可维护的javascript>这本书,为了加深记忆,简单做个笔记,同时也让没有读过的同学有一个大概的了解. 一.编程风格 程序是写给人读的,所以一个团队的编程风格要保持一致. 1.缩进:一种是利用制表符缩进,一种是使用空格符缩进,各有利弊,任选一种,保持一致即可.个人比较喜欢制表符缩进. 2.语句结尾需要加上分号,避免没必要的BUG. 3.命名:首先要语义化,使用驼峰式命名法,小驼峰即首字母小写,之后每个单词首字母大写:大驼峰即首字母大写,之后同小驼峰:变量名前缀应该是名词(my

编写可维护的JavaScript之事件处理

规则1:隔离应用逻辑 这会让你的代码容易调试 规则2:不要分发事件对象 event对象包含了太多信息 // a good example var handlePopup = { // 事件句柄,处理所有和event对象有关的东西 handleClick: function (e) { // 假设事件支持DOM Level2 e.preventDefault(); e.stopPropagation(); // 传入应用逻辑 this.showPopup(e.clientX, e.clientY)

《编写可维护的javascript》读书笔记(中)——编程实践

上篇读书笔记系列之:<编写可维护的javascript>读书笔记(上) 上篇说的是编程风格,记录的都是最重要的点,不讲废话,写的比较简洁,而本篇将加入一些实例,因为那样比较容易说明问题. 二.编程实践 1.UI松耦合 第一.将css从javascript中抽离(要改变dom样式数据,应该去操作dom的class名而非dom的style属性,后续要修改此样式只需到对应的css文件中修改而不用修改js文件): 第二.将javascript从HTML中抽离,比如下面的写法是不好的 <!-- 不

读《编写可维护的JavaScript》第六章总结

第六章 避免使用全局变量 JavaScript执行环境在很多方面都有其独特之处,全局变量就是其中之一.“全局变量”是一个神秘的对象,它表示了脚本的最外层上下文. 在浏览器中,windows对象往往重载并等同于全局对象,因此任何在全局作用域声明的变量和函数都是windows对象的属性. 6.1 全局变量带来的问题 这个就不用照着书详谈了,当我们进入团队合作编写代码时,若大家自定义的变量都是直接挂载在windows对象上(也就是全局变量),很容易发生命名冲突.像这样: function sayCol

编写可维护的javascript

编程风格 ????基本格式化 ????????格式检查工具:JsLine,JsHint, ????????样式风格:Jquery 核心风格指南,Dauglas Crockford的javascript 代码规范,Google 的javascript风格指南 ????????如:使用Sublime Text3 使用 上述的格式检查工具 在sublime Text 3中搜索插件SublimeLinter[https://packagecontrol.io/] 安装sublimeLinter插件,同时

编写可维护的javascript代码---开篇

文章开篇主要推荐了2款检测编程风格的工具: JSLint和JSHint: jsLint是由Douglas Crockford创建的.这是一个通用的javascript代码质量检测工具,最开始JSLint只是一个简单的查找不符合javascript模式的,错误的小工具,经过数年的进化,JSLint已经成为一个有用的工具,不仅能可以找出代码潜在的错误,而且能针对你的代码给出编码风格上的警告. JSHint是JSLint的一个分支项目,在github上可以搜到. JSHint的目标是提供更加个性化的j

编写可维护的JavaScript-第5章-UI层的松耦合

2.将JavaScript从CSS中抽离 别用CSS表达式(现在也没有了) 3.将CSS从JavaScript中抽离 不要直接使用JS给HTML设置样式,使用className作为CSS和JavaScript的桥梁 4.将JavaScript从HTML中抽离 最好将所有JS全部外置出来 5.将HTML从JavaScript中抽离 从服务器中加载HTML(例如使用jQuery的.load()) 将HTML模版放在HTML文件里面,用注释/Script包起来 使用Handlebar的JavaScri

编写可维护的Javascript纪要

第一部分: 编程风格 在大型项目开发中,因为项目可读性规范性的需要(就像<编写可维护性的Javascript>一书作者Nicholas Zakas大神所说,他们团队所有成员写出的代码就像是经同一个人之手写出的一样),风格约定要大于个人喜好这一点毋庸置疑,不过什么样才是好的编程风格约定?下面推荐一些实践中沉淀下来的代码规范和最佳实践: 缩进 缩进问题和编辑器问题一样是一个因为个人喜好和其他不管值得不值得争执的理由而存在争议的问题,目前存在两个流派,空格流和tab流.个人比较习惯于tab(4个空格