Naming Conventions in Programming: Variables, Functions and Classes

naming conventions in programming

Stuck on what to call a variable? Naming conventions in programming are shared rules for casing and word choice. Follow your language’s standard, pick descriptive names and stay consistent, and you’ll write code others can read.

Names are one piece of the wider set of software development best practices that keep a codebase readable.

Item Convention
Python classes PascalCase
Python functions, methods and variables Lowercase snake_case
Python constants UPPER_SNAKE_CASE
Python modules Short, all lowercase names
JavaScript and TypeScript variables and functions camelCase

These conventions come from Beyond the Basic Stuff with Python and Khalil Stemmler’s naming conventions guide. Style guides get revised and teams override them, so check the source before you rely on them.

Why naming conventions matter

The old joke says the hardest problems in computer science are naming things, cache invalidation and off-by-one errors. It is attributed to Leon Bambrick and based on a quote by Phil Karlton, and it holds a kernel of truth: coming up with good names, formally called identifiers, takes real effort. You can read the wording in this chapter on choosing understandable names.

A name is a message to the next reader, and that reader is often you a few weeks from now. Short names often make sense when you first write them, but their precise meaning can be lost a few days or weeks later.

A convention gives you two things, according to Khalil Stemmler’s guide: discoverability, meaning how quickly someone finds the class or method they need to change, and understanding, meaning how fast they grasp what they are looking at. Both save time in every later change.

The three casing styles you will meet most

Identifiers usually cannot contain spaces, so programmers join multiple words using a casing style. Three styles cover most code you will read:

  • camelCase: the first word is lowercase and each later word starts with a capital, as in someVariable.
  • snake_case: words are lowercase and joined by underscores, as in some_variable.
  • PascalCase: like camelCase, but the first word is capitalized too, as in SomeVariable.

Constants usually get a loud variant, UPPER_SNAKE_CASE, where every letter is capitalized and words are separated by underscores. Which of snake_case and camelCase you pick matters less than sticking with it: either is fine as long as your project consistently uses one or the other, not both.

naming conventions in programming Laptop typing
An overhead view shows a person’s hands on a laptop keyboard.

Conventions by language and element

Every language has its own defaults, and the safest move is to follow them. In C-style languages such as Java, JavaScript and TypeScript, it is conventional to use camelCase for variables, functions, method names and object properties, and PascalCase for classes, interfaces and namespaces. Snake case is seldom used there, other than for constants.

Python leans the other way. The PEP 8 style guide, as summarized in Beyond the Basic Stuff with Python, sets these rules:

  • Class names are written in PascalCase.
  • Function, method and variable names are lowercase snake_case.
  • Constants are written in uppercase snake case.
  • Modules get short, all lowercase names.
  • The first argument of a method is named self, and of a class method cls.
  • Private attributes in classes begin with an underscore.

Acronyms need one extra rule. PEP 8 recommends capitalizing every letter of an acronym inside a PascalCase name, as a Python naming guide explains, so HTTPServerError is better than HttpServerError, as the Earth Data Science course puts it.

How long should a name be?

Long variable names are tedious to type, whereas short ones can be confusing or mysterious. Because code is read more often than it’s written, it is safer to err on the side of too long. A name like mon could stand for monitor, month, monster or any number of other words.

Short names do have a few accepted uses:

  • i as the index in a for loop, with j and k for nested loops.
  • x and y for Cartesian coordinates.
  • Any other short name, but only when it is completely clear from context what it represents.

Scope is the best guide to length: the larger a name’s scope, the more descriptive it should be. A short name like payment is fine for a local variable inside a single, short function, but a global variable in a big program needs more context. Short phrases also help, because number_of_trials is more readable than simply number_trials.

Habits that make names easier to read

Most naming problems come from a handful of habits. These are worth checking in your own code:

  • Do not drop letters. If a name isn’t easily pronounceable, it isn’t easily understood.
  • Avoid look-alike characters. Skip the lowercase l, uppercase O and uppercase I as single-character names, because fonts can make them look like numbers.
  • Say what a function does. The Python course at the University of Vermont contrasts weak names like i2m() and sd() with inches_to_meters() and standard_deviation().
  • Keep placeholders in examples. Names like spam, eggs, foo and bar belong in tutorials, not in real-world programs.

The first two points come from the Earth Data Science course and the book chapter linked above, and the function examples come from the University of Vermont’s style notes.

Keeping names consistent across a team

Consistency beats purity. The author of a Python naming guide notes that PEP 8 itself admits the naming conventions of Python’s library are a bit of a mess, so if you join a project that already uses a specific naming convention, you should stick to it. The same source adds that being consistent is more important than following the guide.

Tools help, but only partly. Flavio Copes explains that formatters don’t rename anything, so a badly named GetTotal stays wrong until you fix it yourself, though Ruff flags such names if you enable its naming rules. A tool such as pycodestyle checks a file against PEP 8 and prints every violation with its rule number.

Shared rules also keep code reviews about the change, not about spacing. Settling names up front fits with the advice in pull request best practices, where reviewers can then spend their time on logic.

A simple routine for each new name:

  1. Check what your language and your project already do for this kind of element.
  2. Apply the matching casing: class, function, variable or constant.
  3. Make the name say what the thing holds or does, with more context the wider its scope.
  4. Reread it as a stranger would. If an abbreviation could mean two things, spell it out.

For a wider look at how unclear code adds up over time, read what is technical debt.

Frequently asked questions

Is camelCase or snake_case better?

Show answer

Neither is better in itself. Use the standard of your language, which is snake_case for Python variables and functions and camelCase in JavaScript and TypeScript, and keep it consistent through the project.

Are single-letter variable names ever acceptable?

Show answer

Yes, in narrow cases. Loop indexes such as i, j and k and coordinates such as x and y are common, but most other single letters leave other readers guessing.

Can a formatter fix my bad names?

Show answer

No. Tools like Black reformat layout, but a formatter can’t decide what you should name your variables, so naming stays a human judgment. Linters can flag wrongly cased names, as this PEP 8 overview shows for Ruff.

Do I have to follow PEP 8 strictly?

Show answer

No. According to Beyond the Basic Stuff with Python, you can bend or break these rules as required, because PEP 8 itself says a programmer doesn’t need to follow it strictly. What matters for readability is consistency in the style you choose.