Search code examples
functionoopsingle-responsibility-principle

Responsibility of a function


I was writing a function on PHP and a thing made me confused while writing the function. What is the best way to describe the responsibility of a function?

function createWallet(int $userId) {
  // validate if user exists
  //create wallet
}

or
**client will validate if user exists then call createWalletFunction
function createWallet(int $userId) {
  //create wallet
}

Solution

  • According to SOLID Principal, each class / function / method should have only one responsibility so I suggest the later option, Client should call the method to validate user and on success call createWallet() method. Also this will benefit if user is not authorize then client can inform some kind of message.