📅  最后修改于: 2023-12-03 15:41:52.562000             🧑  作者: Mango
在软件开发的过程中,代码可能会变得越来越复杂和难以维护。而代码重构就是一种通过重新组织代码结构,以达到更好的可读性、可维护性和可扩展性的软件开发技术。在本文中,我们将介绍软件工程中常见的 7 种代码重构技术。
重命名是一种简单但非常有用的编码技巧。它可以帮助改善代码的可读性和可理解性。通常,我们使用一个更准确的名称来替代一个变量、函数或类的旧名称。这个新名称应该更符合其功能或实现。
# Rename before:
def a(x):
return x ** 2
# Rename after:
def square(x):
return x ** 2
Extract Function 是一种重构方法,将代码片段转换为一个独立的函数。这样做可以提高代码的可读性和可维护性,减少重复代码,并使代码更易于测试和扩展。
# Extract Function before:
def calculate_salary(employee):
total_working_hours = employee.get_total_working_hours()
hourly_rate = employee.hourly_rate
salary = total_working_hours * hourly_rate
if employee.is_manager:
salary += 1000
return salary
# Extract Function after:
def calculate_salary(employee):
total_working_hours = employee.get_total_working_hours()
hourly_rate = employee.hourly_rate
salary = calculate_base_salary(total_working_hours, hourly_rate)
if employee.is_manager:
salary += calculate_manager_bonus()
return salary
def calculate_base_salary(total_working_hours, hourly_rate):
return total_working_hours * hourly_rate
def calculate_manager_bonus():
return 1000
Inline Function 是 Extract Function 的逆过程。它将函数调用替换为函数本身的所有内容。这种重构方法可以帮助消除不必要的复杂性和提高代码的可读性。
# Inline Function before:
def calculate_salary(employee):
return get_base_salary(employee) + get_manager_bonus(employee)
def get_base_salary(employee):
total_working_hours = employee.get_total_working_hours()
hourly_rate = employee.hourly_rate
return total_working_hours * hourly_rate
def get_manager_bonus(employee):
if employee.is_manager:
return 1000
else:
return 0
# Inline Function after:
def calculate_salary(employee):
total_working_hours = employee.get_total_working_hours()
hourly_rate = employee.hourly_rate
salary = total_working_hours * hourly_rate
if employee.is_manager:
salary += 1000
return salary
Extract Class 是将一部分相关的属性和方法从一个类中提取出来,创建一个新的类的重构方法。这可以帮助保持类的简洁性和清晰性,并降低代码的复杂性。
# Extract Class before:
class Employee:
def __init__(self, name, phone, email, hourly_rate, total_working_hours, is_manager):
self.name = name
self.phone = phone
self.email = email
self.hourly_rate = hourly_rate
self.total_working_hours = total_working_hours
self.is_manager = is_manager
def calculate_salary(self):
salary = self.total_working_hours * self.hourly_rate
if self.is_manager:
salary += 1000
return salary
# Extract Class after:
class Employee:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
self.job_title = None
self.pay_type = None
class HourlyEmployee(Employee):
def __init__(self, hourly_rate, total_working_hours):
super().__init__()
self.hourly_rate = hourly_rate
self.total_working_hours = total_working_hours
def calculate_salary(self):
salary = self.total_working_hours * self.hourly_rate
return salary
class SalariedEmployee(Employee):
def __init__(self, monthly_salary):
super().__init__()
self.monthly_salary = monthly_salary
def calculate_salary(self):
return self.monthly_salary
Move Method 是将方法从一个类中移到另一个类的重构方法。这样做可以提高类的聚合性和耦合性,并使代码更容易扩展和维护。
# Move Method before:
class Employee:
def __init__(self, name, phone, email, hourly_rate, total_working_hours, is_manager):
self.name = name
self.phone = phone
self.email = email
self.hourly_rate = hourly_rate
self.total_working_hours = total_working_hours
self.is_manager = is_manager
def calculate_salary(self):
salary = self.total_working_hours * self.hourly_rate
if self.is_manager:
salary += 1000
return salary
def print_employee_info(self):
print('Name: {}'.format(self.name))
print('Phone: {}'.format(self.phone))
print('Email: {}'.format(self.email))
class Manager(Employee):
def __init__(self, name, phone, email, hourly_rate, total_working_hours):
super().__init__(name, phone, email, hourly_rate, total_working_hours, is_manager=True)
# Move Method after:
class Employee:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
def print_employee_info(self):
print('Name: {}'.format(self.name))
print('Phone: {}'.format(self.phone))
print('Email: {}'.format(self.email))
class Payroll:
def __init__(self, hourly_rate, total_working_hours):
self.hourly_rate = hourly_rate
self.total_working_hours = total_working_hours
def calculate_salary(self):
salary = self.total_working_hours * self.hourly_rate
return salary
class Employee:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
self.payroll = None
def set_payroll(self, payroll):
self.payroll = payroll
def calculate_salary(self):
return self.payroll.calculate_salary()
class Manager(Employee):
def __init__(self, name, phone, email, hourly_rate, total_working_hours):
super().__init__(name, phone, email)
self.set_payroll(HourlyPayroll(hourly_rate, total_working_hours))
Replace Conditional with Polymorphism 是一种重构技术,将一系列的条件分支语句替换为多态的形式。这可以使代码更加清晰和易于维护。
# Replace Conditional with Polymorphism before:
class Employee:
def __init__(self, name, phone, email, hourly_rate, total_working_hours, is_manager):
self.name = name
self.phone = phone
self.email = email
self.hourly_rate = hourly_rate
self.total_working_hours = total_working_hours
self.is_manager = is_manager
def calculate_payroll(self):
if self.is_manager:
salary = self.total_working_hours * self.hourly_rate + 1000
else:
salary = self.total_working_hours * self.hourly_rate
return salary
class Manager(Employee):
def __init__(self, name, phone, email, hourly_rate, total_working_hours):
super().__init__(name, phone, email, hourly_rate, total_working_hours, is_manager=True)
# Replace Conditional with Polymorphism after:
class Payroll:
def calculate_payroll(self, employee):
pass
class HourlyPayroll(Payroll):
def calculate_payroll(self, employee):
return employee.total_working_hours * employee.hourly_rate
class ManagerPayroll(Payroll):
def calculate_payroll(self, employee):
return employee.total_working_hours * employee.hourly_rate + 1000
class Employee:
def __init__(self, name, phone, email, payroll):
self.name = name
self.phone = phone
self.email = email
self.payroll = payroll
def calculate_payroll(self):
return self.payroll.calculate_payroll(self)
class Manager(Employee):
def __init__(self, name, phone, email, hourly_rate, total_working_hours):
payroll = ManagerPayroll()
super().__init__(name, phone, email, payroll)
Remove Duplication 是一种简单而常见的重构技术,目的是消除代码中的重复部分。
# Remove Duplication before:
def area_square(x):
return x ** 2
def volume_cube(x):
return x ** 3
# Remove Duplication after:
def power(x, n):
return x ** n
def area_square(x):
return power(x, 2)
def volume_cube(x):
return power(x, 3)
以上就是 7 种常见的软件工程中的代码重构技术,程序员可以根据需要,在实际的代码开发中选择适合的重构方法,以提高代码的可读性、可维护性和可扩展性。