函数的责任
Responsibility of a function
我正在 PHP 上编写一个函数,在编写该函数时有件事让我感到困惑。描述函数职责的最佳方式是什么?
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
}
根据 SOLID Principal,每个 class / 函数 / 方法应该只有一个责任,所以我建议后一种选择,客户端应该调用该方法来验证用户并在成功时调用 createWallet() 方法。如果用户未授权,这也将有益,然后客户端可以通知某种消息。
我正在 PHP 上编写一个函数,在编写该函数时有件事让我感到困惑。描述函数职责的最佳方式是什么?
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
}
根据 SOLID Principal,每个 class / 函数 / 方法应该只有一个责任,所以我建议后一种选择,客户端应该调用该方法来验证用户并在成功时调用 createWallet() 方法。如果用户未授权,这也将有益,然后客户端可以通知某种消息。