开发者

If we use the C prefix for classes, should we use it for struct also?

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...

It's a pretty trivial questi开发者_StackOverflow社区on, 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.

class CFoo { };
struct CBar { };

... or ...

class CFoo { };
struct Bar { };


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.


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.


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.


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.


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


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.


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).


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

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.

As of variables, prefix

  • a for array
  • b for boolean
  • c for char
  • f for float
  • g for GUID
  • h for handle
  • i for int
  • j for json
  • k for key
  • l for list
  • m for MarshalByRefObject
  • n for Nullable
  • o for object
  • p for pointer
  • q for queue
  • r for Registry
  • s for single
  • t for TimeZone
  • u for Uri
  • v for version
  • w for WeakReference
  • x for XmlDocument
  • y for yoda
  • z for 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; }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜