首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >代码重构之移除对参数的赋值

代码重构之移除对参数的赋值

作者头像
编程随想曲
发布于 2022-04-21 13:30:56
发布于 2022-04-21 13:30:56
7230
举报
文章被收录于专栏:编程随想曲编程随想曲

意图

区别按值传递和按引用传递,提升代码的清晰度如果只以参数表示被传递进来的东西,代码会清晰很多

示例

代码语言:javascript
复制
/**
 * 移除对参数的赋值之前
 * Created by luo on 2017/4/25.
 */
public class RemoveAssignmentsToParametersBefore {
    void discount (int inputVal, int quantity, int yearToDate){
        if (inputVal > 50){
            inputVal -= 20;
        }
    }
}
/**
 * 移除对参数的赋值之后
 * Created by luo on 2017/4/25.
 */
public class RemoveAssignmentsToParametersAfter {
    void discount (int inputVal, int quantity, int yearToDate){
        int result = inputVal;
        if (inputVal > 50){
            result -= 20;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-04-25,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档