Why Do Programmers Love Using Underscores in Code So Much?

This article analyzes why programmers widely use underscores, such as improving code readability and following naming conventions.
On this page

Why Do Programmers Love Using Underscores in Code So Much?

Excerpt

This article explores why underscores are so commonly used by programmers for naming variables, functions, files, etc. in code.


Walk through any codebase and you’ll notice a common character popping up in variable and function names - the humble underscore. Why is this unassuming symbol so popular among programmers across languages? Read on to find out!

Introduction

The underscore character “_” is frequently used by programmers in naming conventions for variables, functions, files, and more. There are several key reasons underscores have become ubiquitous:

  • They improve readability
  • They follow style guide conventions
  • They avoid spaces
  • They provide delimiters/separators
  • They avoid issues with special characters
  • They distinguish names from reserved words
  • They promote consistency

This article will explore each of these factors that make the underscore a favorite of coders everywhere.

Improved Readability

Underscores can significantly improve the readability of variable and function names. By visually separating words in names, underscores make them easier to parse.

For example, student_count is more readable than studentcount or studentCount. The underscores act like spaces to divide the words.

This readability advantage makes underscores very popular for compound names. The underscores provide visual breaks in long names that use multiple words joined together.

Conventions and Style Guides

In addition to readability, many coding language style guides actually recommend underscores for naming conventions.

Python’s PEP 8 specifically promotes underscores for function and variable names. Other languages like Perl also incorporate underscores into their naming standards.

So using underscores aligns with the official style guides of many languages. This convention then becomes ingrained in programmers who follow these guide’s best practices.

Free Online Conversion Tools

Switching naming conventions like camelCase to underscores can be tedious. Luckily there are free online tools to quickly convert between styles:

For example, pasting “student Count” into this tool would return “student_count”.

These tools help follow naming conventions with minimal effort.

Click for more tools!

Avoid Spaces

Most programming languages do not allow spaces in variable or function names. Therefore, underscores provide a space-like substitute when needing to separate words in a name.

Without underscores, the alternative would be jamming words together without any divider (like studentcount). This decreases readability substantially.

So underscores deliver a spacer where spaces aren’t possible.

Delimiters and Separators

Beyond acting as an internal space, underscores also serve as useful delimiters and separators in names.

For example, a function name like get_input_from_user() uses underscores to neatly separate the different components.

Underscores are also frequently used as separators in file and directory structures like /path/to/documents/.

This provides visual organization of parts in names.

Avoid Special Characters

Some special characters like dashes (-) can potentially cause issues in names depending on the language and environment.

Underscores offer a neutral and “safe” character that is supported across all languages and systems. There is minimal risk of unintended behavior from underscores.

This avoids headaches that can come from special characters in names.

Distinction from Reserved Words

Most languages have reserved words that cannot be used as variable names. For example, you cannot name a variable class in many languages since that is a reserved keyword.

Underscores provide a simple way to disambiguate from reserved words by formatting names like class_name.

This prevents conflicts with reserved words while still allowing related names.

Consistency

Once underscores are established as the naming convention within a particular codebase, consistency adds even more readability and familiarity.

Developers familiar with a codebase will instantly recognize names formatted with underscores. Mixing naming styles randomly would be confusing.

Adhering to a consistent convention, like using underscores, avoids cognitive strain on future readers of the code.

Conclusion

There are many great reasons underscores have become the character of choice among programmers for naming variables, functions, files, and more:

  • Improved readability
  • Conventional style guide formatting
  • Substitute for unavailable spaces
  • Useful as delimiters and separators
  • Avoids issues with special characters
  • Distinguishes names from reserved words
  • Promotes consistency within a project

By following language conventions and leveraging underscores for readability, any programmer can improve their variable and function names. A little consistency goes a long way!

So next time you are naming a new programming construct, consider the humble but handy underscore.