We strive to write code that conforms to the 4 rules for developers by Sandi Metz:
- Classes can be no longer than one hundred lines of code.
- Methods can be no longer than five lines of code.
- Pass no more than four parameters into a method. Hash options are parameters.
- Controllers can instantiate only one object. Therefore, views should only know about one instance variable.
Please read an article on those 4 rules.
The 4th rule can be a handful to conform to. Whenever in doubt of breaking any rule generally, follow this:
- Don't violate a guideline without a good reason.
- A reason is good when you can convince a teammate.
Since it's often a handful, just note that the reason it's created is because of controller actions like this one.
- Avoid writing class methods - they resist refactoring and are better as extracted objects with one public method and multiple privates then as a single class method. Class methods should only be used for describing a rule that's applied for all instances:
class Order def self.policy_class OrderPolicy end end
- Avoid writing local variables - most local variables are better off as extracted methods.