C++ vs. Objective-C II : Why you don’t name a variable for an object with a prefix “p” in Obj-C

A while ago, I noticed that I didn’t write any Obj-C variables pointing to objects prefixed with p.
What I mean is :

NSString *pFirstName;

In C++, it is better to prefix any variable pointing to pointers. Or if you are from PASCAL world, you would prepend with Ptr like “firstNamePtr”.

Without recognizing what I have done, I just didn’t prefix a variable pointing to an object with p. Moreover whenever I see others’ Objective-C in many open source projects, I don’t see that pattern either. What’s different between me as Obj-C programmer and me as C++ programmer?

It turned out that it was due to difference of the two languages.
In Objective-C, you have never seen an object is created in a stack memory like this.

NSString firstName;

Have you ever tried it?
Let’s see what gcc will say.

So, in Objective-C, all objects are pointed by pointer variables.

Additionally, there is another different coding convention.
With C++, you usually name a class prefixed with “C”. I think it is MS coding convention. Whatever it is, I think it is very nice. So, when I write codes on Windows, I also use the prefix “C” for any classes I declare and define.

class CFileBrowser : public ...



However, with Objective-C, i don’t do that. all Objective-C class named as noun.
So, if you read their source codes, C++ codes feels like “codes” while Obj-C codes feels more like natural statement like novel or whatever they are not “program codes”.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: