根据Google JavaScript样式指南,函数声明不应在块中声明,因为它不是ECMAScript的一部分.但是,我并不完全清楚什么算作块.
具体来说,我有一个构造函数,我想在该构造函数的范围内定义一个函数.这是否算作一个块内的函数,因为它在一组{}内?如果是这样,这是否意味着每个函数声明必须是全局的?
一些好的衡量标准:
function Constructor() {
function Shout () { alert('THE BEST UX IS IN ALL CAPS.'); }
}
Run Code Online (Sandbox Code Playgroud)
function Constructor() {
var Shout = function () { alert('THE BEST UX IS IN ALL CAPS.'); };
}
Run Code Online (Sandbox Code Playgroud) 我很好奇Google Python样式指南中有关属性的建议之一.
在其中,他们给出了以下示例:
class Square(object):
"""A square with two properties: a writable area and a read-only perimeter.
To use:
>>> sq = Square(3)
>>> sq.area
9
>>> sq.perimeter
12
>>> sq.area = 16
>>> sq.side
4
>>> sq.perimeter
16
"""
def __init__(self, side):
self.side = side
def __get_area(self):
"""Calculates the 'area' property."""
return self.side ** 2
def ___get_area(self):
"""Indirect accessor for 'area' property."""
return self.__get_area()
def __set_area(self, area):
"""Sets the 'area' property."""
self.side = math.sqrt(area)
def ___set_area(self, area): …Run Code Online (Sandbox Code Playgroud) python properties decorator google-style-guide python-decorators
我理解"分号隐式插入",但我不确定它是否出现在函数表达式的情况下.
例如,这两个表达式总是会同意解释:
Matrix.scale = function (mat, scaleX, scaleY, dest) {
// useful code
};
Matrix.scale = function (mat, scaleX, scaleY, dest)
{
// useful code
};
Run Code Online (Sandbox Code Playgroud)
我喜欢第一个,我也注意到谷歌也喜欢它:http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showow = Code_formatting#Code_formatting.但是我的同事对这种风格不满意.问题是,即使使用函数声明,是否严格遵守此规则,或者它是对均匀性的致敬,并且这个代码在棘手的缩小之后不会破坏?
在Google风格的JavaScript编写指南(证明链接)中,使用"const"结构是一种不好的做法,为什么呢?
我已经在他们的C++样式指南中使用谷歌的DISALLOW_COPY_AND_ASSIGN宏几个月了,但最近我发现,额外禁用移动构造函数和移动赋值会很有用.
我之前没有写过任何真正的宏(实际上,我一直试图尽可能远离它们),所以我想从其他人那里得到一些关于我是否实现它的反馈正确.
// Original Version
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
// Modified Version (no move semantics)
#define DISALLOW_COPY_MOVE_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&); \
TypeName(TypeName&&); \
void operator=(const TypeName&&)
Run Code Online (Sandbox Code Playgroud)
建议和批评是非常受欢迎的.
谢谢你的时间!
我正在使用 cpplint 根据谷歌风格指南检查我的源代码。
Cpplint 的帮助说:
cpplint.py supports per-directory configurations specified in CPPLINT.cfg
files. CPPLINT.cfg file can contain a number of key=value pairs.
Currently the following options are supported:
"exclude_files" allows to specify a regular expression to be matched against
a file name. If the expression matches, the file is skipped and not run
through liner.
Example file:
filter=-build/include_order,+build/include_alpha
exclude_files=.*\.cc
The above example disables build/include_order warning and enables
build/include_alpha as well as excludes all .cc from being
processed by linter, in …Run Code Online (Sandbox Code Playgroud) 我在Google 的 Python styleguide 中阅读了以下内容:
“除非关闭本地值,否则避免嵌套函数或类”。
“关闭本地值”是什么意思?
完整的相关部分如下:
2.6 嵌套/本地/内部类和函数
当用于关闭局部变量时,嵌套局部函数或类很好。内部课程很好。
2.6.1 定义
类可以在方法、函数或类中定义。函数可以在方法或函数中定义。嵌套函数对封闭作用域中定义的变量具有只读访问权限。
2.6.2 优点
允许定义仅在非常有限的范围内使用的实用程序类和函数。非常ADT-y。通常用于实现装饰器。
2.6.3 缺点
嵌套或本地类的实例不能被腌制。嵌套的函数和类不能直接测试。嵌套可以使您的外部函数更长且可读性更低。
2.6.4 决定
他们没有问题,但有一些警告。避免嵌套函数或类,除非关闭本地值。不要嵌套函数只是为了对模块的用户隐藏它。相反,在模块级别用 _ 前缀它的名称,以便它仍然可以被测试访问。
python styles inner-classes nested-function google-style-guide
我正在转换我的代码以遵循google C++ 风格指南。引用参数规则规定“所有通过引用传递的参数都必须标记为 const”和“输入参数是值或 const 引用,而输出参数是指针”。
关于签名void MyTable::LoadTable(ifstream &fin),我如何标记fin const给定的参数LoadTable调用一些非常量函数fin,例如fin.seekg?我认为fin应该被视为输入/输出参数,因此它有点特殊。在这种情况下,谷歌员工会怎么做?
更新:我知道谷歌风格指南有很多批评。我只是想知道谷歌员工如何解决这个问题,也许我找到了答案:还有另一个规则Streams读取“仅使用流进行日志记录”。也许他们只是在这种情况下不使用流。
javascript ×3
python ×3
c++ ×2
c++11 ×1
cpplint ×1
decorator ×1
ecmascript-5 ×1
pep8 ×1
properties ×1
regex ×1
styles ×1