如果我们对类使用C前缀,我们是否也应该对struct使用它?

时间:2022-09-11 12:11:10

Assuming that a project has been using the C class prefix for a long time, and it would be a waste of time to change at a late stage, and that the person who originally wrote the style guide has been hit by a bus, and that there are no structs in the code already...

假设一个项目已经使用C类前缀很长一段时间,这将是一个浪费时间的改变在后期阶段,最初写的风格指南,人已经被车撞,而没有结构的代码已经……

It's a pretty trivial question, but if a C++ code style guide says "use C for class name prefix" then should this be taken to mean also use C for struct prefix also, or should we use something different, like S for example.

这是一个非常简单的问题,但是如果c++代码样式指南说“使用C作为类名前缀”,那么这是否意味着也要使用C作为结构前缀,或者我们是否应该使用一些不同的东西,比如S。

class CFoo { };
struct CBar { };

... or ...

…还是……

class CFoo { };
struct Bar { };

8 个解决方案

#1


13  

If the style guide doesn't specify, I would (probably) use the "structs are classes with all members public"-rule to use C for structs too, yes. Or I would think "hah, here's a loophope to get around that silly initial rule, yay" and not use it. In other words, this is highly subjective.

如果样式指南没有指定,我(很可能)会使用“struct are class with all members public”规则来使用C作为struct,是的。或者我会想“哈,这是一个规避愚蠢的初始规则的环希望,耶”而不使用它。换句话说,这是非常主观的。

#2


26  

Simple answer - don't use the C prefix for classes. This is hungarian notation of the most pointless sort. It's probably time to re-write the style guide. Frankly (and speaking as someone who's written several of the things), most such guides are rubbish and/or were written long, long ago and never updated.

简单的回答——不要对类使用C前缀。这是一种最没有意义的匈牙利符号。可能是时候重新编写样式指南了。坦率地说(作为写过其中一些东西的人来说),大多数这样的指南都是垃圾,或者是很久以前写的,从来没有更新过。

#3


5  

If the code style guide doesn't specify, find code that's been following the style guide and see what's already been done.

如果代码样式指南没有指定,请查找遵循样式指南的代码,看看已经完成了什么。

If there is no code already following the style guide, come to an agreement with everyone involved in the project.

如果没有遵循样式指南的代码,请与参与项目的每个人达成一致。

If nobody else is involved in the project, just decide and be consistent.

如果没有其他人参与这个项目,那就决定并保持一致。

#4


3  

I think this guideline is stupid and confusing.. The fact that you had to ask this question proves it.

我认为这条指导方针是愚蠢和混乱的。你不得不问这个问题的事实证明了这一点。

Coding styles are meant to increase readability; it's obvious if an identifier is a class or not, especially if you are using a decent IDE with mouseover tooltips.

编码风格旨在提高可读性;标识符是否为类是显而易见的,特别是如果您使用的是带有鼠标悬停工具提示的高级IDE。

#5


2  

We usually use C prefix for classes and T prefix for structs that have no methods (ie, "C" structs).

我们通常对类使用C前缀,对没有方法的结构使用T前缀(即“C”结构)。

#6


2  

For me it would come down to:

对我来说,它可以归结为:

Do you want the readers of your code to immediately differentiate between the two declaration types?

您希望代码的读者立即区分这两种声明类型吗?

While the use of the prefix is generally distasteful, carefully consider the view of the code maintainer. Is it helpful for them to think, "Ah! no C prefix, this is a struct". Using a struct instead of a class may imply something specific in your code. If it doesn't, it makes more sense to continue to use the prefix for the sake of the maintainer.

虽然前缀的使用通常令人反感,但是请仔细考虑代码维护人员的视图。对他们来说,“啊!没有C前缀,这是一个struct。使用结构体而不是类可能意味着代码中某些特定的内容。如果没有,那么为了维护人员,继续使用前缀更有意义。

#7


2  

If a style guide does not serve its purpose to promote easy readability, consistency, and correctness, it should be modified until it does so or thrown into the circular file (trash can).

如果一个样式指南没有达到提高易读性、一致性和正确性的目的,那么应该对它进行修改,直到它这样做或者扔进循环文件(垃圾箱)。

Also, if people don't follow it, then it should be updated so that it is easier to follow (or the tools amended to make coding to the guidline easier).

此外,如果人们不遵循它,那么应该对它进行更新,以便更容易地遵循它(或者修改工具以使对指导线的编码更容易)。

#8


0  

According to the existing pattern of prefixing C for classes, you should prefix S for struct, I for interface.

根据现有的类前缀C的模式,应该为struct加上前缀S,为接口加上I。

In addition, prefix E for enum, D for delegate, D for directory, F/M for function/method, F for file, F for field, N for namespace, P for page, P for parameter, P for property, R for return value, v for variable.

另外,enum用前缀E, delegate用D, directory用F/M, function/method用F/M, file用F,命名空间用F, page用P,参数用P,属性用P,返回值用R,变量用v。

As of variables, prefix

变量的前缀

  • a for array
  • 为数组
  • b for boolean
  • b为布尔
  • c for char
  • c字符
  • f for float
  • f为浮动
  • g for GUID
  • g GUID
  • h for handle
  • h的处理
  • i for int
  • 我的智力
  • j for json
  • j为json
  • k for key
  • k为关键
  • l for list
  • l为列表
  • m for MarshalByRefObject
  • 美元MarshalByRefObject
  • n for Nullable
  • n可以为空
  • o for object
  • o为对象
  • p for pointer
  • p为指针
  • q for queue
  • 问的队列
  • r for Registry
  • r为注册表
  • s for single
  • 年代的单
  • t for TimeZone
  • t的时区
  • u for Uri
  • u为Uri
  • v for version
  • v版本
  • w for WeakReference
  • w WeakReference引用
  • x for XmlDocument
  • x XmlDocument
  • y for yoda
  • y为尤达
  • z for ZipFile
  • z为ZipFile

Example

例子

Good:

好:

namespace NGqqnbig.NConsoleApplication1
{
    class CProgram
    {
        static void Main(string[] pasArgs)
        {
            FMain(pasArgs);
        }

        static void FMain(string[] pasArgs)
        {
            var vsLine= CConsole.FReadLine();
            var viSVN= CConvert.FToInt32(vsLine);

            var occCVC = new CCCCVC();
            occCVC.PICVC = viSVN;
        }
    }

    class CCCCVC
    {
        private int fiCVC;

        public int PICVC
        {
            get { return fiCVC; }
            set { fiCVC = pivalue; }
        }
    }
}

Bad:

缺点:

namespace Gqqnbig.ConsoleApplication1
{
    class CProgram
    {
        static void Main(string[] args)
        {
            var line= Console.ReadLine();
            var svn= Convert.ToInt32(line);

            var cardVerificationCode = new CreditCardVerificationCode();
            cardVerificationCode.VerificationCode = svn;
        }
    }

    class CreditCardVerificationCode
    {
        public int VerificationCode { get; set; }
    }
}

#1


13  

If the style guide doesn't specify, I would (probably) use the "structs are classes with all members public"-rule to use C for structs too, yes. Or I would think "hah, here's a loophope to get around that silly initial rule, yay" and not use it. In other words, this is highly subjective.

如果样式指南没有指定,我(很可能)会使用“struct are class with all members public”规则来使用C作为struct,是的。或者我会想“哈,这是一个规避愚蠢的初始规则的环希望,耶”而不使用它。换句话说,这是非常主观的。

#2


26  

Simple answer - don't use the C prefix for classes. This is hungarian notation of the most pointless sort. It's probably time to re-write the style guide. Frankly (and speaking as someone who's written several of the things), most such guides are rubbish and/or were written long, long ago and never updated.

简单的回答——不要对类使用C前缀。这是一种最没有意义的匈牙利符号。可能是时候重新编写样式指南了。坦率地说(作为写过其中一些东西的人来说),大多数这样的指南都是垃圾,或者是很久以前写的,从来没有更新过。

#3


5  

If the code style guide doesn't specify, find code that's been following the style guide and see what's already been done.

如果代码样式指南没有指定,请查找遵循样式指南的代码,看看已经完成了什么。

If there is no code already following the style guide, come to an agreement with everyone involved in the project.

如果没有遵循样式指南的代码,请与参与项目的每个人达成一致。

If nobody else is involved in the project, just decide and be consistent.

如果没有其他人参与这个项目,那就决定并保持一致。

#4


3  

I think this guideline is stupid and confusing.. The fact that you had to ask this question proves it.

我认为这条指导方针是愚蠢和混乱的。你不得不问这个问题的事实证明了这一点。

Coding styles are meant to increase readability; it's obvious if an identifier is a class or not, especially if you are using a decent IDE with mouseover tooltips.

编码风格旨在提高可读性;标识符是否为类是显而易见的,特别是如果您使用的是带有鼠标悬停工具提示的高级IDE。

#5


2  

We usually use C prefix for classes and T prefix for structs that have no methods (ie, "C" structs).

我们通常对类使用C前缀,对没有方法的结构使用T前缀(即“C”结构)。

#6


2  

For me it would come down to:

对我来说,它可以归结为:

Do you want the readers of your code to immediately differentiate between the two declaration types?

您希望代码的读者立即区分这两种声明类型吗?

While the use of the prefix is generally distasteful, carefully consider the view of the code maintainer. Is it helpful for them to think, "Ah! no C prefix, this is a struct". Using a struct instead of a class may imply something specific in your code. If it doesn't, it makes more sense to continue to use the prefix for the sake of the maintainer.

虽然前缀的使用通常令人反感,但是请仔细考虑代码维护人员的视图。对他们来说,“啊!没有C前缀,这是一个struct。使用结构体而不是类可能意味着代码中某些特定的内容。如果没有,那么为了维护人员,继续使用前缀更有意义。

#7


2  

If a style guide does not serve its purpose to promote easy readability, consistency, and correctness, it should be modified until it does so or thrown into the circular file (trash can).

如果一个样式指南没有达到提高易读性、一致性和正确性的目的,那么应该对它进行修改,直到它这样做或者扔进循环文件(垃圾箱)。

Also, if people don't follow it, then it should be updated so that it is easier to follow (or the tools amended to make coding to the guidline easier).

此外,如果人们不遵循它,那么应该对它进行更新,以便更容易地遵循它(或者修改工具以使对指导线的编码更容易)。

#8


0  

According to the existing pattern of prefixing C for classes, you should prefix S for struct, I for interface.

根据现有的类前缀C的模式,应该为struct加上前缀S,为接口加上I。

In addition, prefix E for enum, D for delegate, D for directory, F/M for function/method, F for file, F for field, N for namespace, P for page, P for parameter, P for property, R for return value, v for variable.

另外,enum用前缀E, delegate用D, directory用F/M, function/method用F/M, file用F,命名空间用F, page用P,参数用P,属性用P,返回值用R,变量用v。

As of variables, prefix

变量的前缀

  • a for array
  • 为数组
  • b for boolean
  • b为布尔
  • c for char
  • c字符
  • f for float
  • f为浮动
  • g for GUID
  • g GUID
  • h for handle
  • h的处理
  • i for int
  • 我的智力
  • j for json
  • j为json
  • k for key
  • k为关键
  • l for list
  • l为列表
  • m for MarshalByRefObject
  • 美元MarshalByRefObject
  • n for Nullable
  • n可以为空
  • o for object
  • o为对象
  • p for pointer
  • p为指针
  • q for queue
  • 问的队列
  • r for Registry
  • r为注册表
  • s for single
  • 年代的单
  • t for TimeZone
  • t的时区
  • u for Uri
  • u为Uri
  • v for version
  • v版本
  • w for WeakReference
  • w WeakReference引用
  • x for XmlDocument
  • x XmlDocument
  • y for yoda
  • y为尤达
  • z for ZipFile
  • z为ZipFile

Example

例子

Good:

好:

namespace NGqqnbig.NConsoleApplication1
{
    class CProgram
    {
        static void Main(string[] pasArgs)
        {
            FMain(pasArgs);
        }

        static void FMain(string[] pasArgs)
        {
            var vsLine= CConsole.FReadLine();
            var viSVN= CConvert.FToInt32(vsLine);

            var occCVC = new CCCCVC();
            occCVC.PICVC = viSVN;
        }
    }

    class CCCCVC
    {
        private int fiCVC;

        public int PICVC
        {
            get { return fiCVC; }
            set { fiCVC = pivalue; }
        }
    }
}

Bad:

缺点:

namespace Gqqnbig.ConsoleApplication1
{
    class CProgram
    {
        static void Main(string[] args)
        {
            var line= Console.ReadLine();
            var svn= Convert.ToInt32(line);

            var cardVerificationCode = new CreditCardVerificationCode();
            cardVerificationCode.VerificationCode = svn;
        }
    }

    class CreditCardVerificationCode
    {
        public int VerificationCode { get; set; }
    }
}