你试图让你的代码看起来漂亮吗?
我可能是一名肛门程序员,但我喜欢从远处看起来不错的代码。 我刚刚发现自己正在排列一个 CSS 样式,而不是这样:
#divPreview {
text-align: center;
vertical-align: middle;
border: #779 1px solid;
overflow: auto;
width: 210px;
height: 128px;
background-color: #fff"
}
它现在看起来像:
#divPreview {
width: 210px;
height: 128px;
overflow: auto;
text-align: center;
vertical-align: middle;
border: #779 1px solid;
background-color: #fff";
}
我几乎总是按照大小顺序编写数字比较,就像
if (0 < n && n < 10)
而不是
if (0 < n && 10 > n)
最后我会倾向于安排 if-then-else 代码,以便THEN 部分比 ELSE 部分小(因为较重的东西会落到底部,对吗?)
if (afflicted == true) {
GetSome();
HelpRight();
}
else {
NowPlease();
}
呃!
if (afflicted == false) {
HowMuchTrouble();
}
else {
IsItToDo();
ThisReally();
}
啊啊,
我可以继续举更多的例子,但你明白了……
问题:我是唯一一个患有神经症的人吗? 你的编码问题是什么?
I might be one anal programmer, but I like code that looks good from a distance. I just found myself lining up a CSS style so that instead of this:
#divPreview {
text-align: center;
vertical-align: middle;
border: #779 1px solid;
overflow: auto;
width: 210px;
height: 128px;
background-color: #fff"
}
it now looks like:
#divPreview {
width: 210px;
height: 128px;
overflow: auto;
text-align: center;
vertical-align: middle;
border: #779 1px solid;
background-color: #fff";
}
I will almost always write numerical comparisons in order of size like
if (0 < n && n < 10)
instead of
if (0 < n && 10 > n)
and finally I will tend to arrange if-then-else code so that the THEN part is smaller than the ELSE part (cause the heavier stuff goes to the bottom, right?)
if (afflicted == true) {
GetSome();
HelpRight();
}
else {
NowPlease();
}
ugh!
if (afflicted == false) {
HowMuchTrouble();
}
else {
IsItToDo();
ThisReally();
}
aahhh
I could go on and on with more examples, but you get the idea...
Question: Am I alone in my neurosis here? What's your coding kink?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
根据我的经验,漂亮的代码通常运行得很好,而丑陋的代码通常会以难以发现的方式崩溃。 确实,那种关注细节(例如代码的外观)的头脑也是那种关注更重要的细节的头脑。
也就是说,以 CSS 样式重新排序元素,使它们从短到长是疯狂的。
In my experience, pretty code usually works very well, while ugly code often breaks in hard-to-spot ways. It's just true that the kind of mind that pays attention to details like how the code looks is also the kind of mind that pays attention to more important details.
That said, re-ordering the elements in a CSS style so that they go short-to-long is bat******** crazy.
肯定条件总是在 if-else 语句中首先出现。 美在于内在。
Positive condition first in an if-else statement, always. Beauty lies within.
让代码看起来“好”很重要,这就是我们制定约定和风格指南的原因。 目的是使代码易于阅读并使错误的代码看起来错误。 我想说,这里的一些例子超出了这个范围,没有什么价值。
CSS 属性的顺序
我发现保持属性顺序一致比确保它们按每个选择器的长度升序排列更有用。 这一惯例使您可以更轻松地找到所需的人。
条件语句的顺序
这里的示例对我来说很有意义,因为它清楚地表明 n 介于 0 和 10 之间。
if/else 块的顺序
我认为首先拥有积极条件(或更预期的条件)更有意义。 并且“== true”位是多余的。
It is important to make code look "good" which is why we have conventions and style guidelines. The objective it to make code easy to read and to make wrong code look wrong. I would say that some of the examples here go beyond that and add little value.
Order of CSS Properties
I would find it more useful to be consistent in the order of the properties rather than making sure they are in ordered in ascending length for each selector. That convention makes it easier to find the one you are looking for.
Order of conditionals
You example here makes sense to me because it clearly shows n is between 0 and 10.
Order of if/else blocks
I would think it makes more sense to have the positive condition first (or the more expected condition). And the "== true" bit is redundant.
我可能会更进一步这样做:
I'd probably go even further and do this :
应该
买一台漂亮的打印机,并接受强迫症治疗;-)
it should be
buy a prettyprinter, and get treated for OCD ;-)
我的看法有点不同。 代码的格式应该是为了可读性,而不一定是“漂亮”——尽管这两者通常可能是同一件事。
但作为一种反例,
它比更漂亮的更具可读性:
我知道这一点是有争议的,我完全理解它,我喜欢那个运算符和所有的东西,但是对于 95% 的程序员来说(包括下一个阅读你的文章的程序员)代码)需要更多的时间来解析。
事实上,我什至在 ruby 的优雅方面遇到了麻烦:
仅仅是因为我习惯于通过扫描代码的左侧行来查看控制语句。
我不得不承认,当我看到这个语法时,我的膝盖有点发软(我认为它来自 hascal),
这是一种优雅的程度,甚至可以压倒我厌倦的看法。
最后,虽然格式化对于在代码中显示模式确实很巧妙,但如果您的代码中甚至有模式,那么您可能做得不对。
每当您的代码中有模式时,请寻找重构机会。 尝试识别数据并将其取出,然后将模式的重复部分分解为某种消耗数据的循环。
顺便说一句,您的第一个示例是数据,而不是代码——数据也非常需要格式化——甚至可能更需要格式化。
I view it a little differently. Code should be formatted for readability, not necessarily"Prettiness"--although those two may usually be the same thing.
but as a counter case,
is just more readable than the prettier:
I know the point is arguable, I totally get it and I love that operator and all, but for 95% of the programmers out there (including the next one to read your code) it's going to take a little bit more to parse.
In fact, I even had trouble with ruby's quite elegant:
Simply because I'm so used to seeing control statements by scanning the left line of the code.
I do have to admit that I got a little weak in the knees when I saw this syntax (I think it's from hascal)
That's a level of elegance that can even overpower my jaded perceptions.
Finally, although formatting can be really neat for bringing out patterns in your code, if you even HAVE patterns in your code you're probably not doing it right.
Whenever you have patterns in your code, look for a refactoring opportunity. Try to identify data and pull it out, then factor out the repeated parts of the pattern to be a loop of some sort that consumes the data.
By the way, your first example was data, not code--data very much needs formatting as well--maybe even more so.
代码应该易于阅读且易于维护。 “漂亮”完全符合这些要求,只要你别太可爱就行。
Code should be easy to read an maintainable. "Pretty" is entirely consistent with those requirements, so long as you don't get too cute with it.
我对强迫症格式化有些过度,但我仍然认为保持代码尽可能可读很重要,除非你有真正令人信服的理由不这样做。 最大的原因是:您 6 个多月前编写的代码可能是其他人编写的。 帮自己一个忙,保持代码整洁和一致。
I go overboard with my OCD formatting, but I still think it's important to keep code as readable as possible unless you have a really compelling reason not to. The biggest reason: code you wrote over 6 months ago may as well have been written by someone else. Do yourself a favor and keep your code clean and consistent.
我也喜欢我的代码漂亮,但不喜欢它影响可读性。
我更喜欢将“快乐路径”放在 if 语句中,然后是异常路径。 如果我预计某条路径比其他路径更频繁地发生,那么它就会先行。 它并不需要像克罗森沃尔德所喜欢的那样是一个“积极”的条件。 这样,它读起来就像我的用例。
(blizpasta 在异常路径之前的 happy-path 击败了我。他提到了我故意避免优化的地方......我更喜欢我的代码漂亮且可读。优化排在最后。)
在 CSS 中,我更喜欢“结构性”或首先布局属性,这样我就可以快速看到某些东西将在哪里结束,然后才知道它会是什么样子。 我会重新组织你的第一个 CSS 示例,使其看起来像你的第二个,但出于我的原因 - 不是你的。 :)
I like my code to be pretty too, but not where it interferes with readability.
I prefer to put "happy path" first in an if statement, followed by exception paths. If I expect a certain path to happen more frequently than others, it goes first. It doesn't need to be a "positive" condition as krosenvold prefers. This way, it reads like my use cases.
(blizpasta beat me to the punch with the happy-path preceding exception paths. He mentions optimization where I purposely avoided it... I prefer my code pretty and readable. Optimization comes last.)
And in CSS, I prefer "structural" or layout attributes first so I can quickly see where something is going to end up, and only then what it's going to look like. I would reorganize your first CSS example to look like your second, but for my reasons - not yours. :)
你并不孤单:-)我经常使用对齐练习(就像史蒂夫的回答一样):
越容易阅读越好。
我尝试系统地做的另一件小事是根据运算符优先级对表达式中的术语进行分组,即
而不是
You're not alone :-) I use often the alignment practice (like in Steve's answer):
The easier to read, the better.
Another little thing I try to do systematically is to group the terms in an expression according to operator precedence, i.e.
instead of
很多事情对我来说都是有背景的。
空间或不空间...
折叠多少...
当我编码时,我“必须”做出很多这样的决定。 这对我很有帮助,但如果没有把这些做好,我的大脑不会让我继续前进。 我正在康复:-)
Many things are contextual for me.
Space or not to space...
How much to fold...
I 'had to' make a lot of decisions like these when I code. It helps mostly, but my mind won't let me more forward without getting these right. I am recovering :-)
对我来说,编写可读的代码是最好的。 不管格式如何,只要其他程序员在接手编程时能够读懂,那就很漂亮了。
并且总是有意见。
For me, writing readable code is best. No matter how the format is, as long as it is readable for other programmers when/if they take over programming, it is pretty.
And always have comments.
我总是让我的代码看起来很漂亮。 我发现它更容易阅读。 我格式化所有变量及其值,使它们对齐,并使用空格将事物逻辑地分组在一起。 我还尝试按照变量的使用顺序以及它们所属范围的顶部来声明变量。
I always make my code look pretty. I find it easier to read. I format all variables and their values so they line up, and I use white space to logically group things together. I also try to declare variables in the order they are used and at the top of the scope they belong to.
我没有看到你的 css 示例中的要点...史蒂夫有一个更易读的示例。
至于 if 语句 - 我认为没有任何押韵或理由将最重的放在底部。 我通常把更常见的预期情况放在第一位。
I don't see the point in your css example... Steve has a more readable example.
As for the if statements - I see no rhyme or reason to putting the heaviest at the bottom. I usually put the more commonly expected situation first.
您的示例应该是:
遵循名称的宽度而不是整体线宽。 ;)
但是,是的,你疯了。 :P
Your example should have been:
Following the width of the name and not the overall line width. ;)
But, yeah, you're crazy. :P
代码的样式应该可读、可维护且顺序一致。 就我个人而言,我发现任意间距的代码很难阅读。 为什么要强迫读者扫描屏幕/页面上的代码以将语句的各个部分放在一起? 语句中代码块的顺序应按照逻辑顺序、可读性进行定位,有时还需要进行调整以进行优化。 不必要的间隔是浪费时间,并且使代码更难阅读,而不是更容易。 没有人读过麦康奈尔的书吗?
Code should be styled to be readable, maintainable, and the order consistent. Personally, I find code with arbitrary spacing to be hard to read. Why force the reader to scan the code across the screen/page to put the pieces of the statement together? The order of code blocks in statement should be positioned in order of logic, readability, and sometimes adjusted for optimization. Unnecessarily spacing things out is a waste of time, and makes the code harder to read, not easier. Hasn't anyone read McConnell?
在某种程度上我是。 但我更倾向于最小化。 CSS 绝对是我的例子。 这里添加的内容太长了,但基本上,我对它们进行了阿尔法排序。
为什么?
因为我必须即时做出比我想要的更多的改变。 Alpha 对其中的所有内容进行排序使我可以快速滚动并在一组中找到所有需要更改的内容,而不是使用搜索并到处查找它。
对你们来说这可能是纳秒,但它为我节省了很多挫败感和后来损失的时间。
In a way I am. But I am more into minimizing. Css is definitely my example. It is too long to add here but basically, I alph-order it all.
Why?
Because I have to do changes on the fly a lot more than I want to. Alpha ordering everything in there allows me to just scroll fast and get to what needs the change all in one grouping as opposed to using search and finding it everywhere.
It may be nano seconds to you all but it saves me a lot of frustration and time lost later.
按字母顺序排列效果更好; 它在复杂 CSS 文件中的好处比在小文件中大得多。
Alphabetical ordering scales better; its benefit in a complex CSS file is much greater than in a small file.
我也进行数值比较,因为它对我来说看起来更数学化,而且我更容易将其可视化为数轴。
不过,您的一些特殊风格将不会得到优化。 例如,如果您要根据案例中的语句数量来排列 switch 语句中的案例,则无法通过将更可能的案例放在顶部来优化它。
我不进行网络编程,但我想按照您的方式重新排列CSS代码是可以的,因为它们出现的顺序并不重要? 尽管执行顺序在非 Web 编程中很重要,但在重新排列语句时必须小心副作用。
I do the numerical comparison thing too as it looks more mathematical to me and it's easier for me to visualise it like a number line.
Some of your idiosyncratic style will be non-optimised though. For example if you were to arrange the cases in a switch statement according to number of statements in a case, you won't be able to optimise it by putting the more likely cases at the top.
I don't do web programming but I guess it's okay to rearrange the css code the way you did as the order in which they appear shouldn't matter? Order of execution matters in non-web programming though, have to be careful about the side effects when rearranging statements.
让它看起来“漂亮”通常没有坏处,但就像大多数事情一样,要适度。 例如,我希望每次都将项目保持相同的顺序,并将类似的项目组合在一起,而不是对它们进行排序。 对齐运算符和值很好,但不要更改顺序。
至于“if”语句。 我总是把期待的结果放在第一位,将特殊的结果放在最后。
There is normally no harm in making it look "pretty", but as in most things use moderation. For example I would prefer to keep items in same order each time with like items group together instead of sorting them. Aligning the operators and values are fine, but don't change the order.
As for "if" statement. I always put the expect out come first and the exceptional out come last.
当涉及到代码格式时,我也患有强迫症。 我同意史蒂夫的观点,最好的办法就是列出你的属性。
这让我有点痛。 有些人可能不喜欢它,但这让我感觉好多了:
我的看法是,如果我在代码的某个部分花费大量时间,我希望它不会打扰我的视觉感受并分散我的注意力手头的任务。 另外,我认为它给代码带来了整洁和专业的感觉,有助于加快未来的增强。
I too suffer from a OCD when it comes to code format. I'm with Steve, tabbing out your properties is the way to go.
That hurts me a little. Some may not like it, but this makes me feel much better:
The way I see it is if I'm spending a lot of time in a certain section of code, I would prefer it not bother my visual sensibilities and distract me from the task at hand. Plus, I think it gives the code a neat and professional touch that helps expedite future enhancements.
我认为好的代码看起来漂亮并且有很好的注释是绝对必要的。
当人们要求我调试没有结构的代码时,我无法忍受。
I think it is absolutely necessary for good code to look good and be well commented.
I can't stand it when people ask me to debug code with no structure.
我更喜欢保持简单的格式风格。 块缩进,并按逻辑/优化排序。 我使用 IDE 的格式化程序(至少在 Visual Studio、Eclipse 和 Netbeans 中),但是,对于带有缩进子项的每种样式,我使用 CSS 一行:
I prefer keeping the formatting style simple. Indent on blocks, and order by logic/optimization. I use the formatter of the IDE (at least in Visual Studio, Eclipse, and Netbeans) With CSS, however, one line for each style with indented children:
对我来说漂亮是可读的。
看起来很漂亮,但是一段时间后阅读这样的代码是很痛苦的。
在这种情况下添加新变量是一件痛苦的事情。
我停止
我的痴迷是:比较运算符之前和之后的空格以及左大括号之前的新行(奇怪的是为什么人们不使用这种方式而更喜欢在同一行上的左大括号方法)。
Pretty for me is readable.
looks beautiful but it is a pain to read such a code after sometime.
And adding new variables is a pain in this case.
I stop with
My obsessions are: a space before and after comparison operators and a new line before a opening brace (It is strange for why people don't use this way and prefer the opening-brace-on-the-same-line approach).
我知道在将代码投入生产之前我可能会阅读该代码近 15 遍。 花时间格式化代码,也许看起来我只是让代码看起来更漂亮,但对我来说,它让我的大脑有几秒钟的时间来思考代码,然后再继续下一步。
结构不整齐且一致的代码对我来说是一个警告,表明开发人员没有充分理解问题,无法清楚地表明他的意图,或者编写该代码的开发人员根本不在乎。 如果可以选择的话,我宁愿不与这样的人一起工作。
我相信史蒂夫·麦康奈尔说过,编译器并不真正关心代码的外观,但它是我们人类在大脑的范围和限制内工作的一种机制。
任何人都可以编写看起来很复杂的代码。 但是,需要优秀的开发人员才能将一段复杂的代码变成每个人都会指着它说的东西,哦,是的,这很容易理解。
I know that I will read that code probably close to 15 times before it is placed into production. Spending time in formatting the code, perhaps looks like I am only making the code look pretty, but for me, it gives my mind a couple of seconds to think through the code, before I move on to the next bit.
Code that is not structured neatly and consistently is a warning to me, that the developer did not understand the problem enough, to show his intentions clearly, or the developer that did that code just did not care. I would rather not work with such an individual if I have a choice.
I believe it is Steve McConnell who said, that the compiler does not really car how the code looks, but it is a mechanism for us humans, to work within the confines and limitations of brain.
Any person can write code that looks complex. But it takes a great developer to take a complex piece of code and turn it into something that everybody points to it and say, oh yes that is easy and simple to understand.
我与原始帖子不同的一件事是条件。 我认为这不是一个造型偏好问题,而是一个逻辑条件。 最常见的情况是第一种情况,然后会发展到最不常见的情况。 适用于我的代码中的 if 语句和 switch 语句。
The one thing that I vary with on the original post would be the conditional. I don't look as a conditional as a matter of styling preference, but rather a logical one. The most common case would be the first case, and would progress to the least common case. Applies to both if statements and switch statements in my code.
许多人提到,如果您尝试手动格式化代码,差异工具将显示不必要的差异。 然而,好的 diff 工具会忽略空格,所以这不是问题。
更重要的一点是,人编写的代码意味着人可以阅读。 因此,请尝试使其尽可能具有可读性。 如果额外的空格可以澄清您的代码,那么就这样做。 无论如何,大多数空格都是为了可读性而使用的。 例如,对于大多数语言来说,缩进纯粹是为了美观。
Many people mentioned that diff tools will show unnecessary diffs if you try to format your code manually. However, good diff tools ignore whitespaces, so this is not an issue.
A more important point is that code written by a person is meant to be read by a person. Therefore, try to make it as readable as possible. If additional whitepsaces can clarify your code, then do it. Most whitespaces are used for readability anyway. For example, indentation is purely aesthetic is most languages.
我喜欢确保代码行之间有足够的间距,并且我有注释来解释事情。 这就是我整理的程度。
在 Visual Studio 中,我明智地使用 Ctrl+K 然后 Ctrl+D(自动格式代码)以使所有内容正确对齐/间隔。 不过,我不像你那么强迫症:)
I like to make sure there's adequate spacing between lines of code, and I have comments that explain things. That's about the extent of my tidying up.
In Visual Studio, I use Ctrl+K then Ctrl+D (Autoformat code) judiciously to have everything aligned/spaced properly. I'm not as OCD as you, though :)
参数的垂直对齐看起来不错,但对我来说,它分散了我将声明作为语句进行扫描的注意力。
我完全着迷地剪切并粘贴了我的一个片段:
很多间距:这是由于编写了大量示例代码,并且是纯粹为了可读性而编写的翻转。
我希望你的第二个例子的形式是规范的,我在大学时被教导要像在数字线上一样编写这些比较:
至于最后一个例子,我会省略布尔比较。 一个真实的案例,然后一个错误的案例:
The alignment of the parameters vertically looks nice, yet it for me it detracts me from scanning across the declaration as a statement.
I've cut and paste a snippet of me in full obsessiveness :
Lots of spacing : this is due to writing alot of example code, and is a rollover from writing purely for readability.
I would hope your second example's form is the norm, I was taught at varsity to write these comparisons as if they were on a numberline :
And as for the last example, I would omit the boolean comparison. A true case then a false :
任何在代码更改时让您重新排序的代码风格都是不好的。
它会搞砸差异。 您正在使用版本控制系统,对吗?
还有一些其他的东西可以让你的代码更漂亮,但会搞砸差异。
想象一下这段代码:
现在让我们通过排列 = 符号来使其更漂亮:
但是然后我们添加另一个变量:
现在如果您进行比较,所有 3 行都发生了变化,而只有最后一行发生了变化。
还有更多,在方法之间排列参数是不好的
逗号和分号排列得很好,但是如果您更改了代码,则必须手动重新格式化所有 sqrt 行在一起并再次搞砸差异。
基本上,永远不要手动格式化。 始终使用 IDE 或漂亮的打印机强制执行代码样式。 但也永远不要选择当代码没有更改时格式会改变的代码样式
编辑:
如评论中所述,某些比较工具可以忽略行内的空格。 这很棒,但并非所有 diff 工具都能做到这一点。 如果您要对齐字段或参数,请确保:
Any code style that makes you reorder things when code changes is bad.
It would screw up diffs. You are using a version control system right?
There's a few other things that would make your code prettier, but screw up diffs.
Imagine this code:
Now let's make it prettier by lining up the = signs:
But then we add another variable:
Now if you did a diff, all 3 lines have changed, when only the last one did.
And there's more, lining up params between methods is bad
The comma and semi-colon are nicely lined up, but if you ever change the code, you'll have to manually reformat all the sqrt lines that are together and screw up the diffs again.
Basically, never do manual formatting. Always enforce code styles using an IDE or pretty printer. But also never choose a code style where the format will change when your code did not
EDIT:
As stated in the comments, some diff tools can ignore whitespaces within lines. This is great, but not all diff tools can do this. If you are aligning fields or params, make sure you are: