Page 1 of 1

Naming Conventions that indicate Hierarchy

Posted: Wed Oct 19, 2005 6:20 pm
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?

Posted: Thu Oct 20, 2005 3:11 am
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).

Posted: Thu Oct 20, 2005 10:18 am
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.

Posted: Thu Oct 20, 2005 3:38 pm
by Ambush Commander
No underscores in methods then. Didn't really answer my question, but that's okay.

Posted: Thu Oct 20, 2005 8:08 pm
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".

Re: Naming Conventions that indicate Hierarchy

Posted: Fri Oct 21, 2005 1:29 am
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.