Where should I put the algorithm if it isnt talking to the database?

I have a user who has joined a challenge ( He needs to cover 150000 steps a month and 5k steps daily). So now I track his daily activity. Suppose after 7 days I find that he should have covered 35k steps but has covered only 25k steps then I need to tell him that his average is 25k/7 and he needs to do 35k/7 - 25k/7 steps extra everyday to meet his daily goals.

I have a UserActivity class which keeps a track of the user’s activity like his daily steps, his monthly goal, progress, start date, end date etc.

Now I have to design an algorithm to motivate users based on their performance. So I need to motivate a lazy person in a different way than a person who does not need any motivation from me.

My question is that where should I keep this algorithm, will this be in the UserActivity model? Or should I create a service class?

My approach is that if it is in fact a service, i.e. you tell it to do something and are not particularly interested in it’s data, you can put it on an app/services directory. If not, you can create a class and put in either the app/models or lib directories.

Remember, a class does not need to inherit from ActiveRecord in order to belong to the models directory.

So what would you name the class? A class that decides how to motivate a user depending on his performance. You think a class for this makes sense?

Naming a class is hard, especially given that I’m not familiar with the domain and codebase.

That being said, if you do decide it is a service, I usually name them with verbs, instead of nouns. So it would be something like MotivatesUser.

Oh. Maybe I will write down the algorithm in the UserActivity class and figure out later where to do extract class. Thanks

Probably a good decision. Once you have a better understanding of what you’re building, you’ll be able to name it more easily.

1 Like

Thanks. This is the first full web application I am developing. So there is no way I can predict the problems before hand. Plus I have been given a target to release a prototype by 24th feb including the UI.