2
I have a class called clients. I have 2 methods called verificaCPF()
and queryInsert()
.
Both methods when it makes the instruction it gives a return
(is working normal). My question is whether this form I’m doing is a good practice? Do you do this?
Can you give me a hint to improve that return part of the instruction?
Thank you very much ;)
public function verificaCPF($cpf) {
try {
$this->cpf = $cpf;
$stmt = $this->conn->conexao()->prepare("SELECT cli_cpf FROM clientes WHERE cli_cpf =:cli_cpf");
$stmt->bindParam(":cli_cpf", $this->cpf);
$stmt->execute();
if($stmt->rowCount() <=0){
return 'ok'; //ALGUMA DICA PRA SER MAIS LEGIVEL PRA QUEM TA DE FORA?
}else{
return 'nao';
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
index php.
if (isset($_POST['cadCliente'])) {
$objCli = new clientes();
if($objCli->verificaCPF($_POST['cpf']) =='nao'){
echo '<script>alert("CPF EM DUPLICIDADE");</script>';
}else{
if ($objCli->queryInsert($_POST) == 'ok') {
echo '<script>alert("Cadastro realizado!");</script>';
}
}
It wasn’t easier to return
true
orfalse
, Young? Returning a "loose string" is a bad idea. The most I would do differently in this case would be to return predefined constants with numerical values, but only if the answer were to return something more than true or false– Wallace Maxters
In this case, for those who are "outside", it would no longer be correct to call this function a checkDuplicityCPF. Because verificaCPF, the first thing that comes to mind is that this function serves to verify that the CPF is correct (validation of the check digit, for example)
– William John Adam Trindade
@Williamjohnadamtrindade always use verbs without conjugation: "check". But that’s exactly what I quoted at the end of my answer and I agree with you: The method needs to describe exactly what it will do.
– Wallace Maxters
Sometimes describing too much also kills me with anger, kkkkk. It’s like you have a method
excluirUsuario
within the classUsuario
. I think it’s always more a matter of common sense, of always trying to be clear.– Wallace Maxters
@Wallacemaxters you’re right. I also only use the infinitive for methods and functions.
– William John Adam Trindade