Object-Oriented Programming

Method Naming Standards

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Naming Standards

  • There are a large number of naming practices
  • On the Naming of Methods: A Survey of Professional Developers
  • Survey of 1100+ developers
    • Wide support for these standards
    • Across multiple programming languages
    • Very slight differences based on experience
  • Paper is referring to methods, but also applies to free functions

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

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

Verb Phrase

manage_caching_sizes computeProductBlockingSizes get_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 make an otherwise high-quality identifier hard to understand

Full Words

startConnection c /* for the same function */

  • 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_treerepr 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 they become markers of older code. Names should not contain scope or type information. Martin

Length

returnfalseifnosetterwasfoundandifreportnosetterfoundisfalse

  • Long debate in the research literature and developer discussion Cunningham
  • Longer, more descriptive 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 almost all cases
  • One exception: Name for test-case methods

Summary

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