小波分解与小波包分解代码_分解的功能参数和代码可维护性

小波分解与小波包分解代码

Code keeps changing, there’s no doubt about that. We always do our best to set some rock solid code foundations when a new project starts, we even have one or two favorite functions that become the cornerstone of our beautiful program. “This function will be my rod when I walk through the valley of the shadow of death.” But requirements change, and now your function should not just be the rod, but the rod and the staff as well.

代码不断变化,这毫无疑问。 当一个新项目开始时,我们总是尽力为某些基础代码奠定坚实的基础,我们甚至拥有一个或两个最喜欢的函数,这些函数成为了我们漂亮程序的基石。 “当我走过死亡阴影的山谷时,这就是我的职责。” 但是需求发生了变化,现在您的功能不仅应该是杆,还应该是杆人员。

Let’s take a walk.

让我们出去走走。

转1 (Turn 1)

Let’s imagine a MUD written in Node.js. At some point, we will be implementing melee combat in our game. And you write a function you are very much in love with; it’s a function to calculate the damage inflicted by a melee weapon. For the sake of storytelling, we don’t really care much about the contents of the function. We assume you just love it.

让我们想象一下用Node.js编写的MUD 。 在某个时候,我们将在游戏中实施近战。 并且您编写了一个非常喜欢的功能; 它是计算近战武器造成的伤害的函数。 为了讲故事,我们并不十分在意函数的内容。 我们假设您只是喜欢它。

/**
 * Calculates the damage of a melee attack.
 *
 * @param {object} weapon - Weapon used in the attack.
 * @returns {number} Damage inflicted by <code>weapon</code>.
 */
function calculateWeaponDamage(weapon) {
    return weapon.roll();
}

转2 (Turn 2)

After a couple of days, a game mechanics designer asks you to make some changes in the function because the weapon damage calculation should optionally take into account the physical defenses of the player who is being attacked. Turns out this function is being used both to display average damages in the UI, and to actually calculate the damage in-game when used against a real player. So, sometimes there will be a defender, and sometimes not.

几天后,游戏机制设计人员要求您对功能进行一些更改,因为武器损坏计算应选择考虑被攻击玩家的物理防御能力。 事实证明,此功能既可用于在UI中显示平均伤害,也可用于在对付真实玩家时实际计算游戏中的伤害。 因此,有时会有辩护人,有时没有。

Well, fine! I guess we can add a new argument to the function. We can even try to make the function signature backwards compatible by checking whether the new argument is being passed on or not, so that we do not have to go around refactoring all the usages of calculateWeaponDamage.

好吧,很好! 我想我们可以向该函数添加新的参数。 我们甚至可以尝试通过检查是否传递了新参数来使功能签名向后兼容,这样我们就不必重新解析calculateWeaponDamage所有用法。

/**
 * Calculates the damage of a melee attack.
 *
 * @param {object} weapon - Weapon used in the attack.
 * @param {object} [defender] - Player who receives the attack.
 * @returns {number} Damage inflicted by <code>weapon</code> on <code>defender</code>.
 */
function calculateWeaponDamage(weapon, defender) {
    let tmpDamage = weapon.roll();
    if (defender) {
        tmpDamage -= defender.stat('defense');
    }
    return tmpDamage;
}

Settled down for now, aren’t we?

现在定居下来,不是吗?

转3 (Turn 3)

Hey! We are giving players with extreme strength some melee damage bonus. Do you think you can take this into account in your beloved calculateWeaponDamage function? All developers using your fu

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值