Naming service classes in Rails

One of the core ideas of Rails is convention over configurations. Models go in app/models, controllers go in app/controllers and views go in app/views. The danger is that we stick to those conventions no matter what and we end up either with fat controllers, fat models or even worse a mixture of both.

Many times we don’t take enough advantage of Ruby’s object oriented nature and the ability to extract functionality out into separate classes. Doing so can make an entire application easier to extend, understand and test. I have tried approaching this from different angles in different projects and I’ve found that the two main hurdles to getting this right are naming classes and putting them in the right place.

I have experimented with naming such as UserAuthenticator and UserAuthenticationService, and always end up feeling uncomfortable I constantly wonder if the other name is better or if there is a better way entirely. Using agent nouns in class names is considered a code smell, but the more that I think about it the important part is picking a choice and sticking to it. I was looking at the GitLab repository and noticed that they’ve done exactly that, everything is named in a consistent manner. I think many may dislike that but it makes things extremely clear and easier for anyone contributing as to what they should name their classes and where they should put them.