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 ...
随机推荐
- Linux系统使用lvm扩展根分区
Linux系统使用lvm扩展根分区 背景:买的云主机虚拟机封装镜像是40G的系统盘,后期适用不规范或者其他需求需要扩展系统盘,而非挂载在一个盘至新建目录. 1.原本目录磁盘等信息: 2.使用vgdis ...
- jmeter三种阶梯式加压
一.前言 在做性能测试的时候,在某些场景下需要逐渐加压,不总是停下来再修改线程再加压,且可以对比加压,找出服务的性能拐点. 二.三种逐渐加压方式 备注:普通的压测方式,并发的Samples是可预知的: ...
- 【转】使用ssh-keygen和ssh-copy-id三步实现SSH无密码登录
[原]http://blog.chinaunix.net/uid-26284395-id-2949145.html ssh-keygen 产生公钥与私钥对. ssh-copy-id 将本机的公钥复制 ...
- Dubbo 最基本的几个需求
需求 http://dubbo.apache.org/zh-cn/docs/user/preface/requirements.html 在大规模服务化之前,应用可能只是通过 RMI 或 Hessia ...
- sql 括号
<select id="chlTransQueryByChlType" parameterType="map" resultType="java ...
- Java并发包源码学习系列:阻塞队列实现之DelayQueue源码解析
目录 DelayQueue概述 类图及重要字段 Delayed接口 Delayed元素案例 构造器 put take first = null 有什么用 总结 参考阅读 系列传送门: Java并发包源 ...
- SpringMVC听课笔记(十:处理JSON: 使用HttpMessageConverter)
1. 处理JSON 2. 原理 流程图 3. 看个应用吧 -- 上传 ①jsp ②handler -- 下载 ① jsp ② handler
- hasOwnProperty和 ... in ...的区别
hasOwnProperty() 方法会返回一个布尔值,指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键).https://developer.mozilla.org/zh-CN/docs ...
- GLIBC升级
GLIBC升级 1.安装 1.1 说明 目前大部分架构都已经是GLIBC2.14了,难免会有一些老的机器会是GLIBC2.12,所以下面是我升级GLIBC的过程及步骤. GLIBC是系统核心服务,升级 ...
- P3980 [NOI2008]志愿者招募 (费用流)
题意:最多1000天 每天需要至少ai个工人施工 有10000种工人可以雇佣 每种工人可以工作si到ti天 雇佣一个的花费是ci 问怎样安排使得施工花费最少 思考:最直白的建模方式 就是每种工人可以和 ...