defprocess_order(order): if order isNone: return"订单不存在" ifnot order.is_paid: return"订单未支付" if order.is_cancelled: return"订单已取消" # 主流程:无嵌套,逻辑清晰 return ship_order(order)
优势:减少嵌套,聚焦主逻辑,易于扩展。
二、合并条件(Combine Conditions)
将多个相关的条件合并为一个简洁的逻辑表达式,减少冗余的 if 语句。
核心思想:用范围表达式或逻辑运算符直接表达条件的组合关系。
适用场景:检查同一变量的范围,或多个条件必须同时满足。
示例(Python):
1 2 3 4 5 6 7 8 9 10
# 传统写法:多个条件分开判断 defis_valid_age(age): if age >= 18: if age <= 60: returnTrue returnFalse
# 合并条件:直接返回布尔表达式 defis_valid_age(age): return18 <= age <= 60# Python 支持链式比较
优势:代码更紧凑,避免重复判断。
三、分解条件(Decompose Condition)
将复杂的条件表达式拆分成有意义的变量或函数,让代码“自解释”。
核心思想:用命名清晰的变量或函数封装条件,替代难以理解的逻辑组合。
适用场景:条件包含多个逻辑运算,或条件含义不直观。
示例(Python):
1 2 3 4 5 6 7 8 9 10 11 12
# 传统写法:复杂条件直接写在 if 里 defcan_edit_article(user, article): if user.is_login() and user.has_permission("edit") and article.author_id == user.id: returnTrue returnFalse
defcalc_area(shape): if shape.type == "circle": r = shape.kwargs["radius"] return3.14 * r * r elif shape.type == "rectangle": w = shape.kwargs["width"] h = shape.kwargs["height"] return w * h return0
# 传统写法:3 层嵌套 defprocess_data(data): if data isnotNone: if data.is_valid(): if data.has_key("content"): content = data["content"] # 处理 content... return content returnNone
# 优化写法:卫语句 + 函数分解 defprocess_data(data): if data isNone: returnNone ifnot data.is_valid(): returnNone ifnot data.has_key("content"): returnNone return process_content(data["content"])
defprocess_order(order): if order isNone: return"订单不存在" ifnot order.is_paid: return"订单未支付" if order.is_cancelled: return"订单已取消" # 主流程:无嵌套,逻辑清晰 return ship_order(order)
优势:减少嵌套,聚焦主逻辑,易于扩展。
二、合并条件(Combine Conditions)
将多个相关的条件合并为一个简洁的逻辑表达式,减少冗余的 if 语句。
核心思想:用范围表达式或逻辑运算符直接表达条件的组合关系。
适用场景:检查同一变量的范围,或多个条件必须同时满足。
示例(Python):
1 2 3 4 5 6 7 8 9 10
# 传统写法:多个条件分开判断 defis_valid_age(age): if age >= 18: if age <= 60: returnTrue returnFalse
# 合并条件:直接返回布尔表达式 defis_valid_age(age): return18 <= age <= 60# Python 支持链式比较
优势:代码更紧凑,避免重复判断。
三、分解条件(Decompose Condition)
将复杂的条件表达式拆分成有意义的变量或函数,让代码“自解释”。
核心思想:用命名清晰的变量或函数封装条件,替代难以理解的逻辑组合。
适用场景:条件包含多个逻辑运算,或条件含义不直观。
示例(Python):
1 2 3 4 5 6 7 8 9 10 11 12
# 传统写法:复杂条件直接写在 if 里 defcan_edit_article(user, article): if user.is_login() and user.has_permission("edit") and article.author_id == user.id: returnTrue returnFalse
defcalc_area(shape): if shape.type == "circle": r = shape.kwargs["radius"] return3.14 * r * r elif shape.type == "rectangle": w = shape.kwargs["width"] h = shape.kwargs["height"] return w * h return0
# 传统写法:3 层嵌套 defprocess_data(data): if data isnotNone: if data.is_valid(): if data.has_key("content"): content = data["content"] # 处理 content... return content returnNone
# 优化写法:卫语句 + 函数分解 defprocess_data(data): if data isNone: returnNone ifnot data.is_valid(): returnNone ifnot data.has_key("content"): returnNone return process_content(data["content"])
defprocess_order(order): if order isNone: return"订单不存在" ifnot order.is_paid: return"订单未支付" if order.is_cancelled: return"订单已取消" # 主流程:无嵌套,逻辑清晰 return ship_order(order)
优势:减少嵌套,聚焦主逻辑,易于扩展。
二、合并条件(Combine Conditions)
将多个相关的条件合并为一个简洁的逻辑表达式,减少冗余的 if 语句。
核心思想:用范围表达式或逻辑运算符直接表达条件的组合关系。
适用场景:检查同一变量的范围,或多个条件必须同时满足。
示例(Python):
1 2 3 4 5 6 7 8 9 10
# 传统写法:多个条件分开判断 defis_valid_age(age): if age >= 18: if age <= 60: returnTrue returnFalse
# 合并条件:直接返回布尔表达式 defis_valid_age(age): return18 <= age <= 60# Python 支持链式比较
优势:代码更紧凑,避免重复判断。
三、分解条件(Decompose Condition)
将复杂的条件表达式拆分成有意义的变量或函数,让代码“自解释”。
核心思想:用命名清晰的变量或函数封装条件,替代难以理解的逻辑组合。
适用场景:条件包含多个逻辑运算,或条件含义不直观。
示例(Python):
1 2 3 4 5 6 7 8 9 10 11 12
# 传统写法:复杂条件直接写在 if 里 defcan_edit_article(user, article): if user.is_login() and user.has_permission("edit") and article.author_id == user.id: returnTrue returnFalse
defcalc_area(shape): if shape.type == "circle": r = shape.kwargs["radius"] return3.14 * r * r elif shape.type == "rectangle": w = shape.kwargs["width"] h = shape.kwargs["height"] return w * h return0
# 传统写法:3 层嵌套 defprocess_data(data): if data isnotNone: if data.is_valid(): if data.has_key("content"): content = data["content"] # 处理 content... return content returnNone
# 优化写法:卫语句 + 函数分解 defprocess_data(data): if data isNone: returnNone ifnot data.is_valid(): returnNone ifnot data.has_key("content"): returnNone return process_content(data["content"])