DEV Community

Abu Bakar Siddik
Abu Bakar Siddik

Posted on

The Art of Naming in Programming: Why Good Names Matter!

Hey there, fellow Programmers! Let's talk about something we all do but rarely think about: naming our code.

Why Names Are Your Code's First Impression

Imagine walking into a room where everything is labeled with "thing1", "thing2", "thing3". Confusing, right? That's exactly how bad code names feel to other developers.

Here's a terrible example:

def f(x, y):
    return x * y
Enter fullscreen mode Exit fullscreen mode

Now, a better version:

def calculate_rectangle_area(length, width):
    return length * width
Enter fullscreen mode Exit fullscreen mode

See the difference? The second version tells you exactly what's happening.

Revealing Intent Matters

Good names answer three key questions:

  • What does this do?
  • Why does it exist?
  • How will it be used?

Let's look at a real-world example:

# Bad: Unclear purpose
def process(data):
    result = []
    for item in data:
        if item > 0:
            result.append(item)
    return result

# Better: Clear and intentional
def filter_positive_numbers(number_list):
    return [number for number in number_list if number > 0]
Enter fullscreen mode Exit fullscreen mode

Avoiding the Naming Pitfalls

Common mistakes to dodge:

  1. Cryptic Abbreviations:
# Avoid
usr_cnt = len(users)

# Prefer
user_count = len(users)
Enter fullscreen mode Exit fullscreen mode
  1. Meaningless Variations:
# Confusing
def get_user_info()
def get_user_data()
def get_user_details()

# Clear
def get_user_profile()
Enter fullscreen mode Exit fullscreen mode
  1. Single-Letter Names:
# Bad
def calc(x, y, z):
    return x * y / z

# Good
def calculate_average_rate(total_revenue, total_hours, number_of_projects):
    return total_revenue / (total_hours * number_of_projects)
Enter fullscreen mode Exit fullscreen mode

Practical Naming Guidelines

  • Classes: Use nouns
  • Functions: Use verbs
  • Variables: Be specific
  • Constants: ALL_UPPERCASE
# Great naming example
class CustomerAccount:
    MAX_WITHDRAWAL_LIMIT = 5000

    def calculate_monthly_interest(self, balance):
        return balance * 0.05
Enter fullscreen mode Exit fullscreen mode

Context is King

Names should make sense in their environment. A variable like state could mean anything. But customer_state or order_processing_state is crystal clear.

# Unclear
def update(state):
    pass

# Clear
def update_order_processing_state(order_status):
    pass
Enter fullscreen mode Exit fullscreen mode

The Golden Rules

  1. Be consistent
  2. Be descriptive
  3. Keep it simple
  4. Think about the next developer (maybe future you!)

Final Thoughts

Naming isn't just typing words. It's communication. You're telling a story with your code. Make it a story others want to read.

Your future self will thank you. Your teammates will thank you. Heck, even your computer might give you a virtual high-five✋.

Top comments (0)