您是否将条件代码放在换行符上还是仅在某些时候? [英] Do you put conditional code on a new line or only sometimes?

查看:44
本文介绍了您是否将条件代码放在换行符上还是仅在某些时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常会总是按照以下条件写条件代码

I generally will always write conditional code along the lines of

if (number==1) 
    name="a";
if (number==2)
    name="b";

不管有多短,尽管我发现有很多人在写作

no matter how short, although I find a fair chunk of people writing

if (number==1) name ="a";
if (number==2) name ="b"...

对我来说,这似乎不一致,并且实际上使代码看起来更混乱.

To me this seems inconsistent, and actually makes the code look messier.

做到这一点的最佳方法是什么?

What is the best way to do this?

推荐答案

编码样式是最受争议的主题之一(通常带有宗教色彩). 有关此问题的资源很多,我将尝试查找链接.

Coding styles are one of the most hotly debated topics (and often with religious fervor). There are lots of resources about this question, I'll try to find the links.

无论哪种方式,我都喜欢用大括号将每个这样的条件身体包裹起来;我的经验是,如果增加或省略线条,它们可以使身体更加可见,并减少出错的风险.

Either way, I am a fan of wrapping every such condition body with curly braces; my experience is that they make the body more visible and reduce the risk of errors if lines are added or omitted.

这篇关于您是否将条件代码放在换行符上还是仅在某些时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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