status or state?
2018-07-29 本文已影响0人
holysu
编程的时候经常会需要定义某某状态,比如订单状态、事件的处理状态、
脑壳里边的对于状态的单词有 status 和 state

两个的意思看着是一毛一样的,到底什么时候用哪个,还是没有区别随便用?
看看 stackexchange 上的解释
- status is used to describe an outcome of an operation (e.g. success/fail).
- state is used to describe a stage in a process (e.g. pending/dispatched).
status : 用来描述操作的结果,比如: 成功/失败
state: 用来描述过程的某个阶段,比如 进行中/ 已发送; 处理完成后 “进行中” 就变成 “已发送” 了
也就是说, status 描述的状态处理结果,是终态,这个状态之间不会再从 状态A 变成 状态B
而 state 所标示的“状态”则具有阶段性,可以从状态A变为状态B; 就如我们常遇到的订单状态

这几个状态随着订单的处理过程向前推进,不同状态之间是可以根据业务的发生相互变化的;则适合用 state, 代码或数据库中应该命名为 OrderState
还记得设计模式中的“状态模式”吗,它里边对状态的定义也是用的 state

相反,Status 所描述的状态,可以认为是离散的值,值确定后不能转变为其他状态;常见的例子有 http status, 随便打开一个浏览器控制台查看 网络 可以看到 http 请求的响应状态码命名就是 status code

一个http请求,成功 status code 就是200 即使之后服务器挂了其他请求得到503,也和之前的那个请求没关系
再如,状态码在 spring 中的定义 HttpStatus

so, 命名的时候知道如何选择了吧

参考: