哪个更好/更普遍接受? [英] Which is better/more generally accepted?

查看:58
本文介绍了哪个更好/更普遍接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这:

This :

if(condition)
{
  statement;
}






OR

if(condition)
  statement;





我更喜欢第二种选择,需要选择第一种选择的技术细节。



I prefer 2nd option, in need technical details of any to select 1st option.

推荐答案

每种计算机语言都实现了范围的概念,即执行操作的执行上下文和上下文(名称空间上下文),其中存在对象(变量,类等),并且可以访问。他们如何做到这一点的语义差别很大。



在C#中, if 语句定义逻辑块的开始,包括如果对条件表达式的求值导致布尔值'为真,则执行代码。



包含要执行的代码的大括号定义执行范围。



无论你写道:
Every computer language implements the concept of "scope" which is the execution context in which actions will be executed, and the context (namespace context) in which "objects" (variables, classes, etc.) exist, and are accessible. The semantics of how they do this vary widely.

In C#, an if statement defines the start of a logical "block" which consists of the code to be executed if the evaluation of the conditional expression results in the boolean value 'true.

The braces that enclose code to be executed define execution scope.

Whether you write:
// Kernighan and Ritchie style
if(condition)
{
     // do stuff
     // do more stuff
}

// 1TBS (one true bracing style)
if(condition){
     // do stuff
     // do more stuff
}

风格问题和约定[ ^ ]。对于那些对花括号的格式进行几乎宗教战争的人来说,这也是一个很有意义的问题,无论是否使用制表符或空格更好[ ^ ]等



C#允许你省略仅在的情况下定义代码的逻辑块的封闭大括号,你希望执行单个代码语句。



人们不同意关于在单个语句的情况下是否允许使用无括号的特殊语法是一件好事。



我认为这不是一件好事,并且养成总是使用牙套的习惯是一个非常好的主意。从长远来看,这种做法将导致更易读,可维护的代码,并帮助避免琐碎的错误。



C#也允许你使用?:分隔符对作为特殊的if / then / elseshort-cut:[ ^ ]。我觉得在某些情况下很有用。

Is a matter of style, and convention [^]. It's also a matter of great meaning to some people who engage in almost "religious" wars over the formatting of curly braces, whether or not using tabs or spaces is better [^], etc.

C# allows you to omit the enclosing braces that define a logical block of code only in the case you wish to execute a single code statement.

People disagree about whether allowing a no-braces-enclosed special syntax for the case of a single statement is a "good thing."

I am among those who think it is not a "good thing," and that it's a very good idea to get in the habit of always using braces, and that this practice, in the long-run, will lead to more readable, and maintainable code, as well as helping one avoid trivial errors.

C# also allows you to use the "?:" pair of delimiters as a special if/then/else "short-cut:" [^]. I find that useful in certain cases.


没有更好的,我宁愿选择第一个选项,因为它使你的代码更容易阅读。但是没有理由说任何一个比另一个更好或更差。



Eduard
None is better, I'd rather go for the first option because it makes your code easier to read. But there's not reason to say either one is better or worse then the other.

Eduard


如果只有一个肯定你可以去第二,但如果我们在这种情况下有一套陈述,我们需要去第一。

所以它的要求问题。需要做相应的事情。

- SG
If its a single definitely you can go for 2nd but in case we have set of statement in that case we need to go for 1st.
So its matter of requirement. Need to do accordingly.
- SG


这篇关于哪个更好/更普遍接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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