一 前言
二 什么是复杂性
1 理性度量
2 感性认知
Complexity is anything that makes software hard to understand or to modify
-- John Ousterhout 《A Philosophy of Software Design》
译:所谓复杂性,就是任何使得软件难于理解和修改的因素。
三 复杂性的表现形式
症状1-变更放大
Change amplification: a seemingly simple change requires code modifications in many different places.
-- John Ousterhout 《A Philosophy of Software Design》
译:看似简单的变更需要在许多不同地方进行代码修改。
/**
* 销售捡入客户
*/
public void pick(String salesId, String customerId) {
// 查询客户总数
long customerCnt = customerDao.findCustomerCount(salesId);
// 查询销售库容
long capacity = capacityDao.findSalesCapacity(salesId);
// 判断是否超额
if(customerCnt >= capacity) {
throws new BizException("capacity over limit");
}
// 代码省略 do customer pick
}
症状2-认知负荷
Cognitive load: how much a developer needs to know in order to complete a task.
-- John Ousterhout 《A Philosophy of Software Design》
译:开发人员需要多少知识才能完成一项任务。
症状3-未知的未知
Unknown unknowns: it is not obvious which pieces of code must be modified to complete a task
-- John Ousterhout 《A Philosophy of Software Design》
译:必须修改哪些代码才能完成任务。
四 为什么会产生复杂性
想简单图省事,没有及时治理不合理的内容 缺少匠心追求,对肮脏代码视而不见 技术能力不够,无法应对复杂系统 交接过渡缺失,三无产品几乎无法维护
1 统一的中国与分裂的欧洲
2 软件固有的复杂性
The Complexity of software is an essential property, not an accidental one.
-- Grady Booch 《Object-Oriented Analysis and Design with Applications》
译:软件的复杂性是一个基本特征,而不是偶然如此。
Everything, without exception, requires additional energy and order to maintain itself. I knew this in the abstract as the famous second law of thermodynamics, which states that everything is falling apart slowly.
-- Kevin Kelly 《The Inevitable》
译:世间万物都需要额外的能量和秩序来维持自身,无一例外。这就是著名的热力学第二定律,即所有的事务都在缓慢地分崩离析。
五 软件架构治理复杂度
1 架构的本质
结构化编程取消 goto 移除跳转语句,对程序控制权的直接转移进行了限制和规范
面向对象编程限制 指针 的使用,对程序控制权的间接转移进行了限制和规范
函数式编程以 λ演算法 为核心思想,对程序中的赋值进行了限制和规范
2 递增的复杂性
模糊性创造了复杂,依赖性传播了复杂 复杂性往往不是由单个灾难引起的 我们可以容易地说服自己,当前变更带来的一点点复杂性没什么大不了
3 编程思维论
战术编程
当前一定是最快的 不会花费太多时间来寻找最佳设计 每个编程任务都会引入一些复杂度 重构会减慢当前任务速度,所以保持最快速度
@HSFProvider(serviceInterface = AgnDistributeRuleConfigQueryService.class)
public class AgnDistributeRuleConfigQueryServiceImpl implements AgnDistributeRuleConfigQueryService {
@Override
public ResultModel<AgnDistributeRuleConfigDto> queryAgnDistributeRuleConfigById(String id) {
logger.info("queryAgnDistributeRuleConfigById id=" + id);
ResultModel<AgnDistributeRuleConfigDto> result = new ResultModel<AgnDistributeRuleConfigDto>();
if(StringUtils.isBlank(id)){
result.setSuccess(false);
result.setErrorMsg("id cannot be blank");
return result
}
try {
AgnDistributeRuleConfigDto agnDistributeRuleConfigDto = new AgnDistributeRuleConfigDto();
AgnDistributeRuleConfig agnDistributeRuleConfig = agnDistributeRuleConfigMapper.selectById(id);
if(agnDistributeRuleConfig == null){
logger.error("agnDistributeRuleConfig is null");
result.setSuccess(false);
result.setErrorMsg("agnDistributeRuleConfig is null");
return result
}
this.filterDynamicRule(agnDistributeRuleConfig);
BeanUtils.copyProperties(agnDistributeRuleConfig, agnDistributeRuleConfigDto);
result.setSuccess(true);
result.setTotal(1);
result.setValues(agnDistributeRuleConfigDto);
} catch (Exception e) {
logger.error("queryAgnDistributeRuleConfigById error,", e);
result.setSuccess(false);
result.setErrorMsg(e.getMessage());
}
return result;
}
}
Facade层定义全部逻辑 - 未做结构分层 业务与技术未做分离 - 耦合接口信息与业务数据 Try catch 满天飞 - 缺少统一异常处理机制 没有规范化的日志格式 - 日志格式混乱