Why is my User class so fat?

  • Yo User class is so fat, when it went to the beach, all the whales started singing “We Are Family.”
  • Yo User class is so fat, it took me two trains, a plane, and a bus to get to it’s good side.
  • Yo User class is so fat, when God said “Let there be light,” he asked it to move out of the way.

Enough with mama jokes. Answer is quite simple. Most of things in your website are connected to User class. That’s why it is very convenient to put methods and logic into it.

Is it bad to have a big User class? Probably no, if you are comfortable with it, there is no problem.
Let’s examine how User class becomes fat and ways to prevent it.

How User class becomes fat?

One day at a time. Sorry, couldn’t hold the joke. But really, how User class becomes big? Reality is that you add one method. After some time you will add another method. After several months editor is counting lines in hundreds. Few years go by and by that time you already counting methods by hundreds instead of lines. Then you start to wonder what happened? Where things went wrong? How I became fat...?

1. Example

Let’s imagine that we have new Laravel project. Inside of it there is empty User class.

Everything seems so peaceful and perfect. One day somebody decides that your website needs admin. Let’s add that.

One additional kilogram on the scale, nothing to worry about, I am still fit and can enjoy potato chips. One method in User class. Everything is still good. But after some time you need moderator on your website.

And now you need to sent to admin email if moderator edited something.

You see how we are getting fat?

2. What to do?

The answer is simple, but not easy to implement. You need to go on a diet! Probably you have heard it hundred of times. But you say it doesn’t work.
Of-course it is a lot of hard work, sweat and discipline. This is how you lose weight or lines from your file.

Do you really need to have everything in User class? If sendEmailToAdmin() is called only once from somewhere, can we extract it to another class?

Maybe some logic can be abstracted? Like combining similar methods?

That looks a bit better but still not perfect. Maybe we can extract that logic to separate class also?

Now User class is much shorter. Good job. Let’s have a pizza. While you have eated that deliciuos slice of pizza, you are fat again. That’s what they call Jo-Jo dieting. Keeping User class slim is constant work. It is quite easy to add new methods. But it is much harder to remove them.

How we made User class slim? Basically we moved rarely used methods to separate classes. Also we combined logic of similar functions.

Skinny Fat

So in conclusion I can only say that you need to watch your belly fat constantly. I am just kidding, you need to watch only your User class. Belly fat doesn’t matter while you are programming. Unless you can’t reach your keyboard. But that’s not my expertise.

If you are happy how you User class looks like, be proud of it! Fat is not bad, it’s just different. The most important thing is keeping balance. Too fat or too skinny isn’t good.

Mantas Donelavicius

Written by

Simplicity is the ultimate sophistication.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade