The importance for following best practices in your code not just increases your code readability but will help you to create quality software as well.Back then i always used to think what would be the best practices for naming our functions, classes and all. After reading many articles and codes, this is how i started to do.
Mostly used naming conventions are
- underscore
- camel case
- pascal case
let’s dig a little deeper to the above list of commonly used naming conventions.
underscore : Many developers uses this naming convention for variables and sometimes functions as well. Examples : $first_name, $last_name, is_admin() etc. Bonus tips : variables should start with a small letter.
camel case : This convention is mostly used in methods.Methods start with a small letter and follow camel case approach.Examples : getUsername(), getUserRoles() etc.
pascal case : Pascal case are used in Class. The first letter of every word in a class name would be capital. Examples : User, AccountHistory etc.
Some other tips :
Constants are separated using underscore and they must be uppercase. Examples : GREETING, ERROR_WARNING etc.
Interface are same as the class except it is an adjective instead of the noun. Example : BarInterface ,PaintInterface etc.
Comparison with some of the php frameworks and CMS
| Php | Classes | Methods | Functions | Variables |
|
CodeIgniter Framework
|
Proper_Case | lower_case | lower_case | lower_case |
| WordPress cms | lower_case | lower_case | ||
| Laravel Framework | PascalCase | camelCase | * | * |
* can be referred as not having a specific rules. Though I would suggest camelCase for functions and lower_case for variables.
At the end , we just want to say there is no one universal way. These above ways of naming conventions are just the preferred way by many developers including us.What about you ? How do you name your functions and classes? You can drop in comment section. 🙂 Happy coding.