When passing data from one class to another, should you pass the entire model, or separate arguments?

Viewed 29

I have a model with quite a bit of parameters:

UserDataModel({
    this.id,
    this.name,
    this.email,
    this.phone,
    this.photo,
    this.firstName,
    this.lastName,
    this.birthday,
    this.sex,
    this.land,
    this.bio,
    this.interests,
    this.facebook,
    this.instagram,
    this.telegram,
    this.register,
    this.login,
    this.timezone,
    this.banned,
    this.bannedDate,
    this.locked,
    this.lockedDate,
    this.postsNum,
    this.likesNum,
    this.commentsNum,
    this.views,
    this.notifications,
    this.push,
    this.position,
    this.rating,
    this.rewards,
    this.verified,
    this.notify,
    this.the2Fa,
    this.params,
  });

and I'm using a stream to pass this model to a widget that only needs rating.level(a submodel for the rating parameter) and photo.small(another submodel, but for the photo parameter).

And I'm wondering if passing the entire model to a widget that only requires 2 parameters can lead to more memory being used, for example. It's not a big deal to do it either way, and it would be a bit upsetting to leave some minor optimisation on the table.

Any help is appreciated. I guess this question can apply to any language, but since I'm using flutter, I'll tag it with that.

1 Answers

It's cleaner to pass only what you need. If rating.level is an object, in your Flutter widget, add it as a requirement

Widget rating({required Level level})

In when you call this rating widget, pass the level only:

return rating(level: myUserModel.rating.level)

If this level is an int or String for example, do the same thing as well. I am not sure how significant this would impact performance, but the main reason for it is that this enables better widget reusability and recycling. Apply this to your other projects, when you have created a cool rating widget, but you have ratings for users, and for example products. Your rating model is the same, but the product and user data models are different. Instead of having to create a model for ProductRating and UserRating, you can you the same rating widget, and this widget only requires the rating object, or level object.

Related