7

When I'm using pointers in my code, I don't always remember whether I'm dealing with pointers or the objects they point to. So, I want to call my pointers 'pointerToXYZ'. Do any of the official style guides (Links to official style guides) describe pointer naming conventions?

UPDATE: Ok, I've made this a more concrete question; hopefully it can get reopened. If the answer is a simple "no" then I'll take it.

Community
  • 1
  • 1
tir38
  • 9,810
  • 10
  • 64
  • 107
  • 3
    Maybe it's time to get yourself more familiar with your IDE :) And if there's "a lot of code" between two uses of a variable, then maybe your function are too big. – jrok Apr 26 '13 at 19:47
  • I'm pretty comfortable with it (QT) and this has been my saving grace. I just would like my code to read better. – tir38 Apr 26 '13 at 19:49
  • 4
    I find this kind of coding convention annoying clutter. Keep your scopes small, and let the compiler and/or your IDE tell you when something is wrong. – juanchopanza Apr 26 '13 at 19:55
  • A lot of people use names like pnode or nodep. Spelling it out is extremely amateurish and you'll eventually regret it. P.S. QT is not an IDE – Jim Balter Apr 29 '13 at 08:12
  • @JimBalter I meant QT Creator – tir38 Apr 29 '13 at 17:23

3 Answers3

6

As for: "this is silly" it's pretty much upto you. In teams (/projects) there will likely be naming conventions which everyone agrees to. If you find this format a good naming convention for your own code then there's nobody stopping you.

Naming conventions for pointers that are commonly used aren't to common I think. One that I do see most often (as in consistent) is:

int *somenamePtr;
Floris Velleman
  • 4,848
  • 4
  • 29
  • 46
6

Use your own style of coding, that you are comfortable with , and also keep in mind , your code must be equally comfortable for others to read as well, as a good practice just go through and try to implement well accepted coding standards like the cert secure coding, and a general coding style like the google c++ style , or say the one used in linux(only C style shown not cpp).

int a;
int* ptr_a;

//code here

int c = *ptr_a;

In your above program , try to name your variables, better than a,b,c etc. And yes(in my humble opinion), your approach towards indicating pointer types will suffice.Like the other person said consistency is important in your code, and is always the key to maintain code in the long run.

gmagno
  • 1,770
  • 1
  • 19
  • 40
Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
  • The Google C++ Style is NOT good style for modern C++. You shouldn't recommend anybody to use it. – Rayniery Apr 26 '13 at 20:07
  • why do you say so ? Any reasons ?? – Barath Ravikumar Apr 26 '13 at 20:11
  • 1
    The Google C++ style is a style for writing code for open-source Google projects; they are not a general c++ style. If you read it, you will notice it. For example, that guide doesn't allow the use of exceptions to report errors, instead it recommends the use of error code, but that is considered a bad practice by the C++ community and the standard committee. – Rayniery Apr 26 '13 at 20:24
1

Do whatever you feel comfortable with, but be CONSISTENT.

There are several coding guidelines for C and C++ that you could use as an example for making your own personal coding standard (see here and here).

And using prefixes or postfixes is not uncommon in variable names, mostly in C (in C++ they are seen less favorably).

gmagno
  • 1,770
  • 1
  • 19
  • 40
Rayniery
  • 1,557
  • 2
  • 12
  • 17