2

有没有办法简化这个或更简洁的形式我可以使用?所包含的逻辑都是正确的。它似乎有很多returns 和else ifs。

mode = (function(mode, current, proposed, origins, destinations) {
            if (mode === 'none') {
                return 'project';
            } else if (proposed.count === 0) {
                return 'unseated';
            } else if (current.count > proposed.count && proposed.count > 0) {
                return 'reducing';
            } else if (proposed.count === destinations.count && destinations.count > 1 && current.count === 0) {
                return 'newplus';
            } else if (proposed.count === destinations.count && current.count === 0) {
                return 'new';
            } else if (proposed.count > destinations.count) {
                return 'increasing';
            } else if (proposed.count === destinations.count && destinations.count > origins.count) {
                return 'moveplus';
            } else {
                return 'move';
            }
        }(moves.register[staff].move, {count: mode.currentDesks}, {count: mode.proposedDesks}, {count: mode.origins}, {count: mode.destinations})));

我之前使用了一个嵌套三元,它略微缩短,但我认为更容易出错且难以阅读(由于代码演变而不是复制中的错误,结果略有不同):

mode = 
(moves.register[staff].move === 'none') ? 'project' :
    (mode.proposedDesks === 0) ? 'unseated' :
        (mode.currentDesks > mode.proposedDesks && mode.proposedDesks > 0) ? 'reducing' :
            (mode.proposedDesks === mode.destinations && mode.destinations > 1 && mode.currentDesks === 0) ? 'newplus' :
                (mode.proposedDesks === mode.destinations && mode.currentDesks === 0) ? 'new' :
                    (mode.proposedDesks > mode.destinations) ? 'additional' :
                        (mode.proposedDesks === mode.destinations && mode.destinations === mode.origins) ? 'move' :
                            (mode.proposedDesks === mode.destinations && mode.destinations > mode.origins) ? 'moveplus' :
                                'other';

因此,虽然我喜欢if…then…elseif堆栈的易读性,但感觉它比它可能的更冗长。switch…case由于比较变量的数量,我不认为我正在寻找一个版本并不能完全削减它,并且将if语句嵌套在 aswitch…case或 a 内的三元运算符中感觉不对if…then…else

我本能地认为我想要一种矩阵形式,其中返回值在网格中,并且以某种方式对各种按位条件进行矩阵计算返回了正确的结果。我怀疑这将是紧凑代码胜过易读性的胜利。

有什么建议么?

注意。选择变量名称,包括向每个变量添加计数属性,而不是像这样命名变量或不指示它是计数,是为了易读性而选择的。

4

1 回答 1

0

您可以使用 var 存储您的选择并在最后返回

    mode = (function(mode, current, proposed, origins, destinations) {
                var varName='move';
                if (mode === 'none') {
                    varName='project';
                } else if (proposed.count === 0) {
                    varName='unseated';
                } else if (current.count > proposed.count && proposed.count > 0) {
                    varName= 'reducing';
                } else if (proposed.count === destinations.count && destinations.count > 1 && current.count === 0) {
                    varName= 'newplus';
                } else if (proposed.count === destinations.count && current.count === 0) {
                    varName='new';
                } else if (proposed.count > destinations.count) {
                    varName= 'increasing';
                } else if (proposed.count === destinations.count && destinations.count > origins.count) {
                    varName= 'moveplus';
                }             
}(moves.register[staff].move, {count: mode.currentDesks}, {count: mode.proposedDesks}, {count: mode.origins}, {count: mode.destinations})));
于 2013-06-28T10:07:11.600 回答