DEV Community

Cover image for πŸš€3 Important Rules for Naming Variables in Excellent Code
Fahim Ahammed Firoz
Fahim Ahammed Firoz

Posted on

1

πŸš€3 Important Rules for Naming Variables in Excellent Code

Good variable names are just as important as clear function names for making code easy to understand. Here are 3 simple rules to make your variable names better:

  1. Meaningful Names: Replace generic names (like temp or x, y, x) with names that reflect their purpose.

❌ tempData

βœ… customerOrder

  1. Descriptive Naming: Use descriptive terms that explain the content of the variable.

❌ isSomething (unclear what "something" is)
βœ… isUserLoggedIn (clear and concise)

  1. Case Consistency: Pick a case convention (snake_case, camelCase) and stick to it throughout your codebase.
  • Snake Case: Lowercase letters with underscores for separation (e.g., total_price)
  • Camel Case: Lowercase with the first letter of each word capitalized (e.g., totalPrice)

Bonus Tip: Consider adding prefixes or suffixes for specific variable types (e.g., strFirstName, intEmployeeID).

By following these principles, your variables become self-documenting, improving code readability and maintainability for yourself and your fellow developers.

Consistent naming across functions and variables is key!

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more