选择非标准 Java 代码缩进样式有风险吗?
如果您选择非标准缩进样式,会有什么不同吗?
这是我最常看到的样式:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test {
static public void main(String args[]) throws Exception {
FileInputStream fin = new FileInputStream("infile.txt");
FileOutputStream fout = new FileOutputStream("outfile.txt");
FileChannel inc = fin.getChannel();
FileChannel outc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
while (true) {
int ret = inc.read(bb);
if (ret == -1)
break;
bb.flip();
outc.write(bb);
bb.clear();
}
}
}
但我更喜欢这种样式,所有内容都从下一行开始:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test
{
static public void main(String args[]) throws Exception
{
FileInputStream fin = new FileInputStream("infile.txt");
FileOutputStream fout = new FileOutputStream("outfile.txt");
FileChannel inc = fin.getChannel();
FileChannel outc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
while (true)
{
int ret = inc.read(bb);
if (ret == -1)
break;
bb.flip();
outc.write(bb);
bb.clear();
}
}
}
我发现这更容易阅读,但如果我使用这种样式,我会遇到与其他人合作的问题吗?
Does it make a difference if you choose a non-standard indent style?
This is the style I see most often:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test {
static public void main(String args[]) throws Exception {
FileInputStream fin = new FileInputStream("infile.txt");
FileOutputStream fout = new FileOutputStream("outfile.txt");
FileChannel inc = fin.getChannel();
FileChannel outc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
while (true) {
int ret = inc.read(bb);
if (ret == -1)
break;
bb.flip();
outc.write(bb);
bb.clear();
}
}
}
But I prefer this style where everything starts on the next line:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test
{
static public void main(String args[]) throws Exception
{
FileInputStream fin = new FileInputStream("infile.txt");
FileOutputStream fout = new FileOutputStream("outfile.txt");
FileChannel inc = fin.getChannel();
FileChannel outc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
while (true)
{
int ret = inc.read(bb);
if (ret == -1)
break;
bb.flip();
outc.write(bb);
bb.clear();
}
}
}
I find this easier to read but will I encounter problems working with others if I use this style?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这更像是一个宗教问题,而不是一个编程问题。人们对代码格式化风格有强烈的看法。第一种格式遵循 Sun Microsystems 所提倡的风格,第二种格式遵循我所说的非猴子风格。如上所述,关于代码格式化风格的强烈意见比比皆是。
正如之前的一位或多位发帖者所述,公司使用代码格式化程序,因此您首选的格式样式可能无法通过签入源代码管理。
一致性很重要,但在一个由 6 名程序员组成的团队中,如果 5 个愚蠢的火箭产生了难以阅读的代码,那么与它们不一致并尝试产生不难阅读的代码可能是一个好主意。
This is more a religeous question, than a programming question. People have strong opinions about code formatting style. The first format does follow the style that Sun Microsystems has promoted, the second format follows what I like to call not-monkey style. As mentioned above, strong opinions abound concerning code formatting style.
As stated by one or more previous posters, companys use code formatters, so your preferred format style may not survive a checkin to source control.
Consistancy is important, but in a team of 6 programmers, if 5 goof rockets produce hard to read code, it may be a good idea to be inconsistant with them and, attempt, to produce not-hard to read code.
一致性非常重要。
如果您喜欢第二种风格,请坚持下去。 (我愿意。)
但是无论您最终选择使用什么样式,都不要在代码中间更改它。
{编辑] Netbeans 可以选择以您选择的样式 IIRC 显示代码。
Consistency is all important.
If you like the second style, stick with it. ( I do.)
BUT whatever style you eventually choose to use, do not change it in the middle of your code.
{edit] Netbeans has an option to display the code in your style of choice, IIRC.
我不同意你的标题。第一个标准和第二个非标准如何?
偏好会随着时间而改变。我们不再在 80*25 文本终端上编程。我们正在编写不同类型的代码。节省一行的理由变得越来越有趣。
I disagree with your title. How is the 1st one standard and the 2nd one non-standard?
Preferences changes over time. We no longer program on 80*25 text terminals. We are writing different type of codes. The justification to save one line becomes more and more amusing.
答案是,这取决于代码的受众。如果您能看到代码,那么您就可以做您喜欢的事情。如果其他人可能会看到该代码,那么这取决于您与他们的社交和/或专业关系。如果其他人可能会修改代码,那么不尝试以某种标准方式进行编码将是不专业的。
The answer is that it depends on the audience for the code. If only you will ever see the code, then you may do what you like. If others may see the code, then it depends on your social and/or professional relationship with them. If others might modify the code, then it would be unprofessional not to attempt to code in some standard way.
采用 Sun 十多年前制定的标准样式将使新加入组织的人员更容易跟上进度,代码格式将按照他们在之前职业生涯中习惯的方式进行。
除此之外,任何(经过深思熟虑的)样式只要一致地应用就可以(我花了很长时间来找出样式不一致的代码,其中不同的人在修改现有的代码时也采用了不同的样式)方法)。
Adopting the standard style as laid down by Sun over a decade ago will make it easier for people new to your organisation to get up to speed, it'll be code formatted in a way they've been used to during their prior careers.
Apart from that, any (well thought out) style will do as long as it is applied consistently (I've had a hall of a time figuring out inconsistently styled code, where different people working on it had employed different styles even while modifying existing methods).
这将使所有源始终保持一致,并确保源存储库中的更改是实际更改,而不仅仅是稍后重新格式化。
对于 Eclipse,这可以通过配置格式化程序(我碰巧喜欢默认值)来完成,并保存首选项,然后其他人可以加载这些首选项。还有Java->编辑->保存操作允许在每次按下 Ctrl-S 时自动重新格式化,这也是一个可保存的首选项。
我发现上面的内容中,额外的启发式“
提供了许多自动触发的重构,提供了许多命名的本地变量,然后通过命名捕获意图,这反过来又非常适合调试,就像您通常所做的那样单步执行时,调试器中显示的变量值更多,并且每行出现 NullPointerException 的机会往往更少。
编辑:我在我的博客上写道关于这个。
编辑 2014-08-19:看起来,如果 Eclipse 格式化程序设置保存到文件中,IntelliJ IDEA 可以使用该文件格式化源。
This will make all sources be uniform at all times, and ensure that changes in the source repository is actual changes and not just reformats at a later time.
For Eclipse this can be done by configuring the formatter (I happen to like the defaults), and save the preferences which can then be loaded by everybody else. Also the Java -> Editor -> Save actions allow for automatic reformatting at every Ctrl-S, which is also a savable preference.
I've found with the above that an additional heuristic
gives a lot of automatically triggered refactorings giving a lot of named locals which then capture intent by their naming, which in turn works well for debugging as you generally have more values in variables which show up in the debugger when single stepping, and you tend to have less opportunities for NullPointerExceptions on each line.
Edit: I wrote on my blog about this.
Edit 2014-08-19: It appears that if the Eclipse formatter settings are saved to a file, IntelliJ IDEA can format source using that file.
遵守惯例。您应该查看当前项目之外的代码、程序员流动、公司被收购、工具往往会根据标准进行配置等。
Stick with conventions. You should be looking at code outside of your immediate project, programmers move, companies are acquired, tools will tend to be configured for the standard, etc.
使用哪种风格并不重要,但确保它与团队的其他成员保持一致。
通常这会涉及无休止的讨论,但我想这里列出的两个是更常见的。
It doesn't matter which style you use, but make sure it's consistent with the rest of your team.
Usually this involves endless discussions but I guess that the 2 ones listed here are the more common ones.
没关系。无论如何,大多数公司都会使用代码格式化程序。
我也更喜欢第二种风格。
It doesn't matter. Most companies use a code formatter anyways.
I also prefer the second style.
如果其他人都使用与您相同的大括号放置和缩进标准,您就不会有问题。
如果你是一匹孤独的狼,你就会遇到问题。
最大的问题是你的版本控制系统。您不希望人们在样式之间“加油”,并且由于样式更改而不是实质性的代码修改而出现许多差异。
在罗马,入乡随俗。在你的团队中达成共识并坚持下去。
PS - 我同意你的观点:我更喜欢在下一行加上大括号。太阳大会是第一个。这对书籍作者来说更好,因为空白更少。
You will not have problems with others if they all use the same brace placement and indentation standard as you do.
You will have problems if you're a lone wolf.
The biggest issue will be with your version control system. You don't want people to "oil can" between styles and have lots of differences showing up because of style changes rather than substantive code modifications.
When in Rome, do as the Romans do. Come to a consensus in your team and stick with it.
PS - I'm with you: I prefer to have braces on the next line. The Sun convention is the first one. It's better for book authors, because there's less white space.
与以往一样,当涉及到代码格式时,如果您与更喜欢另一种格式的人一起工作,那是有风险的。
As ever when it comes to code formatting, it is risky if you work with someone who prefer the other one.
我很久以前就不再担心代码格式了。争论花括号的位置真是浪费氧气。当我必须修改代码时,我使用 IDE 的代码格式化程序使用默认值重新格式化文件。然后我做我的工作并承诺它。当其他人更改文件时,他们可以根据自己的喜好对其进行格式化。我一点也不在乎。
我宁愿程序员花时间讨论算法、数据结构和测试策略。
I stopped worrying about code formatting long ago. What a waste of oxygen, to argue about placement of curly braces. When I have to modify code, I reformat the file with the IDE's code formatter, using the defaults. Then I do my work and commit it. When someone else changes the file, they can format it to their liking. I could not care less.
I'd sooner programmers spent their time discussing algorithms, data structures, and test strategies.
这确实是一个选择的问题。如果您发现一种方法比其他方法更容易阅读,那就继续吧!
唯一的例外是当您正在从事大型项目并为其做出贡献时。在这种情况下,您显然必须遵循在源代码的其余部分中维护的约定。
我个人是第一种方式的粉丝,大多数人都遵循这个惯例。
It's all a matter of choice really. If you find a method easier to read than the other than go ahead!
The only exception to this is when you are working on and contributing to a large scale project. In this scenario you will obviously have to follow the convention which is maintained throughout the rest of the source code.
I'm personally a fan on the first way and the majority of people follow this convention.