1

I'm trying to decide how much to reuse partials in Rails. I've seen reuse encouraged here: Best Practices for using partials in Rails

But the more you reuse, the more you add if-then logic.

For example, I could use the same partial: - for the "show" action (showing a single model) - for the "index" action (showing a collection of models) - for the "form" action - for the "show" action of an associated model

My rule of thumb here is if-then is okay in moderation, and without any nested if-thens...but I'm curious if there are any best practices here, as I haven't seen it discussed much.

EDIT: To clarify, I'm talking about if/then based on parameters like "is_editable" or "show_author"...not based on calling action like "for_show" or "for_form".

Community
  • 1
  • 1
mahemoff
  • 44,526
  • 36
  • 160
  • 222

1 Answers1

1

My opinion is that the presence of if/else logic that checks for the calling action in a partial is always an antipattern. It just makes code harder to navigate. Also, if the other developer will do modification on your code he/she will most likely put another elsif, accepting your original design choice. This will lead to eventual maintainability nightmare.

alexs333
  • 12,143
  • 11
  • 51
  • 89
  • I didn't mean if/then based on calling action, more like general parameters. I'll clarify the question.. – mahemoff Nov 09 '11 at 18:20