Неточное название переменной
Анти-паттерн
Название не должно обманывать. Важно называть переменные точно, чтобы при чтении названия переменной впервые было сразу понятно, что в ней лежит.
Пример 1¶
Плохо:
number = 2
salad = 'fruit salad'
fruits = ['apple', 'orange', 'banana', 'grapes']
Хорошо:
servings_num = 2
salad_name = 'fruit salad'
salad_ingredients = ['apple', 'orange', 'banana', 'grapes']
Пример 2¶
Плохо:
def get_book(name, folder):
...
return book
Хорошо:
def get_book(book_name, books_folder_path):
...
return book
Пример 3¶
Плохо:
class Employee:
def __init__(self, name, money, percentage, is_good):
self.name = name
self.money = money
self.percentage = percentage
self.is_good = is_good
self.bonus = 0
def calculate_full_salary(self):
self.bonus = self.calculate_bonus()
return self.money + self.bonus
def calculate_bonus(self):
if self.is_good:
return self.money * (self.percentage / 100)
return 0
Хорошо:
class Employee:
def __init__(self, name, salary, bonus_percentage, is_good_employee):
self.name = name
self.salary = salary
self.bonus_percentage = bonus_percentage
self.is_good_employee = is_good_employee
self.bonus = 0
def calculate_full_salary(self):
self.bonus = self.calculate_bonus()
return self.salary + self.bonus
def calculate_bonus(self):
if self.is_good_employee:
return self.salary * (self.bonus_percentage / 100)
return 0
Пример 4¶
Плохо:
leading_app = deal.leading_application
bank = leading_app.get('bank_name')
portal = leading_app.get('portal_name')
Хорошо:
leading_app = deal.leading_application
bank_name = leading_app.get('bank_name')
portal_name = leading_app.get('portal_name')