为什么一行中的多个分配被认为是不良样式? [英] Why are multiple assignments in one line considered bad style?

查看:66
本文介绍了为什么一行中的多个分配被认为是不良样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,您可以执行此操作

In C++ you can do multiple assignment by doing this

x = y = z = 10;

然而,有很多人告诉我这是一种不好的风格,如果不给我一个理由,我不应该使用它.

Yet multiple people have told me that is a bad style and I shouldn't be using it without giving me a reason why.

有人可以向我解释为什么这被认为是不好的风格吗?

Can someone please explain to me why this is considered a bad style?

推荐答案

这并不是天生的坏样式,但是您通常可以通过每行只执行一次分配并让编译器优化器对它们进行整理来使代码更清晰.如果您使用多重分配样式,则有时可能不清楚x = y = z = 10;是故意的还是对于x = y = z + 10;之类的错字.通过始终将自己限制为每个语句只有一个分配,您可以在出现错字时将其变得显而易见.

It's not inherently bad style, but you can often make the code clearer by doing just one assignment per line and letting the compiler optimizer sort things out. If you use the multiple-assignment style then sometimes it might not be clear whether x = y = z = 10; was intentional or whether it was a typo for something like x = y = z + 10;. By always limiting yourself to one assignment per statement you make it obvious when typos occur.

这篇关于为什么一行中的多个分配被认为是不良样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆