Don't over concern your rails project

16:07 Anil Maurya 0 Comments

Rails Concern is a good feature, don't make it evil . John McDowall has written an excellent article about how we are misusing rails concern. 

Rails concern are not meant to make your fat model skinny, its meant for making your life easy when you want to include, extend  and use included callback ( all the same time) in a module.

`Skinny Controller and Fat Model` is not enough for writing quality code. Fat Model is pain for maintenance and enhancement. Its nightmare for new developer on the project.

We use Code climate to keep a check on code smell.

Code climate complains when your model start becoming fat and simple option to shut code climate is to pull out some methods related to same logic and dump in a module or concern and code climate gives you green signal.

Even though code climate says your code quality improved but in reality you have just deteriorated your code quality. According to code climate team `Pulling out methods from model to some module is not refactoring ` but code climate is not smart enough to catch this. I think that's why they quote :

Any application with an app/concerns directory is concerning.”

 Solution for writing quality code is to follow Single Responsibility Principle . 

 - Model should be responsible for defining validations, callbacks and methods directly related to class.
- Avoid Callbacks
   - only use callbacks for preparing object.
   - after_* callbacks are Bad. Once model is saved, purpose of model is done.

- Add Service(plain ruby) object whenever you can, to solve a particular problem.



0 comments :