Naming Conventions
- 18 February 2014
What are Naming Conventions?
Naming conventions refer to the pattern of naming things such as identifiers, files, variables, table name, field names and other entities. For instance if you had a directory structure containing several files you could implement a convention stating that all folders may only contain lower case alpha characters a-z and hyphens whilst files may contain alpha numeric characters a-z, 0-9 and hyphens e.g :
- /htdocs/
- /css/
- header-v1.css
- grid-system.css
- /images/
- main-bg.gif
- /uploaded-files
- /css/
Naming conventions can often used to denote things such as representing the type of file in a certain application or the database a table belongs to. You could add the prefix 'class.' to a PHP file to denote that the file contains a php Class as opposed to a procedural script. You could then say the name after the prefix also represents the name of the PHP class, yet all files are Camel Case whilst PHP class names are Pascal Case, so a file class.userAddress.php relates to the PHP class name UserAddress like so
- class.userAddress.php
<?php
class UserAddress {
}
?>
You could also state that all PHP class files are saved in a directory called classes. Depending on the nature of your application you may different type of classes or type or PHP file such as Abstract classes or interfaces. You may want to add a prefix to these or to help a user indentify what type of PHP file they are for instance you may prefix Interfaces with an 'interface.' and Abstract classes with 'abstract_class.' and store them in a structure like so:
- /htdocs/
- /application/
- /core/
- /classes/
- /abstract/
- abstract_class.breadcrumbs.php
- abstract_class.pagination.php
- class.breadcrumbsProduct.php
- class.breadcrumbsCategory.php
- class.paginationCategory.php
- /abstract/
- /interfaces/
- interface.observer.php
- /classes/
- /core/
- /application/
Why Bother?
Now you have an idea what naming conventions are you may be asking why bother? Well there are many benefits in implementing naming conventions and following them especially if you are working with a team of other developers as it helps to ensure you are all reading of the same hymn sheet.
Coding Standards
A coding standards policy helps ensure that devlopers produce code to a high standard, that contains fewer bugs, is more secure and means developers can easily read each other developers code and makes maintaining huge/comples web applications more simpler. Naming conventions form an integral part of any coding standards policy. In a development environment when you are not the only the only developer working on a project and especially when you are working in a large team of devleopers it is essential.
If developing in a PHP environment then it is worth reading the PSR standards available by the PHP-FIG team :
- PSR-0 - standard file, class and namespace convention
- PSR-1 - interoperability between shared PHP code
- PSR-2 - coding Style Guide for projects looking to standardise their code