[Javascript] Eliminate Boolean Explosion by Enumerating States
There are several fundamental problems with trying to manage the state of a function through the use of booleans. The first is often referred to as "boolean explosion". For every boolean we add to a function, we increase the number of possible states at a rate of 2^n
where n
is the number of booleans. Running the math just a few times quickly reveals an absurd amount of states.
The second problem is that many of these states are "impossible states", states our application should never be in. The example in the lesson is that the light bulb should not be isLit
and isBroken
. It's simply not possible, and is an inaccurate modeling of an actual light bulb.
The way we solve for this problem is by enumerating the possible states. In other lessons in this course, we'll do that with state machines, but for now, we'll do that by enumerating the possible states and updating our function to only utilize these possible states.
function lightBulb() { let isLit = false; let isBroken = false; return { state() { return { isLit, isBroken }; }, toggle() { if (isBroken) { isLit = false; return; } isLit = !isLit; }, break() { isBroken = true; isLit = false; } }; } const bulb = lightBulb(); const log = () => { console.log(bulb.state()); }; bulb.toggle(); bulb.break(); log(); // { isLit: false, isBroken: true }
Better:
We list all the possible states, which mean 'Lit' is not possible to happen with 'Borken'.
const STATE = { LIT: "lit", UNLIT: "unlit", BROKEN: "broken" }; function lightBulb() { let state = STATE.UNLIT; return { state() { return state; }, toggle() { switch (state) { case STATE.LIT: state = STATE.UNLIT; break; case STATE.UNLIT: state = STATE.LIT; this.break; } }, break() { state = STATE.BROKEN; } }; } const bulb = lightBulb(); const log = () => { console.log(bulb.state()); }; bulb.toggle(); bulb.break(); log(); // broken
原文地址:https://www.cnblogs.com/Answer1215/p/12209611.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 一分钟学Python|Python的字典
- MySQL information_schema详解 ENGINES
- 一日一技:不用轮询,基于事件监控文件变动
- Python 基础(十七):装饰器
- XtraBackup工具详解 Part 10 使用innobackupex对数据库进行部分备份(指定表或数据库)
- Python 基础(十九):数学相关模块
- XtraBackup工具详解 Part 11 使用innobackupex对部分备份进行恢复
- XtraBackup工具详解 Part 12 流式和压缩备份
- 基于STM32+RT-Thread的新冠肺炎疫情监控平台
- Python 基础(二十):sys 模块
- [Oracle 故障处理]记一次Windows监听启动失败
- Python 基础(二十一):argparse 模块
- LeetCode 4 题解
- 操作系统与 CPU 是怎么执行线程的?
- LeetCode 5 题解