为什么不在同一行声明多个相同类型的变量?
为什么在一行中声明变量是一种不好的做法?
例如
private String var1, var2, var3
,而不是:
private String var1;
private String var2;
private String var3;
Why is it bad practice to declare variables on one line?
e.g.
private String var1, var2, var3
instead of:
private String var1;
private String var2;
private String var3;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
在我看来,将每个变量放在单独的行上的主要目标是促进版本控制工具的工作。
如果多个变量位于同一行,则不同开发人员可能会因不相关的修改而发生冲突。
In my opinion, the main goal of having each variable on a separate line would be to facilitate the job of Version Control tools.
If several variables are on the same line you risk having conflicts for unrelated modifications by different developers.
在 C++ 中:
i 是 int * 类型,j 是 int 类型。
这种区别很容易被忽视。
除了将它们各占一行之外,以后添加一些注释也变得更加容易
In C++ :
i is of type int *, j is of type int.
The distinction is too easily missed.
Besides having them on one line each makes it easier to add some comments later
我认为原因有很多,但都归结为第一个原因可读性较差,而且更容易失败,因为一行要做不止一件事。
所有这一切都没有真正的收获,难道你不告诉我你发现节省的两行空间是真正的收获吗?
时发生的情况类似。
这与当你有“当然这个例子比你的例子糟糕得多”
I think that there are various reasons, but they all boil down to that the first is just less readable and more prone to failure because a single line is doing more than one thing.
And all that for no real gain, and don't you tell me you find two lines of saved space is a real gain.
It's a similar thing to what happens when you have
Of course this example is much worse than yours.
在C/C++中,还存在一个问题,即用于指示指针类型的*仅适用于紧随其后的标识符。 因此,缺乏经验的开发人员的一个相当常见的错误是编写
并期望所有三个变量都是“int 指针”类型,而对于编译器来说,这读取为
仅使 var1 成为指针。
In C/C++, you also have the problem that the * used to indicate a pointer type only applies to the directly following identifier. So a rather common mistake of inexperienced developers is to write
and expecting all three variables to be of type 'int pointer', whereas for the compiler this reads as
making only var1 a pointer.
通过单独的行,您可以在描述变量使用的每行上添加注释(如果从名称中不清楚)。
With separate lines, you have the opportunity to add a comment on each line describing the use of the variable (if it isn't clear from its name).
因为在某些语言中,示例中的 var2 和 var3 不是字符串,而是变体(无类型)。
Because in some languages, var2 and var3 in your example would not be strings, they would be variants (untyped).
为什么这是不好的做法? 我不认为是这样,只要你的代码仍然可读。
Why is that bad practice? I don't think it is, as long as your code is still readable.
诸如此类的情况又如何:
这也被认为是不好的做法吗? 我认为没关系,因为它反驳了之前提出的一些观点:
所以在一个(尽管是臭代码)示例中,您有理由不这样做吗?
What about the case such as:
Is that considered bad practice as well? I would consider that okay as it counters some of the points previously made:
So in an (albeit smelly code) example, is there reasons you wouldn't do that?
我的原因如下:
Here's my reasons:
说实话我并不反对。 我认为将相似的变量分组在同一行上是完全可行的,例如
float fMin, fMax;
然而,当变量不相关时,我会避开,例如
int iBalance、iColor;
To be honest I am not against it. I think that its perfectly feasible to group similar variables on the same line e.g.
float fMin, fMax;
however I steer clear when the variables are unrelated e.g.
int iBalance, iColor;
关联。
仅仅因为两个变量是 String 类型并不意味着它们彼此密切相关。
如果两个(或多个)变量通过函数而不是变量类型密切相关,那么也许它们可以一起声明。 即,只有当程序的读者将两个变量放在一起有意义时,它们实际上才应该放在一起
Relevance.
Just because two variables are of type String does not mean they are closely related to each other.
If the two (or more) variables are closely related by function, rather then variable type, then maybe they could be declared together. i.e. only if it makes sense for a reader of your program to see the two variables together should they actually be placed together
同意 edg 的观点,还因为将每个变量放在单独的行上更具可读性且易于维护。 您可以立即看到类型、范围和其他修饰符,并且当您更改修饰符时,它仅适用于您想要的变量 - 这可以避免错误。
Agree with edg, and also because it is more readable and easy for maintenance to have each variable on separate line. You immediately see the type, scope and other modifiers and when you change a modifier it applies only to the variable you want - that avoids errors.
假设您需要使用多种语言进行编码,可能是 C、C#、VBA 等。
通过采取更保守的立场,即适当地拆分声明,
您实际上采用了一种更通用的风格。 你会第一次就做对,至少在更多时候是这样。
此外,VBA 中的样式
会隐式将 var2 和 var3 声明为 Variant。 该代码可以工作,因为 var2 和 var3 会默默地成为变体。 方便,但令人讨厌。
最后也是最不重要的一点是,拆分声明可以让您更自由地分配初始默认值。 更短的代码。
或者在 VBA 中,就像
我总是试图坚持良好的编码习惯,这种习惯会传播到其他语言中。
Suppose you need to code in several languages, maybe C, C#, VBA, etc.
By adopting a more conservative stance, that is, splitting the declaration appropriately,
you're really adopting a style which is a bit more universal. You'll get it right the first time, at least more often.
Further, a style like
in VBA would implicitly declare var2 and var3 as Variant. The code would work because var2 and var3 would be, silently, variants. Convenient, but nasty.
Last and certainly least, splitting the declarations gives you a bit more freedom to possibly assign intial default values. Shorter code.
or in VBA, something like
I would always try to stick with decent coding habits which would spread around other languages.
当您可以并且想要初始化减速变量时,这通常是不好的做法。 这可能还不错的一个例子是:
It is bad practice mostly when you can and want to initialize variables on the deceleration. An example where this might not be so bad is:
一般来说,出于其他人讨论的版本控制和注释原因,我会在 95% 的情况下应用它。 然而,在某些情况下它确实有意义,例如,如果我正在编码图形并且我想要几个变量来表示纹理坐标(按照惯例总是引用为 s 和 t),那么将它们声明为
int s, t; // 纹理坐标
恕我直言,通过缩短代码和明确这两个变量属于一起来增强代码的可读性(当然,有些人会主张在这种情况下使用单点类变量)。
Generally it is, for the version control and commenting reasons discussed by others, and I'd apply that in 95% of all cases. however there are circumstances where it does make sense, for example if I'm coding graphics and I want a couple of variables to represent texture coordinates (always referenced by convention as s and t) then the declaring them as
int s, t; // texture coordinates
IMHO enhances code readability both by shortening the code and by making it explicit that these two variables belong together (of course some would argue for using a single point class variable in this case).
在尝试这个问题时 https://www.interviewbit.com/problems/remove -element-from-array/
方法 1 给出了此代码超出的内存限制:
类型 1:
类型 2:
类型 1:给出超出内存限制
而类型 2 工作得很好
while attempting this question https://www.interviewbit.com/problems/remove-element-from-array/
Method 1 gives Memory Limit exceeded for this code:
Type 1:
Type 2:
type 1: Gives Memory Limit Exceeded
Whereas type 2 works perfectly fine