2.6. Establishing a Naming Convention for Your Variables
Problem
You need to follow a dependable pattern for naming variables in your web site scripts.
Solution
Develop coding guidelines that make sense to you, your team, and your web site, and then stick to them.
In general, a good variable naming scheme:
Uses unique, concise terms
Limits abbreviations
Avoids reserved words in its programming language
Serves as a form of self-documentation
Discussion
Variables are used to store alphanumeric values that will be manipulated by a script or program. Variables can have a constant value defined permanently in the code, or be changed from one value to another through logic or input from a user.
For example, a PHP script might take a Fahrenheit temperature
value entered on a web site form (call it $temp
), perform a calculation on the
variable to convert it to Celsius, and then echo the same variable
(with a new value) back to the user.
The variable name $temp
has
several disadvantages (that's why I chose it). As a generic
abbreviation for temperature, $temp
easily could be misconstrued as representing a temporary value (or the
user's temperament) by another programmer reviewing the code.
Tip
If you find that you have to use abbreviations or shorthand for your variables, compile a glossary and add it to a central comment block in your script.
While the name $temp
meets the simple needs for a hypothetical temperature converter, it becomes woefully inadequate as soon as you try to add other functionality ...
Get Web Site Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.