Ruby on Rails 的反模式

发布于 2024-08-03 02:17:32 字数 1436 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掩耳倾听 2024-08-10 02:17:32

不学Ruby。

Not learning Ruby.

漫雪独思 2024-08-10 02:17:32

我在很多 Rails 代码中看到了两种主要的反模式:

  1. 视图中有很多“繁重的工作”。任何比集合上的简单迭代或字符串插值更复杂的事情都应该在助手或模型方法中。不要查询模型对象、构造大型 JSON 数组或从 ERB 模板更新会话变量。

  2. 不可用于脚本或 API 实现的模型对象。您的模型定义了应用程序的域语义。您应该能够启动脚本/控制台,或编写服务 API 包装器,它们重用现有的功能模型方法来操作应用程序中的所有关键数据。控制器功能仅在 HTTP 请求/响应周期中可用,这只是任何全功能站点生命周期的一部分。

There are two major anti-patterns I've seen in a lot of Rails code:

  1. 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.

  2. 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.

一向肩并 2024-08-10 02:17:32

使用除非与其他

反模式:

unless is_the_weekend?
  do stuff that you do during the week
else
  do stuff that you do on weekends
end

替代:

if is_the_weekend?
  do stuff that you do on weekends
else
  do stuff that you do during the week
end

USING unless WITH else

Antipattern:

unless is_the_weekend?
  do stuff that you do during the week
else
  do stuff that you do on weekends
end

Alternative:

if is_the_weekend?
  do stuff that you do on weekends
else
  do stuff that you do during the week
end
暖阳 2024-08-10 02:17:32

字母汤?

(没有类型声明和无意义的变量命名,导致几乎不可读的代码)

模式名称来自变量名称,如“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文