简体   繁体   English

我们应该在Java中使用方法名后面有换行符吗?

[英]Should we have a newline after method name in Java?

This is one doubt regarding best practice in Java. 这是对Java最佳实践的一个疑问。 Is it good idea to leave a blank line after method name. 在方法名称后留一个空行是个好主意。 Eg. 例如。 say I have following code 说我有以下代码

public void print(String str) {
    System.out.println("Hello word" + str);
}

So, here is it good practice to leave a blank line before Print statement? 那么,在Print语句之前留一个空行是不错的做法? I checked Java doc regarding that. 我检查了Java doc。 http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141388.html http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141388.html

It says, Blank should be :- Between the local variables in a method and its first statement 它说,Blank应该是: - 在方法中的局部变量和它的第一个语句之间

I am not sure if it means the same thing that I am asking or it is something else. 我不确定它是否意味着我要求的东西或者其他东西。

Anything you can tell would be great. 你能说的任何东西都会很棒。

The standard doesn't apply in your case because you have no local variables. 该标准不适用于您的情况,因为您没有局部变量。 So I'd say 所以我会说

// YES
public void print(String str) {
    System.out.println("Hello word" + str);
}

is just fine. 很好。 Much better than: 比以下好多了:

// NO
public void print(String str) {
                                      // This blank line is unnecessary
    System.out.println("Hello word" + str);
}

The standard is talking about something like this: 标准是谈论这样的事情:

// YES
public void print(String str) {
    double rand = Math.random(100);
                                        // A blank here is good
    System.out.println("Hello word" + str + "; today's number is " + rand);
}

being preferred over 优先于

// NO
public void print(String str) {
    double rand = Math.random(100);
    System.out.println("Hello word" + str + "; today's number is " + rand);
}

Short answer: No. That piece of the standard is meaning that you should leave a gap between the local variables and the first statement inside the method. 简答:不。该标准的一部分意味着你应该在局部变量和方法的第一个语句之间留一个空隙。

ie: 即:

String someText = "text";
String moreText = "more text";
// gap here
System.out.println(someText + " " + anotherText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM