这个话题是关于什么的? (What is this topic about?)

If you are from a javascript background you might have heard the terms callback hell or async/await hell. It looks something like this:

如果您来自javascript背景,则可能已经听说过术语callback hellasync/await hell 。 看起来像这样:

There is a similar situation with just using if/else as well. You might label this as developers being obsessive, or ignore it by thinking that this is kind of okay in some situations.

仅使用if/else也存在类似情况。 您可以将其标记为开发人员沉迷,或通过认为在某些情况下还可以忽略它。

I beg to differ. As the saying goes…just pretend that whoever maintains your code next knows where you work and can come yell at you.

我不敢苟同。 俗话说……假装接下来维护您代码的人都知道您在哪里工作,并且可以大吼大叫。

For the purpose of this article, I’ll demonstrate an example using ReactJS. The principle itself can be applied in Javascript or any language for that matter.

出于本文的目的,我将演示一个使用ReactJS的示例。 原则本身可以用Java或任何语言应用。

Before we begin, the <MyButton /> example may not be the best example to explain the if/else nested problem. But hopefully it’ll give you a good guideline as to what the problem is & how to avoid it.

在开始之前<MyButton />示例可能不是解释if / else嵌套问题的最佳示例。 但希望它会为您提供有关问题的根源以及如何避免的良好指导。

Let’s paint a picture. You are given a button to implement in React & the button has 2 options for a theme, either default or primary. You think it’s simple & you write your <MyButton /> component:

让我们画一幅画。 您将获得一个在React实现的按钮,该按钮有2个主题选项,即defaultprimary 。 您认为这很简单,并编写了<MyButton />组件:

Some time passes & another developer is given a task to add functionality for round corners for the button for both themes, default & primary. The developer who picks up the tasks is very big on using ternary operators. They end up doing something like below:

一段时间过后,另一个开发人员被赋予一项任务,即为两个主题(默认主题和主要主题)的按钮添加圆角功能。 承担任务的开发人员在使用三元运算符方面非常重要。 他们最终会执行以下操作:

Time passes & another developer is given a task to add a hover state for both the default & primary buttons. Now the other developer does not want to make changes in the already code implemented, fearing they might end up breaking something.

时间过去了,另一个开发人员被赋予了为default按钮和primary按钮添加hover状态的任务。 现在,另一个开发人员不想更改已经实施的代码,因为担心它们可能最终会破坏某些内容。

So they write a separate if statement:

因此,他们编写了一个单独的if语句:

So far so good …

到目前为止,一切都很好 …

这是有趣的地方 (This is where it gets interesting)

Moving on, a final requirement comes in months later to add an animation when the user hovers over a button which has a primary theme & is of rounded type.

继续前进,几个月后,当用户将鼠标悬停在具有主要主题且为圆形的按钮上时,最终要求添加动画。

Now based on this requirement, the entire API structure changes the <MyButton/> component. The developer working on the code ends up with logic like this:

现在,基于此要求,整个API结构将更改<MyButto n />组件。 开发代码的开发人员最终会遇到如下逻辑:

That got out of hand way too quickly …. didn’t it?

那太不可收拾了……。 不是吗

In order to make this code simpler, we need to understand all the possible states that this code has. I have made a possibility chart of all the possible combinations at a certain time for the button.

为了简化此代码,我们需要了解该代码具有的所有可能状态。 我在某个时间为按钮制作了所有可能组合的可能性图。

If this seems a bit complicated, you can try looking at this next chart for your understanding.

如果这看起来有些复杂,您可以尝试查看下一张图表以了解您的情况。

The key thing when writing code is understanding the data flow of your code. Once you have a complete understanding of it, everything becomes simpler.

编写代码时的关键是理解代码的数据流。 一旦完全了解它,一切都会变得简单。

解 (Solution)

Based on the above given criteria, I can write my code like this to simplify it.

基于以上给出的条件,我可以像这样编写代码以简化代码。

This code is now way more readable. Any developer who works on this code can easily extend its functionality & get on with their life, knowing that they have done a wonderful job with the code.

现在,此代码更具可读性。 任何从事此代码工作的开发人员都可以轻松地扩展其功能并继续他们的生活,因为他们知道自己在代码方面做得很出色。

You can try playing with the code if you want, to see if it matches all the use cases.

您可以根据需要尝试使用该代码,以查看其是否与所有用例匹配。

With the automata (finite state machines)-like coding approach:

使用类似于自动机(有限状态机)的编码方法:

  • Code is more readable now现在代码更易读
  • Code is more maintainable代码更易于维护

Feel free to share your thoughts. Thank you for reading.

随时分享您的想法。 感谢您的阅读。

You can also reach me out on twitter @adeelibr

您也可以通过Twitter @adeelibr与联系

Reference & Inspiration: Stack Exchange Forum

参考与启示: 堆栈交换论坛

翻译自: https://www.freecodecamp.org/news/so-youre-in-if-else-hell-here-s-how-to-get-out-of-it-fc6407fec0e/

因此,您处于if / else地狱中-这是摆脱困境的方法相关推荐

  1. 左边是地狱右边也是地狱_走出教程地狱

    左边是地狱右边也是地狱 Let's face it, we've all been there. Maybe you are still stuck in tutorial hell and are ...

  2. MIT一招霸气颠覆传统:勇敢换导师,成本我买单,学生沸腾了

    金磊 发自 凹非寺 量子位 报道 | 公众号 QbitAI 导师=老板. 似乎这样的"等式",不知何时起,便被科研学生党们默认了. 科研生活顺利还好说,但若非常痛苦,想换" ...

  3. 开源战略游戏源码_开源的历史告诉我们关于战略优势的知识

    开源战略游戏源码 自由软件运动就像许多其他运动一样开始:一群聪明开朗的人被更大的力量所控制,然后站起来,将事情掌握在自己手中. 与美国革命没什么不同. 殖民者对被英国控制感到厌倦,因此他们宣布独立,并 ...

  4. 2017年5月许小年最新演讲:深圳人没房的,还是咬咬牙就买吧!

    许小年最新演讲:深圳人没房的,还是咬咬牙就买吧! 地产号 2017.5.6 18:16 浏览:25152 本文为许小年2017年5月5日在深圳创新发展研究院的演讲:转折点上的中国经济(本文不代表金融五 ...

  5. [转]许小年:转折点上的中国经济/中等收入陷阱

    Posted on 2017 年 5 月 11 日 by Wadelau 本文为许小年教授2017年5月5日在深圳创新发展研究院的演讲:转折点上的中国经济 中国的经济发展经过三十多年的改革开放,到现在 ...

  6. (转)算法帝国:华尔街交易怪兽的核武器缔造史

    算法帝国:华尔街交易怪兽的核武器缔造史 华尔街见闻 2017-02-01 访问量 570 1980年华尔街的黑客生涯:天时地利 http://wallstreetcn.com/node/287583 ...

  7. 当CComboUI控件处于不可见,SelectItem无效果的解决方法

    文章目录 1.问题 2.duilib源码分析 3.解决办法 1.问题 当CComboUI控件处于不可见时,SelectItem无效果的解决方法. 2.duilib源码分析 bool CComboUI: ...

  8. 八字易经算法之用JAVA实现硬币卜卦辅助工具

    世界上没有一种学问比预测未来更重要:而在全人类预测未来的学问中,没有一种比易经卜卦更重要:而在易经卜卦的所有方法中,用硬币卜卦是最简便.最常用的方法之一. -- 陈嘉珉(http://www.chin ...

  9. 洋洋背古诗——暑假版

    你好,亲爱的洋仔,这里是老爸在网络上开辟的一块小天地,记录你背诵的诗词,相信今天的努力和付出,在将来一定会开花结果,收获最美的果实. 为了便于查阅,我对咱们学过的诗词如下分类        --诗词讲 ...

最新文章

  1. SQLServer之删除约束
  2. 汇总|目标检测中的数据增强、backbone、head、neck、损失函数
  3. 二维数组及其指针基础
  4. [jQuery] jQuery与jQuery UI有啥区别?
  5. 因子分析——python
  6. Synchronize和ReentrantLock区别
  7. PHP之post请求php脚本实现
  8. MFC中App、Doc、MainFrame、View各指针的互相获取
  9. c# key event
  10. Linux查找link文件,Linux下查找一个文件的所有软硬链接
  11. 为什么阿里巴巴禁止在foreach里进行元素的remove/add操作
  12. java 读写acr122u_树莓派使用ACR122U读写IC卡
  13. 近年来Person Re-identification论文汇总
  14. Java 八皇后游戏
  15. NAS信令学习笔记 ——GUTI reallocation过程
  16. 天池比赛——新闻文本分类比赛(零基础入门NLP)
  17. 已解决selenium.common.exceptions.WebDriverException: Messag: ‘geckodriver‘ executable needs to be in PA
  18. 在spring boot中使用@WebFilter配置filter(包括排除URL)
  19. 《论工业社会及其未来》—泰德.卡辛斯基
  20. 洛谷_3975 [TJOI2015]弦论(后缀自动机)

热门文章

  1. Nginx学习之十-超时管理(定时器事件)
  2. TreeView节点的演练 c# 1614840318
  3. 前端开发 巨大banner的首页设计 0228 需另行设计
  4. lxml库的基本使用-etree解析html得到对象的不同方式-0233
  5. openssh升级之后git账户免密登陆失效
  6. 《编程珠玑(续)(修订版)》—第2章2.1节Awk中的关联数组
  7. 宁静——一种心灵的奢望
  8. Windows Azure奇趣应用之Swingify
  9. Javascript window.fetch API
  10. Node.js之十大Web框架