【问题标题】:Applying DRY principles to JavaScript, help me optimize this code?将 DRY 原则应用于 JavaScript,帮我优化这段代码?
【发布时间】:2011-09-09 02:27:38
【问题描述】:

在寻找优化代码质量的方法时,我最终遇到了 DRY(不要重复自己)的概念。我尽可能地遵循这一点,但有时我会遇到必须编写两个几乎相同的函数的位置,除了 2 或 3 行代码之外,我在试图找出最好的方法时用光了时间整理一下。

所以这是我的“问题”。我在下面包含了我几周前编写的两个函数,除了末尾的 3 行之外,它们基本相同,一个通过加法制作动画,另一个通过减法制作动画。我很想从其他开发人员那里获得一些关于他们将如何优化以下代码的意见有一些不相关的代码示例,您解决了类似的问题。

/**
 * Go to the previous notification
 *
 * @private
 * @param {object} cl Click event details (ex. {id: 'linkId', ss: '_', index: '1', e: event})
 * @memberOf APP.devices
 */
function slideNext (cl) {
    var button = $('#' + cl.id + cl.ss + cl.index),
        index = cl.index - 1,
        slider = devices[index].container.find('.slideContainer'),
        // In order to get the value of the 'right' position we must take the (container width - slider width - left position - right-margin)
        slidePos = (slider.parent().width() - slider.width()) + (slider.position().left * -1) + (parseFloat(slider.css('margin-right')) * -1);
    if (button.hasClass('disabled')) {
        return false;
    }
    slider.find('.active').removeClass('active').prev().addClass('active');
    disableButtons(index);
    slider.animate({'right': slidePos + notificationOffset}, 200, function () {
        determineButtonState(index);
    });
    updatePositionContext(index);
}


/**
 * Advance to the next notification
 *
 * @private
 * @param {object} cl Click event details
 * @memberOf APP.devices
 */
function slidePrev (cl) {
    var button = $('#' + cl.id + cl.ss + cl.index),
        index = cl.index - 1,
        slider = devices[index].container.find('.slideContainer');
        // In order to get the value of the 'right' position we must take the (container width - slider width - left position - right-margin)
        slidePos = (slider.parent().width() - slider.width()) + (slider.position().left * -1) + (parseFloat(slider.css('margin-right')) * -1);
    if (button.hasClass('disabled')) {
        return false;
    }
    slider.find('.active').removeClass('active').next().addClass('active');
    disableButtons(index);
    slider.animate({'right': slidePos - notificationOffset}, 200, function () {
        determineButtonState(index);
    });
    updatePositionContext(index);
    // Load more notifications once user get's close to the end of the current set of notifications
    if (slider.find('.active').nextAll().length == 3) {
        getMoreNotifications(index);
    }
}

【问题讨论】:

    标签: javascript refactoring dry code-duplication


    【解决方案1】:

    使用基本标志,您几乎可以将其全部消除。我敢肯定有一个很好的理由让我想念你为什么没有这样做,但我从来没有在 DRY 上做得非常大。欢迎赐教:)

    /**
     * Move to another notification
     *
     * @private
     * @param {object} cl Click event details (ex. {id: 'linkId', ss: '_', index: '1', e: event})
     * @param fw Whether to go forwards or backwards. Defaults to true (forwards)
     * @memberOf APP.devices
     */
    function slideNext (cl, fw) {
        var button = $('#' + cl.id + cl.ss + cl.index),
            index = cl.index - 1,
            slider = devices[index].container.find('.slideContainer'),
            // In order to get the value of the 'right' position we must take the (container width - slider width - left position - right-margin)
            slidePos = (slider.parent().width() - slider.width()) + (slider.position().left * -1) + (parseFloat(slider.css('margin-right')) * -1);
            var distance = ((fw) ? slidePos + notificationOffset : slidePos - notificationOffset;
        if (button.hasClass('disabled')) {
            return false;
        }
        if (fw)
            slider.find('.active').removeClass('active').prev().addClass('active');
        else
            slider.find('.active').removeClass('active').next().addClass('active');
        disableButtons(index);
        slider.animate({'right': distance}, 200, function () {
            determineButtonState(index);
        });
        updatePositionContext(index);
        // Load more notifications once user get's close to the end of the current set of notifications
        if (!fw && slider.find('.active').nextAll().length == 3) {
            getMoreNotifications(index);
        }
    }
    

    【讨论】:

    • 我猜没有充分的理由,我认为这是有道理的。更感兴趣的是看到其他人采取的不同方法,但我很可能最终会做你建议的事情。谢谢。
    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多