Google coding Style Guide : Google 编码风格/代码风格 手册/指南
1
1
1
https://github.com/google/styleguide
Google 编码风格/代码风格 手册/指南
Style guides for Google-originated open-source projects.
https://google.github.io/styleguide/htmlcssguide.xml
Google HTML/CSS Style Guide
General Style Rules
Protocol
link▽
Omit the protocol from embedded resources.(省略嵌入资源的协议。)Omit the protocol portion (
http:,https:) from URLs pointing to images and other media files, style sheets, and scripts unless the respective files are not available over both protocols.Omitting the protocol—which makes the URL relative—prevents mixed content issues and results in minor file size savings.
<!-- Not recommended -->
<script src="https://www.google.com/js/gweb/analytics/autotrack.js"></script><!-- Recommended -->
<script src="//www.google.com/js/gweb/analytics/autotrack.js"></script>/* Not recommended */
.example {
background: url(https://www.google.com/images/example);
}/* Recommended */
.example {
background: url(//www.google.com/images/example);
}https://google.github.io/styleguide/javascriptguide.xml
Google JavaScript Style Guide
Tips and Tricks
link▽
JavaScript tidbitsTrue and False Boolean Expressions
The following are all false in boolean expressions:
nullundefined''the empty string0the numberBut be careful, because these are all true:
'0'the string[]the empty array{}the empty objectThis means that instead of this:
while (x != null) {you can write this shorter code (as long as you don't expect x to be 0, or the empty string, or false):
while (x) {And if you want to check a string to see if it is null or empty, you could do this:
if (y != null && y != '') {But this is shorter and nicer:
if (y) {Caution: There are many unintuitive things about boolean expressions. Here are some of them:
Boolean('0') == true
'0' != true
0 != null
0 == []
0 == false
Boolean(null) == false
null != true
null != false
Boolean(undefined) == false
undefined != true
undefined != false
Boolean([]) == true
[] != true
[] == false
Boolean({}) == true
{} != true
{} != falseConditional (Ternary) Operator (?:)
Instead of this:
if (val) {
return foo();
} else {
return bar();
}you can write this:
return val ? foo() : bar();The ternary conditional is also useful when generating HTML:
var html = '<input type="checkbox"' +
(isChecked ? ' checked' : '') +
(isEnabled ? '' : ' disabled') +
' name="foo">';&& and ||
These binary boolean operators are short-circuited, and evaluate to the last evaluated term.
"||" has been called the 'default' operator, because instead of writing this:
/** @param {*=} opt_win */
function foo(opt_win) {
var win;
if (opt_win) {
win = opt_win;
} else {
win = window;
}
// ...
}you can write this:
/** @param {*=} opt_win */
function foo(opt_win) {
var win = opt_win || window;
// ...
}"&&" is also useful for shortening code. For instance, instead of this:
if (node) {
if (node.kids) {
if (node.kids[index]) {
foo(node.kids[index]);
}
}
}you could do this:
if (node && node.kids && node.kids[index]) {
foo(node.kids[index]);
}or this:
var kid = node && node.kids && node.kids[index];
if (kid) {
foo(kid);
}However, this is going a little too far:
node && node.kids && node.kids[index] && foo(node.kids[index]);Iterating over Node Lists
Node lists are often implemented as node iterators with a filter. This means that getting a property like length is O(n), and iterating over the list by re-checking the length will be O(n^2).
var paragraphs = document.getElementsByTagName('p');
for (var i = 0; i < paragraphs.length; i++) {
doSomething(paragraphs[i]);
}It is better to do this instead:
var paragraphs = document.getElementsByTagName('p');
for (var i = 0, paragraph; paragraph = paragraphs[i]; i++) {
doSomething(paragraph);
}This works well for all collections and arrays as long as the array does not contain things that are treated as boolean false.
In cases where you are iterating over the childNodes you can also use the firstChild and nextSibling properties.
var parentNode = document.getElementById('foo');
for (var child = parentNode.firstChild; child; child = child.nextSibling) {
doSomething(child);
}https://google.github.io/styleguide/angularjs-google-style.html
An AngularJS Style Guide for Closure Users at Google
https://google.github.io/styleguide/javaguide.html
Google Java Style Guide
https://google.github.io/styleguide/pyguide.html
Google Python Style Guide
https://google.github.io/styleguide/shell.xml
Shell Style Guide
https://google.github.io/styleguide/xmlstyle.html
Google XML Document Format Style Guide
Version 1.0
Copyright Google 2008https://google.github.io/styleguide/cppguide.html#Formatting
Google C++ Style Guide
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Google coding Style Guide : Google 编码风格/代码风格 手册/指南的更多相关文章
- Google C++ Style Guide的哲学
Google C++ Style Guide并不是一个百科全书,也不是一个C++使用指南,但它描述适用于Google及其开源项目的编码指南,并不追求全面和绝对正确,也有许多人置疑它的一些规则.但作为一 ...
- python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以认作标准
python coding style guide 的快速落地实践 机器和人各有所长,如coding style检查这种可自动化的工作理应交给机器去完成,故发此文帮助你在几分钟内实现coding st ...
- electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google JavaScript Style Guide代码规范
我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...
- 一张图总结Google C++编程规范(Google C++ Style Guide)
Google C++ Style Guide是一份不错的C++编码指南,我制作了一张比較全面的说明图,能够在短时间内高速掌握规范的重点内容.只是规范毕竟是人定的,记得活学活用.看图前别忘了阅读以下三条 ...
- [Guide]Google Python Style Guide
扉页 项目主页 Google Style Guide Google 开源项目风格指南 - 中文版 背景 Python 是Google主要的脚本语言.这本风格指南主要包含的是针对python的编程准则. ...
- [Guide]Google C++ Style Guide
0.0 扉页 项目主页 Google Style Guide Google 开源项目风格指南 -中文版 0.1 译者前言 Google 经常会发布一些开源项目, 意味着会接受来自其他代码贡献者的代码. ...
- Google C++ Style Guide在C++11普及后的变化
转 http://www.cnblogs.com/chen3feng/p/5972967.html?from=timeline&isappinstalled=0&lwfrom=user ...
- [中英对照]Linux kernel coding style | Linux内核编码风格
Linux kernel coding style | Linux内核编码风格 This is a short document describing the preferred coding sty ...
- Google Shell Style Guide
转自:http://google.github.io/styleguide/shell.xml Shell Style Guide Revision 1.26 Paul Armstrong Too m ...
随机推荐
- 前端面试准备笔记之JavaScript(01)
1.1 typeof 能判断哪些类型? typeof可以识别所有的值类型 typeof可以识别函数 //function typeof可以判断是否是引用类型(不可以再细分) //object 1.2 ...
- 前端中的script标签
script标签中的重要属性! . 浏览器解析行内脚本的方式决定了它在看到字符串时,会将其当成结束的 标签.想避免这个问题,只需要转义字符"\" ①即可: 要包含外部文件中的 Ja ...
- history附上时间戳,history命令_Linux history命令:查看和执行历史命令
起因是这样的,一台机器客户反馈连接不上,说没有任何操作.好吧,排查吧. 1.第一步先看网络是否通: 从图中可以看到一开始是一直不通的.然后就通了,问了客户有没操作重启什么的结果说没有任何操作,还让给个 ...
- oblet
oblet - The Go Programming Language https://golang.google.cn/search?q=oblet // put enqueues a poin ...
- python_3 装饰器参数之谜
装饰器参数之谜 之前已经初步了解过装饰器了,知道了装饰器可以"偷梁换柱",在不改变函数的调用方式和函数内容的时候,而把函数的功能偷偷地修改. 那么问题来了,如果被修改的函数中有参数 ...
- Python_1生成器(下)之单线并行--生产着消费者模型
1 import time 2 def consumer(name): 3 print('%s准备吃包子了!' %name) 4 while True: 5 baozi = yield 6 print ...
- nginx proxy pass redirects ignore port
nginx proxy pass redirects ignore port $host in this order of precedence: host name from the request ...
- Sapphire: Copying GC Without Stopping the World
https://people.cs.umass.edu/~moss/papers/jgrande-2001-sapphire.pdf Many concurrent garbage collectio ...
- Qedis实现
对比redis的Qedis 实现在github 和 实验楼都有资料
- 单点登录(SSO)的设计与实现
一.前言 1.SSO说明 SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.https://baike.baidu.c ...