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.

  1. <!-- Not recommended -->
  2. <script src="https://www.google.com/js/gweb/analytics/autotrack.js"></script>
  1. <!-- Recommended -->
  2. <script src="//www.google.com/js/gweb/analytics/autotrack.js"></script>
  1. /* Not recommended */
  2. .example {
  3. background: url(https://www.google.com/images/example);
  4. }
  1. /* Recommended */
  2. .example {
  3. background: url(//www.google.com/images/example);
  4. }

https://google.github.io/styleguide/javascriptguide.xml

Google JavaScript Style Guide

Tips and Tricks

link

JavaScript tidbits

True and False Boolean Expressions

The following are all false in boolean expressions:

  • null
  • undefined
  • '' the empty string
  • 0 the number

But be careful, because these are all true:

  • '0' the string
  • [] the empty array
  • {} the empty object

This means that instead of this:

  1. 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):

  1. while (x) {

And if you want to check a string to see if it is null or empty, you could do this:

  1. if (y != null && y != '') {

But this is shorter and nicer:

  1. 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

    {} != false

Conditional (Ternary) Operator (?:)

Instead of this:

  1. if (val) {
  2. return foo();
  3. } else {
  4. return bar();
  5. }

you can write this:

  1. return val ? foo() : bar();

The ternary conditional is also useful when generating HTML:

  1. var html = '<input type="checkbox"' +
  2. (isChecked ? ' checked' : '') +
  3. (isEnabled ? '' : ' disabled') +
  4. ' 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:

  1. /** @param {*=} opt_win */
  2. function foo(opt_win) {
  3. var win;
  4. if (opt_win) {
  5. win = opt_win;
  6. } else {
  7. win = window;
  8. }
  9. // ...
  10. }

you can write this:

  1. /** @param {*=} opt_win */
  2. function foo(opt_win) {
  3. var win = opt_win || window;
  4. // ...
  5. }

"&&" is also useful for shortening code. For instance, instead of this:

  1. if (node) {
  2. if (node.kids) {
  3. if (node.kids[index]) {
  4. foo(node.kids[index]);
  5. }
  6. }
  7. }

you could do this:

  1. if (node && node.kids && node.kids[index]) {
  2. foo(node.kids[index]);
  3. }

or this:

  1. var kid = node && node.kids && node.kids[index];
  2. if (kid) {
  3. foo(kid);
  4. }

However, this is going a little too far:

  1. 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).

  1. var paragraphs = document.getElementsByTagName('p');
  2. for (var i = 0; i < paragraphs.length; i++) {
  3. doSomething(paragraphs[i]);
  4. }

It is better to do this instead:

  1. var paragraphs = document.getElementsByTagName('p');
  2. for (var i = 0, paragraph; paragraph = paragraphs[i]; i++) {
  3. doSomething(paragraph);
  4. }

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.

  1. var parentNode = document.getElementById('foo');
  2. for (var child = parentNode.firstChild; child; child = child.nextSibling) {
  3. doSomething(child);
  4. }

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 2008

https://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 编码风格/代码风格 手册/指南的更多相关文章

  1. Google C++ Style Guide的哲学

    Google C++ Style Guide并不是一个百科全书,也不是一个C++使用指南,但它描述适用于Google及其开源项目的编码指南,并不追求全面和绝对正确,也有许多人置疑它的一些规则.但作为一 ...

  2. python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以认作标准

    python coding style guide 的快速落地实践 机器和人各有所长,如coding style检查这种可自动化的工作理应交给机器去完成,故发此文帮助你在几分钟内实现coding st ...

  3. electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google JavaScript Style Guide代码规范

    我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...

  4. 一张图总结Google C++编程规范(Google C++ Style Guide)

    Google C++ Style Guide是一份不错的C++编码指南,我制作了一张比較全面的说明图,能够在短时间内高速掌握规范的重点内容.只是规范毕竟是人定的,记得活学活用.看图前别忘了阅读以下三条 ...

  5. [Guide]Google Python Style Guide

    扉页 项目主页 Google Style Guide Google 开源项目风格指南 - 中文版 背景 Python 是Google主要的脚本语言.这本风格指南主要包含的是针对python的编程准则. ...

  6. [Guide]Google C++ Style Guide

    0.0 扉页 项目主页 Google Style Guide Google 开源项目风格指南 -中文版 0.1 译者前言 Google 经常会发布一些开源项目, 意味着会接受来自其他代码贡献者的代码. ...

  7. Google C++ Style Guide在C++11普及后的变化

    转 http://www.cnblogs.com/chen3feng/p/5972967.html?from=timeline&isappinstalled=0&lwfrom=user ...

  8. [中英对照]Linux kernel coding style | Linux内核编码风格

    Linux kernel coding style | Linux内核编码风格 This is a short document describing the preferred coding sty ...

  9. Google Shell Style Guide

    转自:http://google.github.io/styleguide/shell.xml Shell Style Guide Revision 1.26 Paul Armstrong Too m ...

随机推荐

  1. JVM(三)从JVM源码角度看类加载器层级关系和双亲委派

    类加载器我们都知道有如下的继承结构,这个关系只是逻辑上的父子关系. 我们一直听说引导类加载器没有实体,为什么没有实体呢? 因为引导类加载器只是一段C++代码并不是什么实体类,所谓的引导类加载器就是那一 ...

  2. 编程小技巧之 Linux 文本处理命令(二)

    合格的程序员都善于使用工具,正所谓君子性非异也,善假于物也.合理的利用 Linux 的命令行工具,可以提高我们的工作效率. 本篇文章是<Linux 文本处理命令> 续篇,在前文的基础上再介 ...

  3. Netty之JAVA BIO模型

    一.JAVA BIO模型 1.I/O模型 I/O 模型简单的理解:就是用什么样的通道进行数据的发送和接收,很大程度上决定了程序通信的性能 Java 共支持 3 种网络编程模型/IO 模式:BIO.NI ...

  4. v-modal的使用。

    v-model用于表单数据的双向绑定,其实它就是一个语法糖,这个背后就做了两个操作:v-bind绑定一个value属性:v-on指令给当前元素绑定input事件.

  5. 在VMware15安装Ubuntu 16.04

    安装环境: VMware15 VMware15官网地址:https://my.vmware.com/cn/web/vmware/info/slug/desktop_end_user_computing ...

  6. loj黑暗城堡

    黑暗城堡 题目描述 你知道黑暗城堡有\(N\)个房间,M 条可以制造的双向通道,以及每条通道的长度. 城堡是树形的并且满足下面的条件: 设\(D_i\)为如果所有的通道都被修建,第i号房间与第1号房间 ...

  7. HTML5.1 新增的14项特性学习

    1.防止网络钓鱼攻击 使用target=_'blank'时, 新打开的标签可以更改window.opener.location到一些钓鱼网站,它会在开放页面上代表你执行一些Javascript代码.为 ...

  8. apache https 双向证书生成

    Https分单向认证和双向认证 单向认证表现形式:网站URL链接为https://xxx.com格式 双向认证表现心事:网站URL链接为https://xxx.com格式,并且需要客户端浏览器安装一个 ...

  9. JS 实现一个实时动态校验,将输入格式错误的显示为红色背景

    功能描述: 源码: 功能描述: 实时动态校验,如果输入的格式错误,将弹窗提示输入格式错误并将背景展示为红色. 源码: 前台: <hy:formfield name="cxfdl&quo ...

  10. Kubernetes (yaml 文件详解)

    # yaml格式的pod定义文件完整内容:apiVersion: v1       #必选,版本号,例如v1kind: Pod       #必选,Podmetadata:       #必选,元数据 ...