Skip to main content

Naming

Naming a variable or module or function is the hardest job, especially when you realize how incredibly important it is :satisfied:

TL;DR

  • Use descriptive names.
  • Use adjectives to clarify.
  • Be specific about actions.
  • Describe object in class names.
  • Avoid slang/abbreviations.
  • Use meaningful distinctions.
  • Choose pronounceable and searchable names.
  • Use consistent terminology.

How to name things correctly?

Variables & Constants

GuidelineBadGood
For value as objectx, data, lst, cfguser, database_element, order_list, config_settings
For value as number or stringn, s, ms, mtmax_speed, min_temperature, name, age
For value as booleanstatus, perm, authhas_permission, is_authenticated, is_active_user
Add adjectives to add details without introducing redundancycount, order_countactive_user_count, completed_order_count

Functions/Methods

GuidelineBadGood
Functions that perform operationsdo_send(), config_load()send_response(), load_configuration()
Functions that return boolean valuescheck_user(), license_check()is_user_valid(), has_valid_license()
Add adjectives to add details without introducing redundancyfetch_users(), generate_report()fetch_active_users(), generate_monthly_report()

Classes

GuidelineBadGood
Describe the object and provide details avoiding redundancyManagerClass, ProcessorClassTransactionManager, PaymentProcessor

Generic

GuidelineBadGood
Avoid slang and unclear abbreviationscalcStats(), initDB()initialize_database(), calculate_statistics()
Make meaningful distinctionsdate1, date2start_date, end_date
Use pronounceable namesttlAmttotal_amount
Use searchable namesmaxRetmaximum_retries
Avoid mental mappingsi, j, kindex, count
Don’t be overly clearcalculate_the_discount_amount()calculate_discount()
Pick one word per conceptget_data(), retrieve_data()fetch_data()