12

In this answer of mine, I used _t at end of types, which ended at best, controversial. This is the practice I use in my current projects.

typedef struct {
    int x;
    int y;
} point_t;

This was intended to be consistent with C types from stddef.h like size_t or ptrdiff_t. However, according to certain comments in my questions, _t postfix appears to be reserved for POSIX. What should I use instead of _t? Or should I continue using _t anyway?

This question is for both C and C++. If these languages have different conventions, feel free to answer so.

migrated from codereview.stackexchange.com Jan 7 '14 at 8:39

This question came from our site for peer programmer code reviews.

  • 2
    One common convention is to have class names that LookLikeThis. At that point you don't even need suffixes. (This is more common for Java, .net etc. But it works in C++ as well.) – cHao Jan 7 '14 at 7:30
  • 1
    ultimately this is a form of Hungarian Notation. And that discussion is endless. I for one am against it now and go with cHao's solution. Why? Because I used Hungarian Notation for about 4 years for everything, then decided to do a project without it and didn't miss it at all. And didn't waste time on it. So I dropped it completely since it had, apparently, no value at all for me. And I never looked back. – stijn Jan 7 '14 at 15:02
  • 1
    See this question for reserved identifiers (including _t) stackoverflow.com/a/228797/14065 – Martin York Jan 8 '14 at 20:27
16

There is no consensus within the C and C++ communities with regard to naming types.

Although the _t suffix is indeed reserved by POSIX, some will advocate using the _t suffix for your own types as well, while others will equally adamantly argue that you should not tempt fate by risking duplicate names with POSIX.

Alternative naming conventions suggest the use of a prefix for types, such as C or T.

16

I find suffixes rather ugly and so avoid _t etc. A good alternative is to use a capital letter to start type names, so Point in your example above.

  • 3
    and I find it nice to have a style DIFFERENT from the standard C types, so reading the code you can easily find what is from the client program and what is the API – Nikko Jan 7 '14 at 15:50
  • 1
    Also my favorite technique. Types have an initial uppercase letter. All other identifers are not types and thus easily seen. – Martin York Jan 8 '14 at 20:29
  • I always thought it looked quite ugly as well. Good to see that other's agree with me. – syb0rg Feb 1 '14 at 3:40
  • I only use PascalCase for pointer types (Because of languages like C# and Java where PascalCase means class, i.e. reference to data) but I get where you're coming from. – YoYoYonnY Feb 22 '16 at 12:50
1

It depends on you. Unless there is a conflict with the reserved words you are good to go. However, consider a scenario where you are working in a company and multiple people are accessing the same code base, and your code gets to a person who is not aware of your habit He/She can then assume that its a reserved type, especially if you write something like point_t. So, keep it simpler e.g. point is simpler than point_t. And if you want to append something to it, append you name initials.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.