Naming Conventions that indicate Hierarchy

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Naming Conventions that indicate Hierarchy

Post by Ambush Commander »

There are several rules I follow.

Classes are Capitilized, use camelcase, and have underscores to namespace them. Ex) TWP_Mapper_User.
Methods and properties are not capitilized and camel capped: getID()

I also believe that the camel case has higher precedence over the underscore. Do_SubmitPage has a logical division between Do and SubmitPage (which can be further divided into Submit Page).

Now, I wonder what I should do if a method references an object or something with underscores. If you where in this situation, which of the following would you do?

Code: Select all

function getMapperUser();
function getMapper_User();
function get_Mapper_User();
function get_MapperUser();
Or any permutations thereof. Or maybe I should give up trying to indicate hierarchy in method names?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Seems to defeat (in part) coding to some standard interface. If I have a getMapperUser() method which does basically the same task across Classes - I'll just leave it as is. I must have 10 classes in some apps which all use the same method name getByID() simply because its predictable naming that describes a standard task - grabbing something based on an ID.

IMO method names should be short and simple using full words rather than abbreviations (unless something obvious like ID/URI et al). With as few odd conventions (underscoring) as possible unless that clearly offer greater clarity (usually in long method names where needed).
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

I personally like it descriptive, all lowercase, and underscores. it makes it much easier later one when I go back. For classes i put class_ in front of it. For objects I put obj_ in front of it. For acccessory methods I put get_ or set_ in front. For functional methods I just put something descriptive and only use abbreviations if it is obvious.

In VB using camel case was nice because when you type it in all lowercase it would uppercase the correct letters automatically, if it did not then you knew you had a type or the method did not exist.

Everyone has their own way, as long as it is clear and consistent it is better then no way at all.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

No underscores in methods then. Didn't really answer my question, but that's okay.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Naming is worth spending time on.

Maybe it's simply due to my inexperience but I've never had any real namespace problems. I'd much rather choose a new name than something like TWP_Mapper_User. At first glance, it's not clear what TWP means. Also, "UserMapper" reads better to me. As far as possible, I want the code to read like pure English. In the best case, with good names, it should be possible even for a non programmer to have a rough idea of what's going on.

For the mehtod name, my vote would be for "getUserMapper". If there is only one mapper to be got, I'd simplify that to "getMapper".
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Naming Conventions that indicate Hierarchy

Post by Christopher »

For class methods I think you first example getMapperUser() is what I see in almost all the PHP code I see around the web (or just getMapper() ). That's pretty standard -- as are all lowercase function names with underscores (e.g. user_get_mapper() or str_replace()) where the first word is the library name. That's the standard the PHP Group keeps moving toward.

For class names it seems to break into two groups: discriptive or namespaced. PEAR uses the latter where TWP_Mapper_User in in the file path TWP/Mapper/User.php. Mostly you will just see that class named UserMapper.
Post Reply