Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sandi Metz' Rules For Developers

THE RULES:

  1. Classes can be no longer than one hundred lines of code.

  2. Methods can be no longer than five lines of code.

  3. Pass no more than four parameters into a method. Hash options are parameters.

  4. Controllers can instantiate only one object. Therefore, views can only know about one instance variable and views should only send messages to that object (@object.collaborator.value is not allowed).

WHEN TO BREAK THESE RULES:

Paraphrasing Sandi, “You should break these rules only if you have a good reason or your pair lets you.” Your pair or the person reviewing your code are the people who you should ask.

Think of this as rule zero. It is immutable.

Which of Sandi's rules do you feel like might be the hardest to follow and why?

Methods can be no longer than five lines of code

I know this will be difficult for me to adhere to - sticking to 8 lines is sometimes hard. But I like the way they explain it in the article - that it forces you to create more private methods with descriptive names and that those private methods are great documentation. This is something I have been working on and will continue to practice.

I do however think 5 lines is a bit excesive and that there are perfectly reasonable times to use elsif or case statements. It's kind of irritating to have those things and then be told, essentially, that you're not supposed to use them. I'm not sure I understand why Metz wants 5 lines instead of 8. I'm a rock climber and it kind of reminds me of something many climbers do - there's this stigma that your shoes should be super small and that you should keep trying to squeeze your feet into smaller and smaller shoes. It's ridiculous. Why not be comfortable and just learn to use your feet slightly differently? If you have are adhering to the principle of a method doing one thing (which, yes, usually means it's going to be short - but not always!) and it works best in some extra lines so be it. But I realize I'm a baby dev and I still have tons to learn, so I'm going to try to stick to this and see how it feels! :)

Controllers can instantiate only one object

WHA??? When I first read this I thought "this is ridiculous!" and thought of the rock climbing shoe example mentioned above. I mean, come on, Metz, stop carving away at everything just because you think you're cool. But then I kept reading and saw the code samples explaining how the Thoughbot team kept to this rule and I changed my mind. Seeing how they implemented this rule was really cool and makes a lot of sense! In my last project I ended up with a lot of logic in my views that I had to refactor. I learned a lot by having to do that and between that experience and this example I hope I'm not in that position again. I will surely be paying a lot more attention to this now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.