en Diseño de software
Pone énfasis en que el código sea autodescriptivo. Evitar los comentarios y que el nombre de las funciones sea lo mas descriptivo posible.
public function createUser(string $id, string $name, string $password){
// Asegura que tengo por lo menos 5 caracteres
if(strlen($password) > 5){
throw new \RuntimeException('The password should have more than 5 characters');
}
// Resto
$existentStudent = get($studentId, self::$students);
if(null !== $existentStudent){
throw new StudentAlreadyExist($studentId);
}
$student = new Student($studentId,$studentName,$studentPassword);
self::$students[$studentId] = $studentId;
}
Refactorizando queda:
public function createUser(string $id, string $name, string $password){
this->ensureHave5caracters($password);
// Resto
$existentStudent = get($studentId, self::$students);
if(null !== $existentStudent){
throw new StudentAlreadyExist($studentId);
}
$student = new Student($studentId,$studentName,$studentPassword);
self::$students[$studentId] = $studentId;
}
function ensureHave5caracters(string $password){
if(strlen($password) > 5){
throw new \RuntimeException('The password should have more than 5 characters');
}
}
Deja una respuesta