Skip to content

Instantly share code, notes, and snippets.

@MartinKei
Last active May 9, 2017 07:39
Show Gist options
  • Save MartinKei/372a3535568850351b94f50ced48a0f0 to your computer and use it in GitHub Desktop.
Save MartinKei/372a3535568850351b94f50ced48a0f0 to your computer and use it in GitHub Desktop.
Short summary of 2nd review

Second Code-Review

Almost all issues are gone now. Except the usage of any in the channel service nothing serious is left. The channel service looks like as if it was overlooked. The other services look just fine.

any usage where types would make sense

  • channel.service.ts

Usage of inner functions

If inner functions are used as regular private methods and even rely on the usage of call, it makes more sense to define them as private methods.

E.g. in the promotion service (promotion.service.ts) we've got this method:

private preprocessPromotionReceived(promotion: Promotion): Promotion {
    const copy: Promotion = JSON.parse(JSON.stringify(promotion));

    preprocessRestrictions.call(this, copy.restrictions);
    preprocessAdjustments.call(this, copy.adjustments);

    if (copy.overrides === null) {
        copy.overrides = [];
    } else {
        copy.overrides.forEach(override => preprocessRestrictions.call(this, override.restrictions));
    }

    if (!copy.description) {
        copy.description = {};
    }

    return copy;

    function preprocessRestrictions(restriction: PromotionRestriction) {
        if (restriction.los) {
            this.typeLosRestrictions.forEach((key) => {
                if (restriction.los && restriction.los[key] === 0) {
                    restriction.los[key] = null;
                }
            });
        }

        if (restriction.deviceTypeFencing === null) {
            restriction.deviceTypeFencing = '';
        }

        Object.keys(this.typeRestrictions).forEach(key => {
            // Add empty types
            if (!restriction[key] || !restriction[key].type) {
                restriction[key] = { type: '' };
                restriction[key][this.typeRestrictions[key]] = [];
            }

            if (this.typeRestrictions[key] === 'ranges') {
                // Sort ascending
                restriction[key][this.typeRestrictions[key]].sort((range1, range2) => {
                    if (range1.from > range2.from) {
                        return 1;
                    } else if (range1.from < range2.from) {
                        return -1;
                    } else {
                        return 0;
                    }
                });
            }
        });
    }

    function preprocessAdjustments(adjustment: PromotionAdjustment) {
        Object.keys(this.typeAdjustments).forEach(key => {
            // Add empty types
            if (!adjustment[key] || !adjustment[key].type) {
                adjustment[key] = { type: '' };
                adjustment[key][this.typeAdjustments[key]] = null;
            }
        });

        if (adjustment.mealPlanCode === null) {
            adjustment.mealPlanCode = 0;
        }
    }
}

This is completely equivalent with:

private preprocessPromotionReceived(promotion: Promotion): Promotion {
    const copy: Promotion = JSON.parse(JSON.stringify(promotion));

    preprocessRestrictions(copy.restrictions);
    preprocessAdjustments(copy.adjustments);

    if (copy.overrides === null) {
        copy.overrides = [];
    } else {
        copy.overrides.forEach(override => preprocessRestrictions.call(this, override.restrictions));
    }

    if (!copy.description) {
        copy.description = {};
    }

    return copy;
}

private preprocessRestrictions(restriction: PromotionRestriction) {
    if (restriction.los) {
        this.typeLosRestrictions.forEach((key) => {
            if (restriction.los && restriction.los[key] === 0) {
                restriction.los[key] = null;
            }
        });
    }

    if (restriction.deviceTypeFencing === null) {
        restriction.deviceTypeFencing = '';
    }

    Object.keys(this.typeRestrictions).forEach(key => {
        // Add empty types
        if (!restriction[key] || !restriction[key].type) {
            restriction[key] = { type: '' };
            restriction[key][this.typeRestrictions[key]] = [];
        }

        if (this.typeRestrictions[key] === 'ranges') {
            // Sort ascending
            restriction[key][this.typeRestrictions[key]].sort((range1, range2) => {
                if (range1.from > range2.from) {
                    return 1;
                } else if (range1.from < range2.from) {
                    return -1;
                } else {
                    return 0;
                }
            });
        }
    });
}

private preprocessAdjustments(adjustment: PromotionAdjustment) {
    Object.keys(this.typeAdjustments).forEach(key => {
        // Add empty types
        if (!adjustment[key] || !adjustment[key].type) {
            adjustment[key] = { type: '' };
            adjustment[key][this.typeAdjustments[key]] = null;
        }
    });

    if (adjustment.mealPlanCode === null) {
        adjustment.mealPlanCode = 0;
    }
}

The issue and the change is minor, but it's a bit more clear how the functions/methods should be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment