Previous Page
Next Page

Chapter 3. Naming Conventions

Names are but noise and smoke,
Obscuring heavenly light
Johann Wolfgang von Goethe
Faust: Part I

Consistent and coherent code layout is vital, because it determines what the reader of your code sees. But naming conventions are even more important, because they determine how the reader thinks about your program.

Well-chosen identifier names convey to the reader the meaning of the data in variables, the behaviour and results of subroutines, and the features and purpose of classes and other data types. They can help to make the data structures and algorithms used in a program explicit and unambiguous. They can also function as a reliable form of documentation, and as a powerful debugging aid.

Best practice in naming consists of finding a consistent way of assigning identifiers to variables, subroutines, and types. There are two principal components of this method: syntactic consistency and semantic consistency.

Syntactic consistency means that all identifiers should conform to a predictable and recognizable grammatical structure. That is, you should not name one variable $max_velocity and then name another $displacementMax, or $mxdsp, or $Xmaximal. In other words, if one variable name has an adjective_noun structure, all variable names should be adjective_noun. Similarly, if one variable uses underscores to separate components of the name, then others shouldn't omit similar separators elsewhere, or use interCapStyle instead. Your approach to abbreviationboth what to abbreviate and how to abbreviate ithas to be consistent as well.

Semantic consistency means that the names you choose should clearly and accurately reflect the purpose, usage, and significance of whatever you're naming. In other words, a name like @data is a poor choice (compared to, say, @sales_records) because it fails to tell the reader anything important about the contents of the array or their significance in your program. Likewise, naming an indexing variable $i or $n doesn't serve to make the meaning of $sales_records[$i] any clearer, especially when compared to something like $sales_records[$largest_sale_today] or $sales_records[$cancelled_transaction_number].

This chapter explores all these issues, offering a consistent and easy-to-use approach to generating names for the various types of nameable referents in Perl.

    Previous Page
    Next Page