前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >教科书式的代码

教科书式的代码

作者头像
IT云清
发布2019-01-22 15:31:09
5260
发布2019-01-22 15:31:09
举报
文章被收录于专栏:IT云清IT云清
在项目中遇到一个bug,追进接口后,发现这个接口调用了超过10个大方法,下面是其中一个,我觉得可以作为一个教科书级别的典范。我觉得有志于把代码写的优雅的朋友,可以仔细看看这个代码,把这个代码的问题看明白了,那代码自然就写的简洁优雅了。
这是一段历史代码,这个代码是谁写的不重要。重要的是,它暴露出了一些典型的问题,值得让人思考和借鉴,这才是这个文章的目的。(毕竟每个程序员,都写过很不符合规范的代码,可以理解这个现象。)
几个月前,也是在项目中看到了大量类似代码,当时任务比较重,低质量的代码导致项目进度完全无法按照预期进度进行,因为你不知道哪个地方,就会遇到下面这样一个方法,直接导致一个人半天甚至一天的时间就浪费了。我当时写了篇文章言辞比较激烈低端程序员的明显特征,还被博友喷了,现在看来,文章的看似偏激的观点,是值得思考的。
作为一个程序员,科班,还是培训,还是自学,希望你尊重程序员这个职业,也希望你拿着工资干活时,能把活干漂亮点,跟你一起加班熬夜上线的兄弟,你别坑他们。代码质量好,也是一种能力。
时间紧,任务重,加班严重,每个程序员都会遇到这些问题,但是,还是希望,都能把代码写的好一点,坚守一个质量底线,利人利己。
每个基层的程序员,应该都不希望在基层写一辈子代码,如果你是leader,你希望你的兄弟们写出什么样的代码?
代码语言:javascript
复制
public static List<ScfDebtRepayDetail> setAAA(ScfUserLoan userLoan,
                                                          List<Map> list,
                                                          double bj,
                                                          Date curDate,
                                                          String dataSource,
                                                          Integer userId,
                                                          RedisOrderNumService redisOrderNumService,
                                                          ScfDebtRepayService scfDebtRepayService) {
        if (null == list && bj <= 0)
            return null;
        if (!curDate.after(userLoan.getFkTime())) {
            throw new BaseException("本金收款日期(" + DateUtil.getDate(curDate) + ")必须在借款开始时间("
                    + DateUtil.getDate(userLoan.getFkTime()) + ")之后");
        }
        if (bj > userLoan.getRestLoanMoney()) {
            throw new BaseException("不能多还本金");
        }
        Long sign = IdWorker.getId();// 同一次操作的编码
        List<ScfDebtRepayDetail> detailList = new ArrayList<>();
        double rest_corpus = userLoan.getRestLoanMoney();
        boolean yq = curDate.compareTo(userLoan.getExpireTime()) >= 0;

        // 提前还款的情况下,要检查还款的本金与多还的钱是否足以将借款还清
        if (rest_corpus == bj) {
            if (!yq) {
                double tqhkbj = 0d;
                double bj_use_last_money = 0d;
                // 统计下这次还本金是不是可以把本金还完
                double more_interest = 0d;
                Long repay_id = null;
                Date cur_month_start_date = null;
                int cur_period_actual_lengths = 0;
                double lessPayMoney = 0d;
                double dhlx = 0d;

                //为了计算最后一期利息的前面期加起来的值
                double forlastmoney = 0d;
                double originalInterest = 0d;
                double forcountdhlx = 0d;
                for (int i = 0; i < list.size(); i++) {
                    Map repay = list.get(i);
                    Long id = Long.parseLong(repay.get("id").toString());
                    double interest = (double) repay.get(SCF_DEBT_REPAY.interest);
                    double actual_interest = (double) repay.get(SCF_DEBT_REPAY.actual_interest);
                    Date repayDate = (Date) repay.get(SCF_DEBT_REPAY.repay_date);
                    Integer lengths = Integer.parseInt((String) repay.get("lengths"));
                    double loan_money = (double) repay.get(SCF_DEBT_REPAY.loan_money);
                    Date startDate = (Date) repay.get(SCF_DEBT_REPAY.start_date);
                    Long repay_date_ = Long.parseLong(DateUtil.DateToString(repayDate,
                            DateStyle.YYYYMMDD));
                    Long start_date_ = Long.parseLong(DateUtil.DateToString(startDate,
                            DateStyle.YYYYMMDD));
                    Long cur_date_ = Long.parseLong(DateUtil.DateToString(curDate,
                            DateStyle.YYYYMMDD));
                    double dhlx_ = DoubleUtil.sub(interest, actual_interest);
                    double pre_cslx = 0d;
                    if (i < list.size() - 1) {
                        pre_cslx = InterestUtil.createBorrowInterest(lengths,
                                userLoan.getInvestRate(), loan_money, 360);
                        forlastmoney = DoubleUtil.add(forlastmoney, pre_cslx);
                        originalInterest = originalInterest + loan_money * lengths
                                * userLoan.getInvestRate() / 360;
                        forcountdhlx = originalInterest;
                    } else {
                        originalInterest = originalInterest + loan_money * lengths
                                * userLoan.getInvestRate() / 360;
                        pre_cslx = originalInterest - forlastmoney;
                        pre_cslx = DoubleUtil.round(pre_cslx, 2);
                    }
                    if (repay_date_ < cur_date_) {
                        dhlx = DoubleUtil.add(dhlx, dhlx_);
                    } else if (cur_date_ >= start_date_ && cur_date_ <= repay_date_) {
                        tqhkbj = bj;
                        repay_id = id;
                        // 计算出提前还本金的时候催收利息

                        double cslx = DoubleUtil.sub(interest, pre_cslx);
                        lessPayMoney = DoubleUtil.add(lessPayMoney, cslx);
                        cur_period_actual_lengths = DateUtil.getIntervalDays(curDate, startDate) + 1;
                        //                        dhlx = InterestUtil.createBorrowInterest(cur_period_actual_lengths,
                        //                            userLoan.getInvestRate(), bj, 360);
                        dhlx = forcountdhlx + bj * cur_period_actual_lengths
                                * userLoan.getInvestRate() / 360 - forlastmoney;
                        dhlx = DoubleUtil.round(dhlx, 2);
                        //催收利息中加入手续费
                        //                        double calculateServiceCharge = ServiceChargeUtil.getServiceCharge(bj);
                        //                        lessPayMoney = DoubleUtil.add(lessPayMoney, calculateServiceCharge);

                        lessPayMoney = DoubleUtil.add(lessPayMoney, dhlx);
                        more_interest = DoubleUtil.add(more_interest, actual_interest);
                        cur_month_start_date = startDate;
                    } else {
                        more_interest = DoubleUtil.add(more_interest, actual_interest);
                    }
                }
                // 如果还有多还的钱
                // 如果这个钱足以多还本金
                if (more_interest >= lessPayMoney) {
                    // 还本金的时候使用了多少利息,当作本金使用
                    //more_interest 一方面用作了本金用掉还有一方面当作待还的利息用掉
                    bj_use_last_money = DoubleUtil.sub(more_interest, lessPayMoney);
                    //当以前多还的钱足以把本次还的本金抵掉的情况下
                    if (bj_use_last_money > bj) {
                        bj_use_last_money = bj;
                    }
                    double more_money = DoubleUtil.sub(more_interest, bj_use_last_money);//
                    Double op_money = DoubleUtil.sub(bj, bj_use_last_money);
                    ScfDebtRepayDetail detail = init(repay_id, 0d, 0d, more_money, 0, 0, 0d, 0,
                            cur_month_start_date, curDate, dataSource, userId, op_money, sign, 0,
                            curDate, tqhkbj, bj_use_last_money, lessPayMoney, 0d);
                    detailList.add(detail);
                    return detailList;
                }

            } else {
                double hkbj = 0d;
                double bj_use_last_money = 0d;
                // 统计下这次还本金是不是可以把本金还完
                Long repay_id = null;
                Date cur_month_start_date = null;
                int cur_period_actual_lengths = 0;
                double yslx = 0d;//应收利息
                double sslx = 0d;//实收利息
                double yqlx = 0d;//逾期利息
                for (int i = 0; i < list.size(); i++) {
                    Map repay = list.get(i);
                    Long id =Long.parseLong(repay.get("id").toString());
                    int period = (int) repay.get(SCF_DEBT_REPAY.period);
                    int max_period = (int) repay.get(SCF_DEBT_REPAY.max_period);
                    double interest = (double) repay.get(SCF_DEBT_REPAY.interest);
                    double actual_interest = (double) repay.get(SCF_DEBT_REPAY.actual_interest);
                    double fee_corpus = (double) repay.get(SCF_DEBT_REPAY.fee_corpus);
                    Date repayDate = (Date) repay.get(SCF_DEBT_REPAY.repay_date);
                    Date startDate = (Date) repay.get(SCF_DEBT_REPAY.start_date);
                    Long repay_date_ = Long.parseLong(DateUtil.DateToString(repayDate,
                            DateStyle.YYYYMMDD));
                    Long start_date_ = Long.parseLong(DateUtil.DateToString(startDate,
                            DateStyle.YYYYMMDD));
                    Long cur_date_ = Long.parseLong(DateUtil.DateToString(curDate,
                            DateStyle.YYYYMMDD));
                    double dhlx_ = DoubleUtil.sub(interest, actual_interest);
                    yslx = DoubleUtil.add(yslx, interest);
                    sslx = DoubleUtil.add(sslx, actual_interest);

                    if (period == max_period) {
                        Double calYqLx = scfDebtRepayService.queryCalYqLx(id, curDate);
                        yqlx = DoubleUtil.sub(calYqLx, fee_corpus);
                        repay_id = id;
                        hkbj = bj;
                    }
                }
                // 如果还有多还的钱
                double rest_money = 0d;
                if (yqlx >= 0) {
                    rest_money = DoubleUtil.sub(DoubleUtil.sub(sslx, yslx), yqlx);
                } else {
                    //TODO 多还的逾期利息抵扣本金
                    //                    yqlx = 0;
                    //                    rest_money = DoubleUtil.sub(sslx, yslx);
                    rest_money = DoubleUtil.sub(DoubleUtil.sub(sslx, yslx), yqlx);
                }
                double all_tmp_corpus = DoubleUtil.sub(DoubleUtil.add(bj, rest_money), yqlx);
                // 如果这个钱足以多还本金
                if (all_tmp_corpus >= rest_corpus) {
                    // 还本金的时候使用了多少利息,当作本金使用
                    //more_interest 一方面用作了本金用掉还有一方面当作待还的利息用掉
                    bj_use_last_money = rest_money;
                    double more_money = -rest_money;//
                    Double op_money = DoubleUtil.sub(bj, bj_use_last_money);
                    ScfDebtRepayDetail detail = init(repay_id, -yqlx, hkbj, more_money, 0, 0, yqlx,
                            0, cur_month_start_date, curDate, dataSource, userId, op_money, sign, 0,
                            curDate, 0d, bj_use_last_money, 0d, 0d);
                    detailList.add(detail);
                    return detailList;
                }
            }
        }

        // 先把当前时间所在的期之前的期该换的利息先还掉
        for (int i = 0; i < list.size(); i++) {
            Map repay = list.get(i);
            long repay_id = Long.parseLong(repay.get(SCF_DEBT_REPAY.id).toString());
            double interest = (double) repay.get(SCF_DEBT_REPAY.interest);
            double actual_interest = (double) repay.get(SCF_DEBT_REPAY.actual_interest);
            Date repayDate = (Date) repay.get(SCF_DEBT_REPAY.repay_date);
            Date startDate = (Date) repay.get(SCF_DEBT_REPAY.start_date);
            Date lastLateTime = (Date) repay.get(SCF_DEBT_REPAY.last_late_time);
            Long repay_date_ = Long.parseLong(DateUtil.DateToString(repayDate, DateStyle.YYYYMMDD));
            Long start_date_ = Long.parseLong(DateUtil.DateToString(startDate, DateStyle.YYYYMMDD));
            Long cur_date_ = Long.parseLong(DateUtil.DateToString(curDate, DateStyle.YYYYMMDD));
            if (cur_date_ >= start_date_ && cur_date_ <= repay_date_) {
                break;
            }
            // 计算待还利息
            double dhlx_ = DoubleUtil.sub(interest, actual_interest);

            // 计算逾期天数
            int yq_days = DateUtil.getYsYqDays(curDate, DateUtil.addDay(lastLateTime, -1));

            Double op_money = dhlx_;
            // 如果当前所在期产生任何费用的,或者,提前还的本金所在当前期内的,都需要生成明细
            if (dhlx_ > 0) {
                ScfDebtRepayDetail detail = init(repay_id, dhlx_, 0d, 0d, 0, 0, 0d, yq_days,
                        startDate, curDate, dataSource, userId, op_money, sign, 0, curDate, 0d, 0d, 0d,
                        0d);
                detailList.add(detail);
            }
        }

        double bj_use_last_money = 0d;
        double tqhkbj = 0d;
        double hkbj = 0d;
        double yqbjlx = 0d;
        double dhlx = 0d;
        double more_interest = 0d;
        Date start_date = null;
        int yqDays = 0;
        Long repayId = null;
        double lessPayMoney = 0d;
        double forlastmoney = 0d;
        double originalInterest = 0d;
        double forcountdhlx = 0d; //为了计算待还利息
        for (int i = 0; i < list.size(); i++) {
            Map repay = list.get(i);
            int period = (int) repay.get(SCF_DEBT_REPAY.period);
            int max_period = (int) repay.get(SCF_DEBT_REPAY.max_period);
            Integer lengths = Integer.parseInt((String) repay.get("lengths"));
            double loan_money = (double) repay.get(SCF_DEBT_REPAY.loan_money);
            double fee_corpus = (double) repay.get(SCF_DEBT_REPAY.fee_corpus);
            long repay_id = Long.parseLong(repay.get(SCF_DEBT_REPAY.id).toString());
            double interest = (double) repay.get(SCF_DEBT_REPAY.interest);
            double actual_interest = (double) repay.get(SCF_DEBT_REPAY.actual_interest);
            double corpus = (double) repay.get(SCF_DEBT_REPAY.corpus);
            double actual_money = (double) repay.get(SCF_DEBT_REPAY.actual_money);
            Date repayDate = (Date) repay.get(SCF_DEBT_REPAY.repay_date);
            Date startDate = (Date) repay.get(SCF_DEBT_REPAY.start_date);
            Date lastLateTime = (Date) repay.get(SCF_DEBT_REPAY.last_late_time);
            Date corpusLateTime = (Date) repay.get(SCF_DEBT_REPAY.corpus_late_time);
            Long repay_date_ = Long.parseLong(DateUtil.DateToString(repayDate, DateStyle.YYYYMMDD));
            Long start_date_ = Long.parseLong(DateUtil.DateToString(startDate, DateStyle.YYYYMMDD));
            Long cur_date_ = Long.parseLong(DateUtil.DateToString(curDate, DateStyle.YYYYMMDD));

            //            if (repay_date_ < cur_date_ && period != max_period)
            //                continue;

            // 计算待还本金
            double dhbj_ = DoubleUtil.sub(corpus, actual_money);

            // 计算逾期天数
            int yq_days = DateUtil.getYsYqDays(curDate, DateUtil.addDay(lastLateTime, -1));

            int cur_period_actual_lengths = 0;
            if (yq) {
                if (period == max_period) {
                    hkbj = bj;
                    repayId = repay_id;
                    start_date = startDate;
                    yqDays = yq_days;
                    // 计算应收逾期利息
                    double ysyqlx = scfDebtRepayService.queryCalYqLx(repay_id, curDate);
                    double yqqx = DoubleUtil.sub(ysyqlx, fee_corpus);
                    yqbjlx = DoubleUtil.add(yqbjlx, yqqx);

                    /*dhlx = interest;

                    more_interest = actual_interest;*/
                }
            } else {
                double pre_cslx = 0d;

                if (i < list.size() - 1) {
//                    pre_cslx = InterestUtil.createBorrowInterest(lengths, userLoan.getInvestRate(),
//                        loan_money, 360);
                    pre_cslx = loan_money * lengths * userLoan.getInvestRate() / 360;
                    forlastmoney = DoubleUtil.add(forlastmoney, pre_cslx);
                    originalInterest += loan_money * lengths * userLoan.getInvestRate() / 360;
                    forcountdhlx = originalInterest;
                    pre_cslx = DoubleUtil.round(pre_cslx, 2);
                } else {
                    originalInterest += loan_money * lengths * userLoan.getInvestRate() / 360;
                    pre_cslx = originalInterest - forlastmoney;
                    pre_cslx = DoubleUtil.round(pre_cslx, 2);
                }
                if (repay_date_ < cur_date_) {

                } else if (cur_date_ >= start_date_ && cur_date_ <= repay_date_) {
                    tqhkbj = bj;
                    repayId = repay_id;
                    start_date = startDate;
                    yqDays = yq_days;
                    // 计算出提前还本金的时候催收利息
                    double cslx = DoubleUtil.sub(interest, pre_cslx);

                    //催收利息中加入手续费
                    //                    double calculateServiceCharge = ServiceChargeUtil.getServiceCharge(bj);
                    //                    lessPayMoney = DoubleUtil.add(lessPayMoney, calculateServiceCharge);

                    lessPayMoney = DoubleUtil.add(lessPayMoney, cslx);
                    cur_period_actual_lengths = DateUtil.getIntervalDays(curDate, startDate) + 1;
                    //                    dhlx = InterestUtil.createBorrowInterest(cur_period_actual_lengths,
                    //                        userLoan.getInvestRate(), bj, 360);
                    dhlx = forcountdhlx + bj * cur_period_actual_lengths * userLoan.getInvestRate()
                            / 360 - forlastmoney;
                    dhlx = DoubleUtil.round(dhlx, 2);
                    lessPayMoney = DoubleUtil.add(lessPayMoney, dhlx);
                    //dhlx = DoubleUtil.add(dhlx, cslx);
                    more_interest = DoubleUtil.add(more_interest, actual_interest);
                } else {
                    more_interest = DoubleUtil.add(more_interest, actual_interest);
                }
            }

        }
        Double op_money = 0d;
        Double m_money = 0d;

        if (yq) {
            if (yqbjlx > 0) {
                op_money = DoubleUtil.add(hkbj, tqhkbj, yqbjlx);
            } else {

                op_money = DoubleUtil.add(hkbj, tqhkbj);
                yqbjlx = 0d;
            }
        } else {
            m_money = DoubleUtil.sub(more_interest, lessPayMoney);
            if (m_money >= 0) {
                //如果有多还的钱,就不用去计算利息
                op_money = DoubleUtil.add(hkbj, tqhkbj);
                dhlx = 0d;
                m_money = more_interest;
            } else {
                //如果当期有少还的利息,余额扣除时还需要增加需还的利息
                op_money = DoubleUtil
                        .sub(DoubleUtil.add(lessPayMoney, hkbj, tqhkbj), more_interest);
                m_money = lessPayMoney;
//                m_money = 0d;
                dhlx = DoubleUtil.sub(lessPayMoney, more_interest);
                //lessPayMoney = dhlx;
            }
        }

        // 如果当前所在期产生任何费用的,或者,提前还的本金所在当前期内的,都需要生成明细
        if (yqbjlx > 0 || dhlx > 0 || hkbj > 0 || tqhkbj > 0) {
            ScfDebtRepayDetail detail = init(repayId, dhlx, hkbj, m_money, 0, 0, yqbjlx, yqDays,
                    start_date, curDate, dataSource, userId, op_money, sign, 0, curDate, tqhkbj,
                    bj_use_last_money, lessPayMoney, 0d);
            detailList.add(detail);
        }
        return detailList;
    }

    /**
     *
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年11月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在项目中遇到一个bug,追进接口后,发现这个接口调用了超过10个大方法,下面是其中一个,我觉得可以作为一个教科书级别的典范。我觉得有志于把代码写的优雅的朋友,可以仔细看看这个代码,把这个代码的问题看明白了,那代码自然就写的简洁优雅了。
  • 这是一段历史代码,这个代码是谁写的不重要。重要的是,它暴露出了一些典型的问题,值得让人思考和借鉴,这才是这个文章的目的。(毕竟每个程序员,都写过很不符合规范的代码,可以理解这个现象。)
  • 几个月前,也是在项目中看到了大量类似代码,当时任务比较重,低质量的代码导致项目进度完全无法按照预期进度进行,因为你不知道哪个地方,就会遇到下面这样一个方法,直接导致一个人半天甚至一天的时间就浪费了。我当时写了篇文章言辞比较激烈低端程序员的明显特征,还被博友喷了,现在看来,文章的看似偏激的观点,是值得思考的。
  • 作为一个程序员,科班,还是培训,还是自学,希望你尊重程序员这个职业,也希望你拿着工资干活时,能把活干漂亮点,跟你一起加班熬夜上线的兄弟,你别坑他们。代码质量好,也是一种能力。
  • 时间紧,任务重,加班严重,每个程序员都会遇到这些问题,但是,还是希望,都能把代码写的好一点,坚守一个质量底线,利人利己。
  • 每个基层的程序员,应该都不希望在基层写一辈子代码,如果你是leader,你希望你的兄弟们写出什么样的代码?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档