为什么它被认为是不好的做法,忽略大括号? [英] Why is it considered a bad practice to omit curly braces?

查看:91
本文介绍了为什么它被认为是不好的做法,忽略大括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么每个人都告诉我,写code一样,这是一个不好的做法?

 如果(富)
    酒吧();//要么的for(int i = 0 I<计数;我++)
    杆(一);

我最大的省略大括号的说法是,它有时是两倍多行他们。例如,这里是一些code画在C#标签的发光效果。

 使用(刷BR =新SolidBrush(Color.FromArgb(15 GlowColor)))
{
    为(中间体X = 0; X&下; = GlowAmount; X ++)
    {
        对于(INT Y = 0; Y< = GlowAmount; Y ++)
        {
            g.DrawString(文字,this.Font,BR,新点(IconOffset + X,Y));
        }
     }
 }
 //与
使用(刷BR =新SolidBrush(Color.FromArgb(15 GlowColor)))
    为(中间体X = 0; X&下; = GlowAmount; X ++)
        对于(INT Y = 0; Y< = GlowAmount; Y ++)
            g.DrawString(文字,this.Font,BR,新点(IconOffset + X,Y));

您还可以得到链接的好处 usings 在一起,而无需缩进万次。

 使用(图形G = Graphics.FromImage(BMP))
{
    使用(刷刷=新SolidBrush(的backgroundColor))
    {
        使用(笔笔=新朋(Color.FromArgb(penColor)))
        {
            //做大量的工作
        }
    }
 }
//与
使用(图形G = Graphics.FromImage(BMP))
使用(刷刷=新SolidBrush(的backgroundColor))
使用(笔笔=新朋(Color.FromArgb(penColor)))
{
    //做大量的工作
}


有关大括号中的最普遍的说法是围绕着维持性节目,这将通过插入原来的if语句和预期结果之间code随之而来的问题:

 如果(富)
    酒吧();
    商务();


问题:


  1. 这是错误的希望使用更紧凑的语法,语言的优惠?该设计这些语言都是聪明人,我无法想象他们会放一个功能,它总是不好用。

  2. 我们应该或不应该我们写code这样的最小公分母能够理解,并与它的工作没有问题?

  3. 有没有办法,我失踪另有说法?


解决方案

其实,这是有史以来唯一一次真的咬了我是在我的调试,并注释掉条():

 如果(富)
  //巴();
doSomethingElse();

除此之外,我倾向于使用:

 如果(富)巴();

这需要上述情况的照顾。

修改澄清的问题谢谢,我同意,我们不应该写code的最小公分母。

Why does everyone tell me writing code like this is a bad practice?

if (foo)
    Bar();

//or

for(int i = 0 i < count; i++)
    Bar(i);

My biggest argument for omitting the curly braces is that it can sometimes be twice as many lines with them. For example, here is some code to paint a glow effect for a label in C#.

using (Brush br = new SolidBrush(Color.FromArgb(15, GlowColor)))
{
    for (int x = 0; x <= GlowAmount; x++)
    {
        for (int y = 0; y <= GlowAmount; y++)
        {
            g.DrawString(Text, this.Font, br, new Point(IconOffset + x, y));
        }
     }
 }
 //versus
using (Brush br = new SolidBrush(Color.FromArgb(15, GlowColor)))
    for (int x = 0; x <= GlowAmount; x++)
        for (int y = 0; y <= GlowAmount; y++)
            g.DrawString(Text, this.Font, br, new Point(IconOffset + x, y));

You can also get the added benefit of chaining usings together without having to indent a million times.

using (Graphics g = Graphics.FromImage(bmp))
{
    using (Brush brush = new SolidBrush(backgroundColor))
    {
        using (Pen pen = new Pen(Color.FromArgb(penColor)))
        {
            //do lots of work
        }
    }
 }
//versus
using (Graphics g = Graphics.FromImage(bmp))
using (Brush brush = new SolidBrush(backgroundColor))
using (Pen pen = new Pen(Color.FromArgb(penColor)))
{
    //do lots of work
}


The most common argument for curly braces revolves around maintance programming, and the problems that would ensue by inserting code between the original if statement and its intended result:

if (foo)
    Bar();
    Biz();


Questions:

  1. Is it wrong to want to use the more compact syntax which the language offers? The people that design these languages are smart, I can't imagine they would put a feature which is always bad to use.
  2. Should we or Shouldn't we write code so the lowest common denominator can understand and have no problems working with it?
  3. Is there another argument that I'm missing?

解决方案

Actually, the only time that's ever really bit me was when I was debugging, and commented out bar():

if(foo)
  // bar();
doSomethingElse();

Other than that, I tend to use:

if(foo) bar();

Which takes care of the above case.

EDIT Thanks for clarifying the question, I agree, we should not write code to the lowest common denominator.

这篇关于为什么它被认为是不好的做法,忽略大括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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