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 ...
随机推荐
- 【IDEA】Lombok--是否值得我们去使用
官网 https://projectlombok.org/ 简介 Project Lombok is a java library that automatically plugs into your ...
- 有状态 无状态 stateful stateless monolithic architecture microservice architecture 单体架构
为什么游戏公司的server不愿意微服务化? - 知乎 https://www.zhihu.com/question/359630395 我大概说了,方便测试,方便维护,方便升级,服务之间松耦合,可多 ...
- Java面向对象(三)—— 继承
标签: java 继承 抽象类 this super abstract 概述 多个类中存在相同的属性和行为的时候,将这些内容抽取到单独一个类中,那么多个类无需在定义这些属性和行为,只要继承那个类即可. ...
- 语言反射规则 - The Laws of Reflection
[译]Go反射的三个原则(官方博客) | seven的分享 https://sevenyu.top/2019/12/21/laws-of-reflection.html wilhg/The-Laws- ...
- 【LinuxShell】命令行常用快捷键
Ctrl + A :光标跳到一行命令的开头.一般来说,Home 键有相同的效果: Ctrl + E :光标跳到一行命令的结尾.一般来说,End 键有相同的效果:. Ctrl + U :删除所有在光标左 ...
- 后端API接口的错误信息返回规范
前言 最近我司要制定开发规范.在讨论接口返回的时候,后端的同事询问我们前端,错误信息的返回,前端有什么意见? 所以做了一些调研给到后端的同事做参考. 错误信息返回 在使用API时无可避免地会因为各种情 ...
- Language Guide (proto3) | proto3 语言指南(二)标量值类型
标量值类型 标量消息字段可以具有以下类型之一 -- 下表显示了.proto文件中指定的类型,以及自动生成的类中相应的类型: .proto Type 说明 C++ Type Java Type Pyth ...
- .NET使用MailKit进行邮件处理
0.介绍 MimeKit and MailKit are popular fully-featured email frameworks for .NET 框架支持版本如下 Supports .NET ...
- EasyUI框架
使用EasyUI框架时,需要导入3个包在项目js文件夹之中. 在项目之中,每次需先引入相关文件: <!--引入jquery--> <script src="../js/jq ...
- js创建map
function Map() { var struct = function(key, value) { this.key = key; this.value = value; } var put = ...