We strive to write code that conforms to the four 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 these four rules.
You may find it difficult to conform to the fourth rule. In general, whenever you think you might be breaking a rule, 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 difficult, just remember that it was created 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 than as a single class method. Class methods should be used only for describing a rule that is applied to all instances:
class Order def self.policy_class OrderPolicy end end
- Avoid writing local variables—most local variables are better as extracted methods.