简体   繁体   English

Java编码标准:变量声明

[英]Java Coding standard: variable declaration

Which of the following is best practice according to Java coding standards 根据Java编码标准,以下哪一项是最佳做法

public void function1(){
 boolean valid = false;
 //many lines of code
 valid = validateInputs();
 //many lines of code
}

or 要么

public void function1(){
 //many lines of code
 boolean valid = validateInputs();
 //many lines of code
}

Here 'valid' will not be for returning. 这里的“有效”将不会返回。 Its scope is internal to the function only. 其范围仅在功能内部。 Sometimes only in one if condition 有时只有一种情况

I usually code similar to the second case. 我通常编写类似于第二种情况的代码。 It seems my superior does not like this and modifies the code when I put it for review. 我的上级似乎不喜欢这样,并在我进行审查时修改了代码。 Is there some specific reason that my approach is not correct? 是否有某些特定原因导致我的方法不正确?

The disadvantage I see for the first approach is that it is very difficult to refactor the method to multiple methods at a later point. 我看到的第一种方法的缺点是,在以后很难将方法重构为多种方法。

I would go for the second approach - not much a matter of Java coding standards here, but a matter of clean and readable code. 我会选择第二种方法-这里不是Java编码标准的问题,而是干净易读的代码的问题。 Also, you assign the value false to valid in the first case, but that's not really correct as valid shouldn't have any value at that point. 另外,在第一种情况下,您将值false分配给valid ,但这并不是正确的,因为valid在那一刻不应该有任何值。

On a side note, I won't expect a method called validateInputs() to return a boolean . 附带说明一下,我不会期望一个名为validateInputs()的方法返回boolean There's no parameter passed, and the name is not giving an hint that the method would return something. 没有传递任何参数,并且名称没有暗示该方法将返回某些内容。 What about refactoring your code to something like boolean validInput = isValid(input) ? 将代码重构为boolean validInput = isValid(input)怎么样?

There should always be reasoning behind a decision. 决策背后总应该有推理。

The second example is better because it is good to initialize values in the declaration . 第二个示例更好,因为在声明中初始化值是很好

Google has a good set of standards that is applicable to many C-type languages. Google 有一套适用于许多C型语言的标准。 The example you are referring to is shown in the 'local variables' section. 您引用的示例在“局部变量”部分中显示。

I would prefer to only declare variables within the scope they are used. 我宁愿只在使用范围内声明变量。 This avoid accidentally using it when you shouldn't and means you can see both the declaration and the usage together, instead of having to jump to the start of your code to find it. 这样可以避免在不需要的时候意外使用它,这意味着您可以同时看到声明和用法,而不必跳到代码的开头来查找它。

In the C days, you had to use the first form, because the compilers were not very smart. 在C天中,您必须使用第一种形式,因为编译器不是很聪明。 But the second form was added as it made the code easier to understand AFAIK. 但是添加了第二种形式,因为它使代码更易于理解AFAIK。

Whichever is better is a matter of personal taste. 哪个更好取决于个人喜好。 Every place has its own standards, so you should follow it at work. 每个地方都有自己的标准,因此您应该在工作中遵循它。

That's one more reason I think every programmer should have their own personal projects. 这是我认为每个程序员都应该拥有自己的个人项目的另一个原因。 That way you can also code in your own style at home, so you don't get your mind stuck with just one style. 这样一来,您还可以在家中以自己的样式编写代码,因此您不会只拘泥于一种样式。

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

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