在我的入门编程课程中,我的老师在代码中声明实例变量时总是使用以下命名约定:
public class Snowman {
private Ellipse _top;
private Ellipse _middle;
private Ellipse _bottom;
public Snowman() {
_top = new Ellipse();
_top.setColor(Color.WHITE);
_top.setFrameColor(Color.BLACK);
_top.setFrameThickness(1);
_top.setSize(80, 80);
_middle = new Ellipse();
_middle.setColor(Color.WHITE);
_middle.setFrameColor(Color.BLACK);
_middle.setFrameThickness(1);
_middle.setSize(120, 120);
_bottom = new Ellipse();
_bottom.setColor(Color.WHITE);
_bottom.setFrameColor(Color.BLACK);
_bottom.setFrameThickness(1);
_bottom.setSize(160, 160);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在课程教科书中,实例变量不是以下划线开头,而是遵循与基本类型变量(int revolutionsPerMinute)相同的命名约定.当我进行在线搜索时,我发现了几个来源,包括在线Javadoc,它引用了与我的教科书相同的约定.由于我无法重现我的老师所遵循的命名惯例,我对他的惯例是否合法持怀疑态度.会议是否教会我的老师即使不被广泛接受也是如此?
一个比另一个更可读吗?起初,我不喜欢这种false ===方法,但正如我越来越常见,我正在热身.我很确定他们会得到相同的结果.
我最近发现我的一位程序员写了类似的东西:
int foo()
{
//some code
{
//some code
}
//some code
{
//some code
}
//some code
}
Run Code Online (Sandbox Code Playgroud)
如您所见,两对内部的卷曲括号仅用于逻辑分隔两段代码.虽然我写了一段时间的C,但我从来没有真正看过这样的风格.这被认为是一种好的,或者至少是C中可接受的风格吗?
用一个处理两者的方法替换getter和/或setter方法是个好主意吗?
例如:
function name($what = null){
if(!$what)
return $this->name;
$this->name = $what;
}
Run Code Online (Sandbox Code Playgroud)
使用如下:
// get name
print $obj->name();
// set name
$obj->name('bla');
Run Code Online (Sandbox Code Playgroud)
我见过一些框架做到了.它被社区认为是好的还是坏的做法?:P
它似乎更有效,但它看起来有点混乱,因为我习惯于在PHP中使用getThing()和setThing().这种风格让我想起了jQuery.
我有一个列表Dsr
>>> Dsr
[59.10346189206572, 40.4211078871491, 37.22898098099725]
type(Dsr)
<type 'list'>
Run Code Online (Sandbox Code Playgroud)
我需要计算最大值并将该值的列表的每个元素分开
dmax = numpy.max(Dsr)
RPsr = []
for p in xrange(Dsr):
RPsr.append(float(Dsr[p]/dmax))
Run Code Online (Sandbox Code Playgroud)
我有以下问题:
1)当我运行此循环时,我收到了错误消息:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: an integer is required
Run Code Online (Sandbox Code Playgroud)
2)是否有可能在最优雅的列表理解中转换循环?
我写了这个函数来使用另一个函数,检查输出是否有正确的后缀.在我使用之前,我有两个问题:
在这种情况下使用是TypeError最好的例外吗?
是否有一些我可以使用的内置功能而不是我的功能?
码:
def suffix_NameCheck(inFile):
if os.path.splitext(inFile)[1] is None:
raise TypeError('"%s" has not a suffix' % inFile)
else:
return inFile
Run Code Online (Sandbox Code Playgroud) 回调越来越成为编码的必要条件,尤其是当您考虑Node.JS非阻塞工作方式时.但是很快就会写下大量的协程回调变得难以回读.
例如,想象像这样的金字塔末日:
// This asynchronous coding style is really annoying. Anyone invented a better way yet?
// Count, remove, re-count (verify) and log.
col.count(quertFilter, function(err, countFiltered) {
col.count(queryCached, function(err, countCached) {
col.remove(query, function(err) {
col.count(queryAll, function(err, countTotal) {
util.log(util.format('MongoDB cleanup: %d filtered and %d cached records removed. %d last-minute records left.', countFiltered, countCached, countTotal));
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
是我们经常看到的东西,很容易变得更加复杂.
当每个函数至少延长几行时,开始分离函数变得可行:
// Imagine something more complex
function mary(data, pictures) {
// Do something drastic
} …Run Code Online (Sandbox Code Playgroud) 我经常想做这样的事情:
unsigned char urlValid[66] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
Run Code Online (Sandbox Code Playgroud)
...要么:
unsigned char listOfChar[4] = "abcd";
Run Code Online (Sandbox Code Playgroud)
...也就是说,从字符串文字中初始化一个字符数组,并忽略该字面值中的空终止符.它非常方便,而且我可以做类似的事情sizeof urlValid并得到正确的答案.
但不幸的是它给出了错误initializer-string for array of chars is too long.
有没有办法:
请原谅,我是Python的新手,并试图学习Pythonic方法.我正在设计一个基本上从许多不同来源(文件)初始化其状态的类.我已将此行为隔离到单个实例方法中_load_from_file(file).它被称为多次__init__,但我通常喜欢将我的构造函数保留在类定义的开头,并将我的内部帮助器方法放在最后.
但是,如果我采用这种方法,_load_from_file则不会__init__在我想要使用它的地方定义.你怎么把pythonistas列出来?
详细说明:
class Thing(object):
def __init__(self, file_path):
f = open('file_path')
_load_from_file(self,"someData",f) # ISSUES!
_load_from_file(self,"moreData",f) # WRONG!
f.close()
# Interface
# ...
# Internal - Where do you guys put this stuff?
def _load_from_file(self,thingToLoad,file):
# logic
Run Code Online (Sandbox Code Playgroud) coding-style ×10
python ×3
c ×2
class ×2
java ×2
php ×2
syntax ×2
callback ×1
javascript ×1
list ×1
node.js ×1
oop ×1
performance ×1