Home arrow Practices arrow Page 4 - Writing Quality Software: A Primer

Naming Conventions - Practices

development prracticesOkay, so you have a respectable handle on syntax, and you are proud of yourself for getting that far. And, truth be told, you should be. But, knowing syntax is not the same as mastering syntax in such a way that builds quick, efficient web software. This article takes a look at not just writing software, but writing quality software.

  1. Writing Quality Software: A Primer
  2. Some General Quality Issues
  3. Consistent and Readable Code Format
  4. Naming Conventions
  5. Appropriately Commenting Your Code
  6. Unit Testing
  7. System Testing
By: Steve Adcock
Rating: starstarstarstarstar / 24
December 15, 2003

print this article


An issue that many developers neglect is naming conventions. Some hasty programmers use the first word that comes to mind when naming variables, which hurts the intuitiveness of the application. Variable names need to accurately reflect the variable's purpose in life.

Let's take a look at some good and bad naming conventions, using PHP.

$A = 15 // Bad name 
$Age = 15 // Good name 
$P = 4 // Bad name 
$Perimeter = 4 // Good name 
$Date = "01/14/04" // Potentially good name 
$RetireDate = "01/14/04" // Better name 

Always use the shortest variable name that you can, while still maintaining the meaning of what the variable holds. Let's again look at a few examples.

$CircumferenceOfTheCircle = 4 // Name is too long 
$CircumCircle = 4 // Better name 
$MaximumPointsPossible = 100 // Name is too long 
$MaxPoints = 100 // Better name

Both $CircumCircle and $MaxPoints satisfy both of our examined criteria, (1) the variable name implies the value, and (2) the variable names are both concise. Try to avoid using words like 'the', and 'a' and other filler words within variable names, along with 'ing', 'ed', 'ies' etc. They violate both our criteria.

Also notice my use of capitalizations. It is often easier to read variables when using initial caps for each word. So, instead of $Maxpoints, I wrote $MaxPoints. Some programmers only capitalize the first letters of words other than the first, for example, $maxPoints. Either method works fine, although I prefer to initial cap each word, including the first.

Another method to naming your variables includes a reference to the datatype in the variable name. Although this does lengthen our variable, it does add understanding to what the variable should hold. Using this method, instead of declaring our "First name" variable $FirstName, we could declare it as $strFirstName. And, instead of declaring our Boolean variable "Success" $Success, use $bolSuccess.

How do we tell the difference between local and global variables? Unless they are declared within functions themselves, it may not be initially obvious. Consider tacking on g_ for global variables and f_ for function variables (or m_ for module variables). So, our global variable called $Multiplier could be renamed to read $g_Multiplier. This way, when other programmers read your code, or even when you read your code months from writing it, it is clearly understandable that g_Multiplier is a variable used globally, and you will look for such usage.

To wrap up, here's what you should have learned:
  • Choose specific, descriptive variable names
  • Keep variable names short
  • Avoid filler words, and be consistent with abbreviations
  • Consider including variable datatypes in names (strName)
  • With global variables, tack on g_, or function vars with f_

    >>> More Practices Articles          >>> More By Steve Adcock

    blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Calculating Development Project Costs
- More Techniques for Finding Things
- Finding Things
- Finishing the System`s Outlines
- The System in So Many Words
- Basic Data Types and Calculations
- What`s the Address? Pointers
- Design with ArgoUML
- Pragmatic Guidelines: Diagrams That Work
- Five-Step UML: OOAD for Short Attention Span...
- Five-Step UML: OOAD for Short Attention Span...
- Introducing UML: Object-Oriented Analysis an...
- Class and Object Diagrams
- Class Relationships
- Classes

Developer Shed Affiliates


Dev Shed Tutorial Topics: