哪种声明变量的更好方法?

发布于 2024-10-20 06:33:41 字数 1429 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

万人眼中万个我 2024-10-27 06:33:41

这最终是个人品味的问题。不管怎样,这对编译器或你的程序来说都没有关系。

如果您与其他程序员在一个团队中工作,重要的是您遵循他们既定的标准。如果您要维护现有代码的基础,请遵循源代码中已建立的样式。否则,您可以自行决定如何格式化代码。

就我个人而言,我更喜欢第二种风格。它使我更清楚每个变量的类型是什么。此外,如果您使用 C 或 C++ 并声明指针,请务必记住,

int* i, j, k;

只会i 声明为指向 int< 的指针/code> (有关更多讨论,请参阅此问题)。使用第二种声明样式使其完全明确,这对于长期可维护性总是更好。通过将所有变量声明压缩到一行来节省的金额对我来说似乎不值得。

This is ultimately a matter of personal taste. It doesn't matter to the compiler or your program either way.

If you're working on a team with other programmers, the important thing is that you follow their established standards. If you're maintaining a base of existing code, follow the style already established in the source. Otherwise, you're free to make your own decisions about how to format your code.

Personally, I prefer the second style. It makes it much clearer to me what the types are of each variable. Additionally, if you're working in C or C++ and declaring pointers, it's important to keep in mind that

int* i, j, k;

will only declare i as a pointer to an int (see this question for more discussion). Using the second declaration style makes it completely unambiguous, which is always better for long-term maintainability. The amount you're saving by squashing all variable declarations to one line doesn't seem worth it to me.

誰認得朕 2024-10-27 06:33:41

这是一个品味问题,就编译器而言没有区别

It's a matter of taste, there is no difference as far as the compiler is concerned

倚栏听风 2024-10-27 06:33:41

第一个可能有助于提高声明中变量的可读性和轻松发现,特别是如果您不使用任何 IDE 并且单个文件中的代码很长

the first might help in readability and easy spotting of variables in declaration especially if you are not using any IDE and the code is long in single file

瑶笙 2024-10-27 06:33:41

你需要定义“更好”。就程序的效率而言,没有什么区别。

如果您指的是样式,那么我相信让每个变量单独占一行是最易读和最方便的:
int i; // 保存...的值
int j; // 迭代器...
int k; // Dummy for function()...

这也为您提供了为每个函数提供漂亮的描述性注释的选项。当然,这取决于您(以及科迪所说的您的团队)的品味和惯例。

You need to define "better". When it comes to the program's efficiency, there's no difference.

If you're refering to styling, then I believe it's most readable and convenient to allow every variable a line for itself:
int i; // Holds the value of...
int j; // Iterator for...
int k; // Dummy for function()...

That also gives you the option for nice descriptive comments per each. Naturally, it boils down to your (and your team's as Cody stated) taste and conventions.

幸福丶如此 2024-10-27 06:33:41

取决于您希望在代码中保持何种程度的可读性、可理解性和全面性。
第一种类型在可理解性方面表现良好,而第二种类型在可读性和全面性方面更适合。

另外,正如 @Code Gray 所提到的,如果不小心使用,在某些语言中使用第一种语法可能会令人困惑。

通常第一种类型总是在循环中使用,但在声明部分,我更喜欢第二种。

最后,您想要采用的是您的选择和风格。

Depends on what level of readability, understandability and comprehensiveness you want to keep in your code.
First type qualifies well in terms of understandability whereas second fits better in terms of readability and comprehensiveness.

Also as mentioned by @Code Gray, it can be confusing to use first syntax in some languages if it isn't used carefully.

Usually first type is always used in loops but in declaration sections, I prefer second one.

In the end, it is your choice and style that you want to adopt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文