以下第二种缩进方法的一般意见是什么?
// Normal indentation
a.Value = "foobar";
ab.Checked = false;
foo.Value = "foobar";
foobar.Checked = true;
// Spaces before the dot to align the properties/methods
a .Value = "foobar";
ab .Checked = false;
foo .Value = "foobar";
foobar.Checked = true;
Run Code Online (Sandbox Code Playgroud)
这应该是一个维基,但我要么没有足够的权限,要么不知道如何更改它.
编辑
我已经决定添加另一个例子来更好地展示这种缩进样式可能有用的地方.
fooA .PropertyA = true;
foobarB.PropertyA = true;
Run Code Online (Sandbox Code Playgroud)
使用VS2010中的新多线编辑功能,可以更轻松地更改所有线路上的PropertyA.
在C#中也有空格,甚至在点之前的换行并不罕见(参见LINQ).
我有这样的链接:
<a href="#" onclick="renkDegistir()" title="Yaz .......
Run Code Online (Sandbox Code Playgroud)
和JS代码:
function renkDegistir()
{
$("#cont #main article").addClass("siyah-stil");
}
Run Code Online (Sandbox Code Playgroud)
单击此链接时,siyah-stil类将添加到文章中.但我想这样做,如果链接点击第二次删除siyah-stil类.
综上所述 ,
on first click : addClass("siyah-stil");
on second click : removeClass("siyah-stil");
Run Code Online (Sandbox Code Playgroud) 我最近有一个家庭作业问题,我们应该在棋盘上工作,并在棋盘上放置一些女王.
这个问题在困难方面是微不足道的,但我想问的是:
我应该只创建一个布尔二维数组并将包含一个皇后的每个位置更改为1,还是应该创建一个私有类来表示一个皇后,它有x和y坐标作为实例变量?
这可能看起来不是很重要或者很紧迫,但我正在使用Java,它有点接近OO编程概念的核心.如果我们从不使用Java的模块化功能,那么为什么要使用Java呢?我们不妨在C或Python中编写相同的东西.
你认为哪种更合适?如果你可以将你的答案限制在理性支持的答案而不是意见或个人偏好,我将不胜感激.
我有一个方法,例如getSome(String param, boolean active).当我调用这个方法时,我创建了一个变量,如下所示.
boolean active = true;
getFoo("some", active); // To get active foo
getFoo("some", !active); // To get inactive foo
Run Code Online (Sandbox Code Playgroud)
是否值得创建一个额外的变量或只是调用 getFoo("some", true);
一个javascript库,比如Prototype,如何决定何时编写一个接受一系列参数的函数或者只接受一个巨大的对象?以下是两者的例子.每个人的利弊都很平衡,所以我不明白何时使用一种方法或另一种方法.
new Ajax.Request(
'server.php', {
onSuccess: function() {
console.info('I succeeded');
},
onFailure: function() {
console.info('I failed');
}
);
Run Code Online (Sandbox Code Playgroud)
优点:对象中的参数顺序无关紧要.当您忘记参数的顺序时,您不必检查函数定义.此外,当其他人阅读该功能的调用时,他们会更好地了解您正在做的事情.想象一下,如果Ajax调用是这样的:
new Ajax.Request(
'server.php',
function() {
console.info('mystery function');
},
function() {
console.info('is this success or failure?');
}
);
Run Code Online (Sandbox Code Playgroud)
缺点:函数标题没有清楚地显示需要传递的内容.您被迫保持评论的最新状态.
// unclear what options need to be passed in without comments
Ajax = {
Request: function(url, options) {
}
};
// a little clearer
Ajax = {
Request: function(url, successCallback, failureCallback) {
}
};
Run Code Online (Sandbox Code Playgroud)
new PeriodicalExecuter(
function(myself) …Run Code Online (Sandbox Code Playgroud) 我要比较4个变量,a,b,c,d如果它们中的任何一个-1返回false.这可能是多么简洁?可能是一些数学运算可以做!! 我不喜欢为这个简单的事情浪费这么多的字符或线条.
所有,
我是一名自学成才的程序员 - 而不是CS毕业生 - 所以我可能会经常忽略数百种编码最佳实践.
无论如何,这是一个普遍的问题......
编码时,使用更多代码或更多变量(或数组,散列等)来实现逻辑是否更好?
这是一个模糊的问题,但这里有一个特定的"例如..."
我正在为RIA构建UI; 其中一个可供选择的是一行中的一系列小点 - 每个点都是可点击的,并且用作导航,允许用户选择不同的屏幕.
(想想iPhone中可以切换屏幕的主屏幕底部的点,或者此页面上的图像切换器导航:http://www.apple.com/ipad/)
无论如何,我已经将这个"点导航控件"实现为自定义的Sprite子类.当用户单击一个点时,该类将一个自定义事件调度到包含与所点击的点对应的索引值(uint)的侦听器(例如,"0"是第一个点,"n-1"是第n个点).
现在,在监听器中,我需要采取行动 - 将用户导航到相应的页面.所以,一个明显的选择:
private function dotClicked(e:customDotEvent):void {
// e.target.index contains the index of the dot clicked
switch (e.target.index) {
case 0:
// navigate the user to the screen that corresponds to dot 0
loadScreen("home");
....
break;
case 1:
// navigate the user to the screen that corresponds to dot 1
loadScreen("about");
....
break;
....
case n:
// navigate the user to …Run Code Online (Sandbox Code Playgroud) 有人看过Adobe在他们网站上的ColdFusion编码指南吗?它似乎消失了,我没有副本.
哪个更pythonic?
T = 0
for a in G:
T += a.f()
Run Code Online (Sandbox Code Playgroud)
要么
T = sum(a.f() for a in G)
Run Code Online (Sandbox Code Playgroud) 我有这个简单的正则表达式替换代码,其中包含一个块.当Ruby执行gsub时,匹配将传递给块,并且从块返回的任何内容都将用作替换.
string = "/foo/bar.####.tif"
string.gsub(/#+/) { | match | "%0#{match.length}d" } # => "/foo/bar.%04d.tif"
Run Code Online (Sandbox Code Playgroud)
有没有办法在Python中做到这一点,同时保持简洁?是否有支持lambdas或with语句的++ replace ++变体?
coding-style ×10
javascript ×2
python ×2
block ×1
c ×1
c# ×1
coldfusion ×1
flash ×1
java ×1
jquery ×1
math ×1
prototypejs ×1
regex ×1
ruby ×1
syntax ×1
variables ×1
xor ×1