New Gig!

I just started a new job at an early stage startup. The app was actually built by a junior developer, so I will probably be posting some of the code that we are changing, since it will cover mistakes that junior devs often make.

First up: listen to Sandi Metz  and try to limit the number of objects instantiated in your controllers. Sometimes you need more than one, but 5+ is usually preventable.

Sandi Metz' Rules For Developers

I came across this post from thoughtbot about Sandi Metz’ rules for developers. This is a bit of an older post, but I had a few thoughts about it.

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).

How a mid-level developer feels about these rules:

  1. This tends to be pretty easy for me to keep to for regular classes, less so for specs. It’s hard to argue with this rule and it’s pretty *relatively* easy to follow.
  2. This one is a bit harder. Mainly, as thoughtbot points out, if you follow this rule, you can’t use elsif with else if following this rule. Sometimes, it’s not worth it to break things up. However, I do try to follow this rule as often as possible.
  3. This is another hard one. If you need more than four values, storing them in an object works fairly well. However, sometimes you don’t want to bulk up your code by creating another object instead of just passing params/a hash.
  4. I’m definitely a fan of the Facade Pattern as well. Keeping one instance variable per controller is another one that is fairly easy to keep.

Even for the ones that I find hard to follow, I am trying to keep to these rules with my own code. Definitely worth reviewing and incorporating into your development process.