Writing readable code means proper indentation. Usually you'd tab (or use 2 or 4 or 3 spaces) after every curly bracket. Something like this:
编写可读代码意味着适当的缩进。 通常,您在每个大括号后使用制表符(或使用2或4或3个空格)。 像这样:
if (true) {
// indent
if (false) {
// another indent
// and some more
}
}
Same goes when you have a bigger hash/object sort of thing:
当您具有更大的哈希/对象类型的东西时,也是如此:
var memememe = {
name: 'Stoyan',
family_name: 'Stefanov',
blog: 'http://www.phpied.com',
kids_count: 2,
books_count: 3,
occupation: 'programmer'
}
Sometimes I find myself going a little fancy and aligning all the values in the name/value pairs:
有时我发现自己有点花哨并且将名称/值对中的所有值对齐:
var memememe = {
name: 'Stoyan',
family_name: 'Stefanov',
blog: 'http://www.phpied.com',
kids_count: 2,
books_count: 3,
occupation: 'programmer'
}
But recently, inspired by Firebug's Net panel way of presenting header information, I tried aligning the keys to the right in addition to aligning the values to the left. So I ended up with something like this:
但是最近,受Firebug展示标题信息的Net面板方法的启发,除了将值左对齐外,我还尝试将键向右对齐。 所以我最终得到了这样的东西:
var memememe = {
name: 'Stoyan',
family_name: 'Stefanov',
blog: 'http://www.phpied.com',
kids_count: 2,
books_count: 3,
occupation: 'programmer'
}
Fancy, eh? I liked the way it looks. But then I thought that when writing maintainable code, anything fancy suggests uncommon, uncommon suggests that other team members won't be using it, so it means breaking the rule #1 of writing maintainable code: be predictable. (this also happens to be rule #1 of other common activities, such as driving on the highway and designing usable web sites)
看中,是吗? 我喜欢它的外观。 但是后来我认为,在编写可维护代码时,任何花哨的建议都很少见,很少见的是其他团队成员不会使用它,因此这意味着违反了编写可维护代码的规则#1:可预测。 (这也是其他常见活动的规则#1,例如在高速公路上开车和设计可用的网站)
This type of formatting is also not easy to type in an editor, so it will require a little more effort. Those two drawbacks are enough, I believe, to dismiss this idea. But I can't help myself liking the way the code looks. Here's a piece of PHP, which looks even better than javascript, because even more characters are centered.
在编辑器中键入这种格式也不容易,因此需要更多的努力。 我认为,这两个缺点足以消除这个想法。 但是我不能不喜欢代码的外观。 这是一段PHP,它看起来比javascript更好,因为居中的字符更多。
<?php
$memememe = array(
'name' => 'Stoyan',
'family_name' => 'Stefanov',
'blog' => 'http://www.phpied.com',
'kids_count' => 2,
'books_count' => 3,
'occupation' => 'programmer'
);
?>
Ain't that cool?
那不是很酷吗?
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息