Principles of class naming
Moderator: General Moderators
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Principles of class naming
I'm writing a huge project that involves lots of classes. I'm already using classes like zip, security, http, service, etc so I am wondering how I should name these classes. (While it's still an easy job.)
For example, I won't call my zip class a "zip" since it would probably get intefered by PHP in the future. What about other names such as "security","http","service","string",etc? How do you name your classes, do you use a prefix, is using a prefix way to go?
For example, I won't call my zip class a "zip" since it would probably get intefered by PHP in the future. What about other names such as "security","http","service","string",etc? How do you name your classes, do you use a prefix, is using a prefix way to go?
Re: Principles of class naming
I try to name my classes as close (and descriptive) to what I'm doing as possible: user_auth (User authentication class) is pretty self explanatory for example, instead of maybe name it authenticate and confuse myself later on when i want to make authentication for something else. Sometimes I use prefix, MySQL_connect for example.
Would be interesting to see how other people name their classes though.
Would be interesting to see how other people name their classes though.
Re: Principles of class naming
Use your project name as the prefix, that should prevent future collisions
Re: Principles of class naming
What if you have 2 zip classes that does different things?pytrin wrote:Use your project name as the prefix, that should prevent future collisions
Re: Principles of class naming
They should have different names according to what they do - and probably extend the same zip base class
Code: Select all
class Project_Zip {
...
}
class Project_Zip_Archive extends Project_Zip {
...
}
class Project_Zip_Memory extends Project_Zip {
...
}- VirtuosiMedia
- Forum Contributor
- Posts: 133
- Joined: Thu Jun 12, 2008 6:16 pm
Re: Principles of class naming
I've more or less modeled my classes after the Zend naming schema.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Principles of class naming
That's exactly what I'm doing. I'm separating payment gateways and using a base abstract class called "payment".pytrin wrote:They should have different names according to what they do - and probably extend the same zip base class
Code: Select all
class Project_Zip { ... } class Project_Zip_Archive extends Project_Zip { ... } class Project_Zip_Memory extends Project_Zip { ... }
So do you think I should go for a prefix?
Code: Select all
class ph_http {}
abstract class ph_security {}
abstract class ph_payment {}Re: Principles of class naming
Yep. It's the common namespacing tactic without actual namespaces (coming in PHP 5.3)
Re: Principles of class naming
Group classes together into packages, classes should be loosely coupled yet highly cohesive. Naming is everything. Often a trivial name changes causes me to have a series of breakthroughs in looking at the design in a different light. Naming is actually a really involved process so maybe if you provided some context I could give a more descriptive answer.
For instance imagine you were just dropped into a 3rd world country and you saw a rabbit run by, one of the natives yells out "gordiva", does gordiva=rabbit? furry animal? scurring creature? does it refer to the ground the rabbit is running on, the air that was displaced by the rabbit moving in space? does it refer to the plants next to the rabbit?*
As you can see language is actually exceedingly complex. Names should be entirely unambiguous and clear. You should be able to explain your design to non-programmers...
- Josh
*example shamelessly stolen from Steven Pinker
For instance imagine you were just dropped into a 3rd world country and you saw a rabbit run by, one of the natives yells out "gordiva", does gordiva=rabbit? furry animal? scurring creature? does it refer to the ground the rabbit is running on, the air that was displaced by the rabbit moving in space? does it refer to the plants next to the rabbit?*
As you can see language is actually exceedingly complex. Names should be entirely unambiguous and clear. You should be able to explain your design to non-programmers...
- Josh
*example shamelessly stolen from Steven Pinker
Re: Principles of class naming
Accurate and descriptive naming. Name collisions? Don't care. Use namespaces if collisions bother you.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Principles of class naming
I don't want to require PHP 5.3 just yetJenk wrote:Accurate and descriptive naming. Name collisions? Don't care. Use namespaces if collisions bother you.
@Josh: Right now I'm using short and describtive names for my classes and they use the same one prefix. I do have an online documentation explaining the purpose of each classes. I hope that makes sense.
Re: Principles of class naming
That's actually really excellent. If you want to get better I'd learn to divide stuff into packages / control dependencies, etc... Domain Driven Design is a good book which focuses a lot on the skill of naming things. Also OO models the way our mind categorizes things, if you're really into it like me you'll be very interested in learning about linguistics and nuero science.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Principles of class naming
Thanks, are you talking about this book http://books.google.fi/books?id=7dlaMs0SECsC ?jshpro2 wrote:That's actually really excellent. If you want to get better I'd learn to divide stuff into packages / control dependencies, etc... Domain Driven Design is a good book which focuses a lot on the skill of naming things. Also OO models the way our mind categorizes things, if you're really into it like me you'll be very interested in learning about linguistics and nuero science.
I'm not sure if I really want to look at linguistics or nuero science, but I'll jot them down for the future just in case.
Re: Principles of class naming
Yes that book. I actually found out about the Steven Pinker books because Eric Evans cites them in his bibliography, if you read DDD you'll get the general idea.. just if you're a nut like me you trace stuff back to it's source to try and get the full understanding