リーダブルコード実践 第二弾 ~Vol.2~

前回書いたコードに対し、まずは見た目を見やすくします。

//商品クラス
var CartItem = function(cart_no, goods_id, unit_price, goods_num, tax_rate, tax, price) {
    this.cart_no    = cart_no;
    this.goods_id   = goods_id;
    this.unit_price = unit_price;
    this.goods_num  = goods_num;
    this.tax_rate   = tax_rate;
    this.tax        = tax;
    this.price      = price;
}

//カートクラス
var Cart = function() {
    this.latest_cart_no = 0;
    this.cart_items     = [];

    //商品をカートに追加する。
    this.Add = function(goods_id, unit_price, goods_num) {
        if (goods_id === "") {
            this.ShowError("商品を指定してください。");
            return;
        }
        if (goods_num < 1) {
            this.ShowError("数量は1以上を入力してください。");
            return;
        }

        this.latest_cart_no++;
        var cart_item = new CartItem();

        cart_item.cart_no    = this.latest_cart_no;
        cart_item.goods_id   = goods_id;
        cart_item.unit_price = unit_price;
        cart_item.goods_num  = goods_num;

        if (Date.now() >= "2019/10/01") {
            if (!this.IsReducedTaxRate(goods_id))
                cart_item.tax_rate = 0.1;
            else
                cart_item.tax_rate = 0.08;
        }
        else {
            cart_item.tax_rate = 0.08;
        }

        cart_item.tax   = ((unit_price * goods_num) * cart_item.tax_rate);
        cart_item.price = ((unit_price * goods_num) + cart_item.tax);

        this.cart_items[this.cart_items.length] = cart_item;
    }

    //商品の数量を変更する。
    this.ChangeGoodsNum = function(cart_no, goods_num) {
        if (cart_no === "") {
            this.ShowError("商品が存在しません。");
            return;
        }
        if (!this.ExistNo(cart_no)) {
            this.ShowError("商品が存在しません。");
            return;
        }
        if (goods_num < 1) {
            this.ShowError("数量は1以上を入力してください。");
            return;
        }

        //数量を変更したら消費税・金額を再計算する。
        for (var i = 0; i < this.cart_items.length; i++) {
            if (this.cart_items[i].cart_no === cart_no) {
                this.cart_items[i].goods_num = goods_num;
                this.cart_items[i].tax       = ((this.cart_items[i].unit_price * goods_num) * this.cart_items[i].tax_rate);
                this.cart_items[i].price     = ((this.cart_items[i].unit_price * goods_num) + this.cart_items[i].tax);
                break;
            }
        }
    }

    //カートの商品を削除する。
    this.Remove = function(cart_no) {
        if (cart_no === "") {
            this.ShowError("商品が存在しません。");
            return;
        }
        if (!this.ExistNo(cart_no)) {
            this.ShowError("商品が存在しません。");
            return;
        }

        for (var i = 0; i < this.cart_items.length; i++) {
            if (this.cart_items[i].cart_no === cart_no) {
                this.cart_items.splice(i, 1);
                break;
            }
        }
    }

    //合計金額を3桁カンマ区切りにして返す。
    //カートに商品がない場合は「¥0」を返す。
    this.TotalPrice = function() {
        if (this.cart_items.length === 0)
            return "¥0";

        var total_price = 0;

        for (var i = 0; i < this.cart_items.length; i++)
            total_price += this.cart_items[i].price;

        return "¥" + String(total_price).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    }

    //指定したカートNo.が存在するかチェックする。
    this.ExistCartNo = function(cart_no) {
        for (var i = 0; i < this.cart_items.length; i++) {
            if (this.cart_items[i].cart_no === cart_no)
                return true;
        }
        return false;
    }

    //指定した商品が軽減税率対象商品かチェックする。
    this.IsReducedTaxRate = function(goods_id) {
        if (goods_id.substr(0, 3) === "RTR")
            return true;
        else
            return false;
    }

    //エラーメッセージを表示する。
    this.ShowError = function(msg) {
        console.log("Error: " + msg);
    }
}

<変更点>
- 変数名「no」や「id」など、抽象的な名前を具体的な名前「cart_no」や「goods_id」などに変更しました。
- コメントは名前からわかるものは削除して「何を行うか」を記述するよう変更しました。
- 中身が1行のブロックの波カッコ「{}」を省略しました。一部のコードはOneLinerにしました。
- 関連のあるコードは縦位置を揃えました。

名前が明確になって個々のコードは読みやすくなりました。が、重複したコードや本題とは関係ないコードまで入り混じっているので、コード全体はまだまだ読みにくいです。次回はロジックを書き直して読みやすくします。

この記事が気に入ったらサポートをしてみませんか?