2

I have some service that return user's data by it's id. User already stored in request(session) - no need to fetch it every time.

The question is what is the best practice for parameter. Id of the user(SOLID) or user object(more validation/typings).

Example:

//...

// this
public async getByUser(user: User) {
  return await Transaction.findAll({
    where: { userId: user.id }
  });
}

// or this
public async getByUser(userId: number) {
  return await Transaction.findAll({
    where: { userId }
  });
}

Alex Kalmikov
  • 1,865
  • 19
  • 20
  • 1
    Methods should only do what they are supposed to do. If it gets a user by ID, the only parameter provided should be an ID. – Oliver Apr 23 '19 at 12:45

1 Answers1

2

Either. Really. Just keep the naming proper.

The more declarative your function names, the less confusion

//...

// this
public async getByUser(user: User) {
  return await Transaction.findAll({
    where: { userId: user.id }
  });
}

// or this
public async getByUserId(userId: number) {
  return await Transaction.findAll({
    where: { userId }
  });
}
Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51