[TIPS] Refactoring - Clean Code - Tip 2 - Use Meaningful Names

By JoeVu, at: 2024年6月28日10:25

Estimated Reading Time: 5 min read

[TIPS] Refactoring - Clean Code - Tip 2 - Use Meaningful Names
[TIPS] Refactoring - Clean Code - Tip 2 - Use Meaningful Names

Refactoring Tip 2: Use Meaningful Names

  • Junior Developer: Might use vague or short variable names (e.g., a, temp).
     
  • Senior Developer: Uses descriptive and meaningful names that convey the purpose of the variable or function (e.g., user_age, calculate_total).

Meaningful names are essential for writing clear, maintainable code. Here's an example to illustrate the difference between how a junior and a senior developer might approach this principle:

 

Example 1: Refactoring a Data Processing Script


Junior Developer's Approach:

A junior developer might write a script with vague variable names that make the code harder to understand.

def process_data(x, y):
    z = []
    for a in x:
        z.append(a * y)
    return z


# Usage
result = process_data([1, 2, 3], 10)

 

Senior Developer's Approach:

A senior developer would use descriptive names that clearly indicate the purpose of each variable and function.

def multiply_elements(data_list, multiplier):
    multiplied_data = []
    for element in data_list:
        multiplied_data.append(element * multiplier)
    return multiplied_data


# Usage
multiplied_result = multiply_elements([1, 2, 3], 10)

 

Example 2: Refactoring a Web Scraping Script


Junior Developer's Approach:

A junior developer might use non-descriptive variable names, making the script less readable.

import requests
from bs4 import BeautifulSoup


def scrape_data(url):
    r = requests.get(url)
    s = BeautifulSoup(r.text, 'html.parser')
    d = s.find_all('div', class_='data')
    return [i.text for i in d]


# Usage
data = scrape_data('https://example.com')

 

Senior Developer's Approach:

A senior developer would use meaningful names to make the script self-explanatory.

import requests
from bs4 import BeautifulSoup


def scrape_website_data(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    data_elements = soup.find_all('div', class_='data')
    return [element.text for element in data_elements]


# Usage
website_data = scrape_website_data('https://example.com')

 

Example 3: Refactoring API Integration


Junior Developer's Approach:

A junior developer might use generic variable names, reducing the clarity of the code.

import requests


def get_data(u):
    r = requests.get(u)
    return r.json()


# Usage
data = get_data('https://api.example.com/user')

 

Senior Developer's Approach:

A senior developer would use specific names that clearly indicate what data is being fetched.

import requests


def fetch_user_data(api_url):
    response = requests.get(api_url)
    return response.json()


# Usage
user_data = fetch_user_data('https://api.example.com/user')

 

Example 4: Refactoring a Sorting Function

Junior Developer's Approach:

A junior developer might write a sorting function with unclear variable names.

def s(l):
    for i in range(len(l)):
        for j in range(i + 1, len(l)):
            if l[i] > l[j]:
                temp = l[i]
                l[i] = l[j]
                l[j] = temp
    return l


# Usage
sorted_list = s([4, 2, 5, 1, 3])

 

Senior Developer's Approach:

A senior developer would use names that clearly indicate the purpose of each variable and function.

def bubble_sort(unsorted_list):
    for current_index in range(len(unsorted_list)):
        for next_index in range(current_index + 1, len(unsorted_list)):
            if unsorted_list[current_index] > unsorted_list[next_index]:
                temp = unsorted_list[current_index]
                unsorted_list[current_index] = unsorted_list[next_index]
                unsorted_list[next_index] = temp
    return unsorted_list


# Usage
sorted_numbers = bubble_sort([4, 2, 5, 1, 3])

 

These examples demonstrate how a senior developer's approach to using meaningful names can lead to code that is more understandable, maintainable, and easier to debug. By providing clear and descriptive names, the intent of the code becomes immediately apparent, reducing the cognitive load for anyone reading or maintaining it.


Subscribe

Subscribe to our newsletter and never miss out lastest news.