In episode 87 of the fantastic Ruby Rogues podcast, Sandi Metz shares these programming rules:
- Your class can be no longer than 100 lines of code.
- Your methods can be no longer than four, maybe five lines of code.
- You can pass no more than four parameters and you can’t just make it one big hash.
- Your Rails controller can only instantiate one object
- Your view can only know about one instance variable
(There is significant discussion before and after noting that she only considers these a simplistic starting point, and that any rule can be broken if you have a good reason)
I like these rules, I can see how they can improve code, except for the one about controllers -- to me, the job of a controller is to match a user's action with the behavior of the system and orchestrate actions and relationships between parts of the system, so it seems natural to instantiate several objects at that level and have them interact.
Go listen to the episode, it's great!
Co-Founder and CTO of Medstro