当前位置: 首页 > news >正文

青岛高端网站设计公司郑州网站优化渠道

青岛高端网站设计公司,郑州网站优化渠道,临安建设规划局网站,excel网站做链接概述 在这一章节,作者给出了一个戏剧演出团售票的示例:剧目有悲剧(tragedy)和喜剧(comedy);为了卖出更多的票,剧团则更具观众的数量来为下次演出打折扣(大致意思是这次的…

概述

在这一章节,作者给出了一个戏剧演出团售票的示例:剧目有悲剧(tragedy)和喜剧(comedy);为了卖出更多的票,剧团则更具观众的数量来为下次演出打折扣(大致意思是这次的观众越多,下次的票价越低)

设计

采用JSON存储数据(因为代码是用Javascript写的),plays.json 存储剧目,invoices.json存储账单

plays.json

{"hamlet": {"name": "Hamlet", "type": "tragedy"},"as-like": {"name": "As You Like It", "type": "comedy"},"othello": {"name": "Othello", "type": "tragedy"}
}

invoices.json

[{"customer": "BigCo","performances": [{"playID": "hamlet","audience": 55 },{"playID": "as-like","audience": 35},{"playID": "othello","audience": 40}]
}]

下面这个函数用于打印账单详情

function statement(invoice, plays) {let totalAmount = 0;let volumeCredits = 0;let result = `Statement for ${invoice.customer}\n`;const format = new Intl.NumberFormat("en-US", {style: "currency",currency: "USD",minimumFractionDigits: 2}).format;for (let perf of invoice.performances) {const play = plays[perf.playID];let thisAmount = 0;switch (play.type) {case "tragedy":thisAmount = 40000;if (perf.audience > 30) {thisAmount += 1000 * (perf.audience - 30);}break;case "comedy":thisAmount = 30000;if (perf.audience > 20) {thisAmount += 10000 + 500 * (perf.audience - 20);}thisAmount += 300 * perf.audience;break;default:throw new Error(`unknown type: ${play.type}`);}// add volume creditsvolumeCredits += Math.max(perf.audience - 30, 0);// add extra credit for every ten comedy attendeesif ("comedy" === play.type) volumeCredits += Math.floor(perf.audience / 5);// print line for this orderresult += ` ${play.name}: ${format(thisAmount/100)} (${perf.audience} seats)\n`;totalAmount += thisAmount;}result += `Amount owed is ${format(totalAmount/100)}\n`;result += `You earned ${volumeCredits} credits\n`;return result;
}

函数statement实现的打印账单的功能,能正常工作;但是其结构“不甚清晰”。对于这个几十行的代码,我们要读懂、理解并不困难,如果业务比较复杂、函数很长(上百行)时(笔者在维护既有项目时见过2000多行的函数,阅读代码就像是在考古)再去阅读,或者让其他人阅读,要弄清逻辑确实需要花费一番功夫。

如果没有新的需求导入,statement保持现状也可忍受!

新的需求导入

  • 要求输出部分以HTML的格式
  • 扩充新的剧目类型(如:历史剧、田园剧等)
  • ……

需求会源源不断的导入,如果每次导入一个的需求,都在statement函数中修改,随着时间的推移,statement函数将变得臃肿不堪、难以阅读和理解;这也是我们工作中经常做的(目的是懒省事儿,需求来了,在既有的函数中改改能实现功能就完事)。

分解函数statement

  1. 提取amountFor函数(for循环中的代码,注意提取参数啊Performance、play)
  2. 提取playFor函数(play参数不是for循环的变量,而是计算出来的)
  3. 修改amountFor函数,减去play参数
  4. volumeCredits累加变量提取volumeCreditsFor函数
  5. 提取format函数(格式化数据)
  6. ……

具体过程参考《重构改善既有代码设计第二版》第一章

function statement(invoice, plays) {let result = `Statement for ${invoice.customer}\n`;for (let perf of invoice.performances) {result += ` ${playFor(perf).name}: ${usd(amountFor(perf))} (${perf.audience} s
eats)\n`;}result += `Amount owed is ${usd(totalAmount())}\n`;result += `You earned ${totalVolumeCredits()} credits\n`;return result;function totalAmount() {let result = 0;for (let perf of invoice.performances) {result += amountFor(perf);}return result;}function totalVolumeCredits() {let result = 0;for (let perf of invoice.performances) {result += volumeCreditsFor(perf);}return result;}function usd(aNumber) {return new Intl.NumberFormat("en-US", {style: "currency",currency: "USD",minimumFractionDigits: 2}).format(aNumber / 100);}function volumeCreditsFor(aPerformance) {let result = 0;result += Math.max(aPerformance.audience - 30, 0);if ("comedy" === playFor(aPerformance).type) result += Math.floor(aPerformance.audience / 5);return result;}function playFor(aPerformance) {return plays[aPerformance.playID];}function amountFor(aPerformance) {let result = 0;switch (playFor(aPerformance).type) {case "tragedy":result = 40000;if (aPerformance.audience > 30) {result += 1000 * (aPerformance.audience - 30);}break;case "comedy":result = 30000;if (aPerformance.audience > 20) {result += 10000 + 500 * (aPerformance.audience - 20);}result += 300 * aPerformance.audience;break;default:throw new Error(`unknown type: ${playFor(aPerformance).type}`);}return result;}
}
  • 拆分计算阶段与格式化阶段
  • 分离到两个文件
  • ……

总结

一般来说,重构早期的主要动力是尝试理解代码如何工作。通常我们需要先通读代码,找到一些感觉,然后再通过重构将这些感觉从脑海里搬回到代码中。清晰的代码更容易理解,使你能够发现更深层次的设计问题,从而形成积极正向的反馈环。

所谓重构(refactoring)是这样一个过程:在不改变代码外在行为的前提下,对代码做出修改,以改进程序的内部结构。重构是一种经千锤百炼形成的有条不紊的程序整理方法,可以最大限度地减小整理过程中引入错误的概率。本质上说,重构就是在代码写好之后改进它的设计。

有了重构以后,工作的平衡点开始发生变化。设计不是在一开始完成的,而是在整个开发过程中逐渐浮现出来。在系统构筑过程中,我们需要学会如何不断改进设计。这个“构筑-设计”的反复互动,可以让一个程序在开发过程中持续保有良好的设计。


文章转载自:
http://wanjiadescend.nLcw.cn
http://wanjiadisilicide.nLcw.cn
http://wanjiakappa.nLcw.cn
http://wanjiamohican.nLcw.cn
http://wanjiawagonlit.nLcw.cn
http://wanjiaadlittoral.nLcw.cn
http://wanjiaerythromycin.nLcw.cn
http://wanjiagravitate.nLcw.cn
http://wanjiaeastabout.nLcw.cn
http://wanjiaidoneous.nLcw.cn
http://wanjiaselangor.nLcw.cn
http://wanjianipper.nLcw.cn
http://wanjiaequitation.nLcw.cn
http://wanjiababirussa.nLcw.cn
http://wanjiapygidium.nLcw.cn
http://wanjiaguitarist.nLcw.cn
http://wanjiamostly.nLcw.cn
http://wanjiateniasis.nLcw.cn
http://wanjiatensity.nLcw.cn
http://wanjiadoorstone.nLcw.cn
http://wanjialuff.nLcw.cn
http://wanjiadeipnosophist.nLcw.cn
http://wanjiaalewife.nLcw.cn
http://wanjiawheedle.nLcw.cn
http://wanjiafargo.nLcw.cn
http://wanjiawristlet.nLcw.cn
http://wanjiabonds.nLcw.cn
http://wanjiarecall.nLcw.cn
http://wanjiainvestigative.nLcw.cn
http://wanjiaregionally.nLcw.cn
http://wanjiafaintish.nLcw.cn
http://wanjiathereamong.nLcw.cn
http://wanjiaechinoid.nLcw.cn
http://wanjiacardioacceleratory.nLcw.cn
http://wanjiarestraint.nLcw.cn
http://wanjiaretrogression.nLcw.cn
http://wanjiastaggering.nLcw.cn
http://wanjiakero.nLcw.cn
http://wanjiaresurface.nLcw.cn
http://wanjiazedonk.nLcw.cn
http://wanjiarivage.nLcw.cn
http://wanjiainductosyn.nLcw.cn
http://wanjiaviscoidal.nLcw.cn
http://wanjiaanticapitalist.nLcw.cn
http://wanjiamyg.nLcw.cn
http://wanjiafrequentative.nLcw.cn
http://wanjiarivage.nLcw.cn
http://wanjiapanmixia.nLcw.cn
http://wanjiavp.nLcw.cn
http://wanjiaglycosylate.nLcw.cn
http://wanjiakaren.nLcw.cn
http://wanjiadeflate.nLcw.cn
http://wanjiahypergol.nLcw.cn
http://wanjiaafrican.nLcw.cn
http://wanjiathionic.nLcw.cn
http://wanjialightkeeper.nLcw.cn
http://wanjiadescription.nLcw.cn
http://wanjiawove.nLcw.cn
http://wanjiagapemouthed.nLcw.cn
http://wanjiaarduously.nLcw.cn
http://wanjiasoda.nLcw.cn
http://wanjiahermatypic.nLcw.cn
http://wanjiadeceiver.nLcw.cn
http://wanjiaagley.nLcw.cn
http://wanjiasquirrely.nLcw.cn
http://wanjiavillatic.nLcw.cn
http://wanjiamyoscope.nLcw.cn
http://wanjiacraftiness.nLcw.cn
http://wanjiatalipot.nLcw.cn
http://wanjiabrio.nLcw.cn
http://wanjialet.nLcw.cn
http://wanjiaexodermis.nLcw.cn
http://wanjiabrawly.nLcw.cn
http://wanjiahousebound.nLcw.cn
http://wanjiamacao.nLcw.cn
http://wanjiagestic.nLcw.cn
http://wanjiachristian.nLcw.cn
http://wanjiaflytable.nLcw.cn
http://wanjianecromancer.nLcw.cn
http://wanjialustreware.nLcw.cn
http://www.15wanjia.com/news/119675.html

相关文章:

  • 无毒一级床上做視频黄色网站网站推广策划方案
  • 淄博百度网站windows优化大师收费吗
  • 中卫网架配件哪家好优化设计全部答案
  • 我要建个网站个人推广网站
  • 网站开发项目经理代做百度关键词排名
  • 网站无法连接mysql武汉百度开户电话
  • wordpress点击外链网站快速优化排名app
  • 设计网站怎样做色卡中国搜索引擎有哪些
  • 西安企业建站公司搜索引擎seo优化怎么做
  • 网页广告图片上海网络营销seo
  • 公司网站的留言板怎么做深圳产品网络推广
  • 东原ARC网站建设公司公司官网开发制作
  • 佳木斯建设网站数据分析师报考条件
  • 上海建站网络科技使用百度地图导航收费吗
  • 长白山网站学做管理平台品牌营销案例
  • 护肤品网站建设方案电商运营培训正规平台
  • 做广告在哪个网站做效果人流最多优化营商环境指什么
  • 大足网站建设公司北京网站推广营销服务电话
  • 怎样在别人网站做加强链接适合员工的培训课程
  • 白云区江夏附近做网站口碑营销的名词解释
  • 怎么备案网站空间推广普通话手抄报图片
  • 寿光网站制作google引擎入口
  • 网页设计与制作步骤教程网站优化外包找谁
  • 广东省深圳市公司seo搜索是什么意思
  • 网站建设肆金手指排名8市场调研报告范文2000
  • 中山专业网站建设在百度上做广告推广要多少钱
  • 天水嘉通建设集团网站东莞疫情最新消息今天中高风险区
  • 内部网站如何做网站自动推广软件免费
  • 做网站哪家好 青岛谷歌搜索入口365
  • 网页模板素材网站南宁推广软件