应该是if()还是if()?
当您的代码行带有括号语句时,您是否在括号之前包含一个空格?
例如,应该是
if()
还是
if ()
提前致谢!
When you have line of code with a parenthetical statement, do you include a space before the parenthetical?
For example, should it be
if()
or
if ()
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
如果您是该项目中唯一的开发人员,请选择您喜欢的任何内容,否则请坚持团队使用的任何约定。
If you are the only developer on the project, pick whatever you prefer, otherwise stick with whatever convention the team is using.
这是一个品味问题。我个人使用带有空格
if ()
的版本,以便在视觉上将其与我不使用空格的函数调用区分开来(如在数学中)。It’s a matter of taste. I personally use the version with the space
if ()
in order to distinguish this visually from a function call where I don’t use a space (as in mathematics).Visual Studio 自动将其更改为
if ()
,所以我遵循它。我认为它的可读性稍高,但括号已经提供了一些空间,所以我认为这是完全不重要的事情之一。
Visual Studio automatically changes it to
if ()
, so I follow that.I think it's marginally more readable, but the brackets provide some space already as it is so I think this is one of those totally-doesn't-matter things.
在 php 中这并不重要,但你可能知道这一点。我在中间留了一个空格。我认为它更具可读性,可能是因为对于函数,我在它们之间没有放置空格,并且“if”不是函数:)
In php it doesn't matter, but you probably knew that. I'm putting a space in between. I think it's more readable, probably because for functions I put no space in between and 'if' is not a function :)
我不喜欢使用空格,对我来说,这样以后阅读起来更容易,而且我认识的许多人也不喜欢使用空格来阅读。
I prefer not to use a space, for me it makes it easier to read later and many of the people I know prefer no space as well for reading purposes.
您应该使用空格,因为并非所有语言都使用方括号分隔 if 语句。留出空间是更安全的习惯。
You should use a space because not all languages use bracket delimited if statements. Leaving a space is the safer habit to get into.
解释器和编译器会忽略它,所以你也可以忽略它!
Interpreters and compilers ignore it, So you can ignore it too!!!
这取决于编译器/语言是否忽略空格。 (虽然我想不出有什么会拒绝带有空格的“if”语句)。除了语言细节之外,这是您的偏好。话虽这么说,我更喜欢没有空格,因为它使代码看起来更简洁。
It depends on if the compiler/language ignores white-spaces. (all though off the top of my head i can't think of any that would balk at the "if" statement with a space). Outside of the language specifics, it's your preference. That being said, I prefer no space as it makes the code look more concise.
做任何一个,但要学会阅读其中任何一个,但不要陷入其中。
与括号样式相同的处理。
这就是我能给出的最有意义的答案了。
Do whichever, but learn to read either without getting caught up about it.
Same deal as with bracket style.
There, that's about as meaningful an answer I can give to this.
主观,但我更喜欢有空间,因为它看起来更干净,或者可能是因为这就是我学会使用诸如
switch
、if
、<代码>while和for
。如果您希望它“看起来像”函数调用,则可以不使用空格,因为我不使用空格进行函数调用。
Subjective, but I prefer having the space because it looks cleaner, or maybe because that's just how I learnt to 'do it' with language constructs such as
switch
,if
,while
andfor
.You could do it without the space if you like it to 'look like' a function call, because I don't use a space for function calls.
我更喜欢将其保留为我使用的 IDE 的默认行为,以降低源代码控制系统标记我的编辑和同事的编辑之间微不足道的差异的风险。其他样式问题也是如此,例如缩进多少空格、是否使用空格或制表符缩进等等。
I prefer keeping it to the default behavior of the IDE that I use, in order to reduce the risk of the source control system flagging insignificant differences between my edits and those of my co-workers. The same goes for other styling issues such as how many spaces to indent, whether to indent using space or tab and so on.
这绝对是个人偏好(或与其他开发人员合作时的团队偏好),从长远来看并不重要,但我(和我认识的其他人)不喜欢没有空间。
无论您选择什么,只要保持一致即可。没有什么比查看多种编码风格混合在一起且缺乏一致性的代码更让我烦恼的了。
It's definitely a personal preference (or a team preference when working with other developers) and really doesn't matter in the long run, but I (and everybody else I know) prefer no space.
Whatever you choose, just be consistent. Nothing bothers me more than looking at code that has multiple coding styles all mixed together with no consistency.