不可用于脚本或 API 实现的模型对象。您的模型定义了应用程序的域语义。您应该能够启动脚本/控制台,或编写服务 API 包装器,它们重用现有的功能模型方法来操作应用程序中的所有关键数据。控制器功能仅在 HTTP 请求/响应周期中可用,这只是任何全功能站点生命周期的一部分。
There are two major anti-patterns I've seen in a lot of Rails code:
Lots of "heavy lifting" in views. Anything more complicated than simple iteration over collections or interpolation of strings should be in helpers or model methods. Don't query for model objects, construct big JSON arrays, or update session variables from your ERB templates.
Model objects which aren't usable for scripting or API implementation. Your models define the domain semantics for your application. You should be able to fire up script/console, or write service API wrappers, which reuse existing, functional model methods to manipulate all of the key data in your application. Controller functionality is only available in the HTTP request/response cycle, which is only part of any full-featured site's lifecycle.
发布评论
评论(4)
不学Ruby。
Not learning Ruby.
我在很多 Rails 代码中看到了两种主要的反模式:
视图中有很多“繁重的工作”。任何比集合上的简单迭代或字符串插值更复杂的事情都应该在助手或模型方法中。不要查询模型对象、构造大型 JSON 数组或从 ERB 模板更新会话变量。
不可用于脚本或 API 实现的模型对象。您的模型定义了应用程序的域语义。您应该能够启动脚本/控制台,或编写服务 API 包装器,它们重用现有的功能模型方法来操作应用程序中的所有关键数据。控制器功能仅在 HTTP 请求/响应周期中可用,这只是任何全功能站点生命周期的一部分。
There are two major anti-patterns I've seen in a lot of Rails code:
Lots of "heavy lifting" in views. Anything more complicated than simple iteration over collections or interpolation of strings should be in helpers or model methods. Don't query for model objects, construct big JSON arrays, or update session variables from your ERB templates.
Model objects which aren't usable for scripting or API implementation. Your models define the domain semantics for your application. You should be able to fire up script/console, or write service API wrappers, which reuse existing, functional model methods to manipulate all of the key data in your application. Controller functionality is only available in the HTTP request/response cycle, which is only part of any full-featured site's lifecycle.
使用除非与其他
反模式:
替代:
USING unless WITH else
Antipattern:
Alternative:
字母汤?
(没有类型声明和无意义的变量命名,导致几乎不可读的代码)
模式名称来自变量名称,如“a”、“b”、“c”、“d”等。
Alphabet Soup?
(No type declared and meaningless variable naming which leads to nearly un-readable code)
Pattern name comes from variables names as 'a','b','c','d', etc.