为什么人们没有缩进C ++访问说明符/ case语句? [英] Why don't people indent C++ access specifiers/case statements?

查看:147
本文介绍了为什么人们没有缩进C ++访问说明符/ case语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到这样的东西:

  class SomeClass {
public:
void someMethod );
private:
int someMember;
};

这对我来说完全不自然(同样适用于 case - 使用开关时的状态)。我期待这样,当我开始使用C ++(从那以后很长时间,但我仍然想知道):

  class SomeClass {
public:
void someMethod();
private:
int someMember;
};

是否有资助的原因打破(否则)一致的缩进规则?

解决方案

增加缩进通常反映进入新的嵌套范围,而访问说明符和切换 case 不改变范围(一般来说标签也是如此)。访问说明符可以是可选的,因为您可以开始实现类或结构,并且所有成员只需要隐式访问(即分别为私有和公共访问),但随后代码演变,您需要为非隐含访问添加一个说明符,访问成员:是否真的值得突然改变所有成员上的缩进?再次,没有嵌套的范围,所以我认为这将是积极的误导。



如果你在固定宽度的终端上工作,并相应地换行,reindenting的痛苦。但是,使用访问说明符从成员中脱颖而出是有用的,这意味着将它们放回到左边某个地方 - 符合类关键字或部分行。



个人而言,我这样做:

  class X 
{
public:
int member_function()
{
switch(expression)
{
case X:
return 6;

默认值:
{
int n = get_value()/ 6;
return n * n;
}
}
}

private:
int member_variable_;
};

为什么我不为每个 case缩进代码进一步?我不能声称我做的是特别合乎逻辑的,但因素包括:




  • 我不想强调一般的 switch / case 缩进,可视化地传达交换机的范围对于快速理解代码很重要

  • 我很高兴将 {和} 代码 - 更像是一个注释嘿,我需要一个范围 - 而不是感到迫切要缩进一切进一步,这并没有什么意义,甚至对我,但种类的感觉是正确的 - 我喜欢有每个 case 的代码

  • 某些编译器会在 case 语句,而不引入作用域,即使没有任何情况下,变量以后可能未初始化使用

  • 我是一个80列编码器一般,缩进通常,因此在切换内 - 因此显然是一个函数 - 意味着剩余的列是有价值的。



相同 / struct


$ b b

  • 我想扫描左侧的列并快速找到类的结尾,或者轻松计算屏幕上或打印输出中的小类;具有完全相同缩进级别的访问说明符不强调关键字,但是它有助于使它们与成员视觉上不同(如果类/结构体超过几行,我也预先添加一个空行)

  • 我不想改变现有的类 / struct成员缩进当我介绍
  • c

    总之:很多小因素都涉及到开发人们的缩进偏好,如果你想成为一个C ++程序员在企业环境 - 特别是承包商 - 你只需要去与流程,并能够改变自己(例如,我现在卡在camelCaseLand,公共成员变量以大写字母开头 - yikes!)。不要出汗 - 这是不值得的。


    I often see stuff like this:

    class SomeClass {
    public:
        void someMethod();
    private:
        int someMember;
    };
    

    This seems totally unnatural to me (the same applies to case-statements when using switch). I expected something like this, when i started using C++ (it's been a long time since then, but i am still wondering):

    class SomeClass {
        public:
            void someMethod();
        private:
            int someMember;
    };
    

    Is there a funded reason to break (otherwise) consistent indentation rules?

    解决方案

    Increasing indentation normally reflects entry to a new nested scope, whereas both access specifiers and switch case statements don't vary the scope (the same is true of labels generally). Access specifiers can be optional in that you may start implementing a class or struct and have all members only need the implied access (i.e. private and public respectively), but then the code evolves and you need to add a specifier for the non-implied-access members: is it really worth having to suddenly change the indentation on all members? Again, there's no nested scope, so I think that would be actively misleading.

    If you work on fixed-width terminals and wrap your lines accordingly, reindenting's a pain. But, it is useful to have the access specifiers stand out from the members, which means putting them back to the left somewhere - either in line with the class keyword or part-way there.

    Personally, I do this:

    class X
    {
      public:
        int member_function()
        {
            switch (expression)
            {
              case X:
                return 6;
    
              default:
              {
                int n = get_value() / 6;
                return n * n;
              }
            }
        }
    
      private:
        int member_variable_;
    };
    

    Why do I not indent code for each case further? I can't claim what I do is particularly logical, but factors include:

    • I don't want to deemphasise the general switch/case indentation, as visually communicating the extent of the switch is important to quickly understanding the code
    • I'm happy just to put the { and } around existing case code - more like a comment "hey, I need a scope" - rather than feeling compelling to indent everything further, which doesn't make much sense even to me but kind of feels right - I like having the code for each case line up
    • some compilers do warn if you introduce new variables inside a case statement without introducing a scope, even if there are no cases when the variable is later used potentially uninitialised
    • I'm an 80-column coder generally, with 4 space indents normally, so being inside a switch - and thus obviously a function - means remaining columns are valuable.

    Same for class/struct:

    • I want to scan the left column and quickly find the end of the class, or easily count the small classes on screen or in a printout; access specifiers at exactly the same indentation level deemphasise the class keyword, but it does help to have them visually distinct from the members (if the class/struct is more than a few lines, I also add a blank line beforehand)
    • I don't want to change existing class/struct member indentation when I introduce a private or protected specifier

    In conclusion: lots of small factors go into developing people's indentation preferences, and if you want to be a C++ programmer in a corporate environment - especially a contractor - you just have to go with the flow and be able to change your own style sometimes too (e.g. I'm stuck in camelCaseLand right now, with public member variables starting with an uppercase letter - yikes!). Don't sweat it - it's not worth it.

    这篇关于为什么人们没有缩进C ++访问说明符/ case语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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