为什么不建议在响应中使用裸露的HTTP状态码?

huangapple go评论65阅读模式
英文:

Why isn't it recommended to use bare HTTP status codes in a response?

问题

根据Django REST Framework的文档,"在响应中使用裸状态代码不建议"。他们建议使用status模块中的对象(例如status.HTTP_404_NOT_FOUND)。我的问题是,这个建议是从哪里来的?我找不到任何类似的"官方"来源来支持这个建议。

英文:

According to the documentation for Django REST Framework, "using bare status codes in your responses isn't recommended". They recommend instead using the objects from the status module (e.g. status.HTTP_404_NOT_FOUND). My question is, where does this recommendation come from? I wasn't able to find any kind of 'official' source for this recommendation.

答案1

得分: 3

这个建议来自于那份文件,但是它基于避免使用魔法数等概念。

在Django Rest Framework中,这些状态的命名方式(来源)使开发人员阅读代码时更容易理解和记住代码代表的含义,而不必记住或双重检查每个数字所指代的内容。

英文:

The recommendation comes from that document but it's based on concepts like avoiding magic numbers.

With the way these statuses are named in Django Rest Framework (source) makes it easier for the developer reading the code to understand and remember what the code represents rather than having to remember or double check what each number refers to.

huangapple
  • 本文由 发表于 2023年6月19日 15:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定