Object-Oriented Programming

Method Naming Standards

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Naming Standards

List of Naming Standards

  • Naming Style
  • Grammatical Structure
  • Verb Phrase
  • Dictionary Terms
  • Full Words
  • Idioms and Slang
  • Abbreviations & Acronyms
  • Prefix/Suffix
  • Length

Naming Style

getFullName

getScriptState

call_with_default

garbage_collection

check_static_allocation_size

getfullName

getscriptstate

  • Popular styles: camelCase and under_score
  • Little difference in cognitive load during comprehension
  • Pretty equivalent, except that camelCase appears to have a slight advantage

Grammatical Structure

registerManagedResource

managedResourceRegister

// verb phrase
drawContentBorder

// verb phrase with a prepositional phrase
performTestsFromZipFile

// noun phrase
nextArea

  • Function names with multiple words need to have a grammatically correct sentence structure
  • verb phrase
  • verb phrase with a prepositional phrase
  • noun phrase

Verb Phrase

manage_caching_sizes

computeProductBlockingSizes

get_cached_node

x_cached_node

  • Not only grammatically correct but contain a verb or a verb phrase
  • Functions are actions

Dictionary Terms

findLength

abcdefg

cccc

aa2020

  • Words used in the function name are actual dictionary terms
  • Meaningful natural-language terms
  • Not non-dictionary word, e.g., "2" instead of "to"
  • Use of non-dictionary words makes an otherwise high-quality identifier hard to understand

Full Words

startConnection

/* for the same function as above */
c

  • Use full words as opposed to single-letter identifier names
  • Research clearly shows improved comprehension with full-word identifiers instead of single-letter names

Idioms and Slang

// personal name
fido

// idiom
cutting_corners

// slang
CurveBall

  • The name should not contain personal expressions, idioms, or slang
  • Special case of Dictionary Terms and Full Word standards
  • Using slang and idioms referred to as a cute practice
  • Choose clarity over entertainment value

Abbreviations & Acronyms

getStr pyConnection

get_algo close_db_connection

open_GUI_interface get_URL get_FIFO traverse_DOM_tree

repr getProtoNameNode

get_QWE sendAAAA

  • If a name contains words that are abbreviations or acronyms, they should be well-known or standard
  • Standard ones used by the organization or domain
  • evil abbreviations - unfamiliar abbreviations that are very hard for programmers to understand

Prefix/Suffix

gimpItemGetPath

swift_stdlib_u_char

m_count

  • Method names should not contain a prefix or suffix that is a term from the system
  • Does not apply to the C language or groups of related free functions
  • Developers quickly learn to ignore the prefix/suffix, and their existence becomes a marker of older code. Names should not contain scope or type information. Martin

Length

returnfalseifnosetterwasfoundandifreportnosetterfoundisfalse

Length

  • Long debate in the research literature and developer discussion Cunningham
  • Longer, more descriptive names have a positive impact on code comprehension Knuth
  • Fixed maximum number of words in the name from 1 to 5.
  • Some disagreement in the results, as some developers stated that there should be no maximum length in all cases. However, almost all agreed to around 5 in nearly all cases.
  • One exception: Names of unit-test methods

Summary

  • Presented standards are widely accepted in the developer community
  • Quality of identifiers has a substantial impact on code comprehension
  • Although the survey was for methods, almost all the standards apply to free functions
  • Will follow these standards in this course