网站源码网址修改,求个网站2021年能用,青岛做网站建设定制,微信商城和微网站建设今天#xff0c;我想谈谈相当简单的事情。我不会发明什么#xff0c;但我在生产代码中经常看到这样的事情#xff0c;所以我不能回避这个话题。
我经常要解开多个复杂的 if else 结构。多余的缩进、过多的逻辑只会加深理解。首先#xff0c;这篇文章的主要目的是让代码更透…今天我想谈谈相当简单的事情。我不会发明什么但我在生产代码中经常看到这样的事情所以我不能回避这个话题。
我经常要解开多个复杂的 if else 结构。多余的缩进、过多的逻辑只会加深理解。首先这篇文章的主要目的是让代码更透明、更易读。不过在某些情况下还是必须使用这些操作符。
else 操作
例如我们有简单的用户处理程序
func handleRequest(user *User) {if user ! nil {showUserProfilePage(user)} else {showLoginPage()}
}如果没有提供用户则需要将收到的请求重定向到登录页面。If else 似乎是个不错的决定。但我们的主要任务是确保业务逻辑单元在任何输入情况下都能正常工作。因此让我们使用提前返回来实现这一点。
func handleRequest(user *User) {if user nil {return showLoginPage()} showUserProfilePage(user)
}逻辑是一样的但是下面的做法可读性会更强。
break 操作
对我来说Break 和 Continue 语句总是可以分解的信号。
例如我们有一个简单的搜索任务。找到目标并执行一些业务逻辑或者什么都不做。
func processData(data []int, target int) {for i, value : range data {if value target {performActionForTarget(data[i])break}}
}你应该始终记住使用 break 操作符并不能保证整个数组都会被处理。这对性能有好处因为我们丢弃了不必要的迭代但对代码支持和可读性不利。因为我们永远不知道程序会在列表的开头还是结尾停止。
在某些情况下带有子任务的简单功能可能会破坏这段代码。
func processData(data []int, target int, subtask int) {for i, value : range data {if value subtask {performActionForSubTarget(data[i])}if value target {performActionForTarget(data[i])break}}
}这样我们实际上可以拆出一个 find 的方法
func processData(data []int, target int, subTarget int) {found : findTarget(data, target)if found notFound {performActionForTarget(found)}found findTarget(data, subTarget)if found notFound {performActionForSubTarget(found)}
}const notFound -1func findTarget(data []int, target int) int {if len(data) 0 {return notFound}for _, value : range data {if value target {return value}}return notFound
}同样的逻辑但是拆分成更细粒度的方法也有精确的返回语句可以很容易地通过测试来实现。
continue 操作
该操作符与 break 类似。为了正确阅读代码您应该牢记它对操作顺序的具体影响。
func processWords(words []string, substring string) {for _, word : range words {if !strings.Contains(word, substring) {continue}// do some buisness logicperformAction(word)}
}Continue 使得这种简单的流程变得有点难以理解。
让我们写得更简洁些
func processWords(words []string, substring string) {for _, word : range words {if strings.Contains(word, substring) {performAction(word)}}
}关注公众号【爱发白日梦的后端】分享技术干货、读书笔记、开源项目、实战经验、高效开发工具等您的关注将是我的更新动力
文章转载自: http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.fpkpz.cn.gov.cn.fpkpz.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.krwzy.cn.gov.cn.krwzy.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.pngph.cn.gov.cn.pngph.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.ntqlz.cn.gov.cn.ntqlz.cn http://www.morning.jcwt.cn.gov.cn.jcwt.cn http://www.morning.rlhh.cn.gov.cn.rlhh.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.xysxj.com.gov.cn.xysxj.com http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.wrtpk.cn.gov.cn.wrtpk.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.llqch.cn.gov.cn.llqch.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.bpmmq.cn.gov.cn.bpmmq.cn http://www.morning.lrgfd.cn.gov.cn.lrgfd.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.mxhcf.cn.gov.cn.mxhcf.cn http://www.morning.nnykz.cn.gov.cn.nnykz.cn http://www.morning.qcsbs.cn.gov.cn.qcsbs.cn http://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.pluimers.cn.gov.cn.pluimers.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.xcnwf.cn.gov.cn.xcnwf.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.cmhkt.cn.gov.cn.cmhkt.cn http://www.morning.cjqqj.cn.gov.cn.cjqqj.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn