在Rails中使用局部的最佳实践 [英] Best Practices for using partials in Rails

查看:58
本文介绍了在Rails中使用局部的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了与DRY原理保持一致,一旦我重复特定模式超过一两次,我就会尝试使用局部函数。结果,我的某些观点包含十个或更多个不同的部分。我担心这可能会对整体表现产生负面影响。一些编程书籍比较了部分函数和方法的使用。因此,我应该使用相同的原理来确定何时使用它们吗?

In keeping with the DRY-principle I try to use partials as soon as I am repeating a particular pattern more than once or twice. As a result, some of my views consist of ten or more different partials. I am worried that this might have a negative effect on the overall performance. Some programming books compare the use of partials with the use of methods. So should I use the same rationale to determine when to use them?

在Rails项目中有关零件的大小和数量的最佳实践是什么? strong>

推荐答案

我已经很喜欢您的练习:重复两次查看代码后,将其重构为部分代码。蒂姆(Tim)的权利是,您可以在配置文件后 和在被证明是必需的 之后加快速度。

I like your practice already: Once you've repeated view code twice, refactor it out to a partial. Tim's right that you can speed it up as necessary after it's been profiled and after it's been proven necessary.

这是我的一个警告:如果您与负责处理视图的专业设计师合作,从长远来看,拥有相当重复的视图代码可能会更容易。有些人很难搜索局部内容并看到它们如何组合在一起。我发现这些人可以更轻松地管理整个文件,并根据需要更新多个文件。最佳?作为程序员,对我们而言不是这样,但是设计师更习惯于在一个或三个文件中看到大多数HTML,而不是20。:)

Here is my one caveat: If you work with professional designers who handle the views, it may be easier over the long term to have rather repetitive view code. Some people have a hard time searching through partials and "seeing" how they all fit together. I've found it easier with those people to let them manage the whole shebang and update more than one file if they need to. Optimal? Not to us as programmers, but designers are more often used to seeing most of the HTML in one or three files rather than 20. :)

这篇关于在Rails中使用局部的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆