Write simple units of code

Hieronder eens stuk code uit het eerste project. Hier wordt een wachtwoord aangemaakt en gecontroleerd op een aantal punten. Echter gebeurt alles in dezelfde methode, waardoor deze onoverzichterlijk wordt.

<html> 
<head> 
<body>

Dit zou kunnen worden aangepast door de code op te splitsen in verschillende onderdelen:


    public boolean validateInput() {
        //create 2 boolean variables
        clearErrors(errorMessagePasswordShort,errorMessagePasswordNoSpecial, errorMessageUsernameExists);
        clearRedBorder(passwordPasswordfield, usernameTextfield);
        boolean isValid = true;
        boolean hasSpecialChar;
        //check if all fields have been filled out
        checkIfEmpty(usernameTextfield, passwordPasswordfield, firstnameTextfield, lastnameTextfield, role);
        //check if the length of the password is larger than a certain amount of characters
        if (passwordPasswordfield.getText().length() < MINPASSWORDLENGTH) {
            setRedBorder(passwordPasswordfield);
            setError(errorMessagePasswordShort,"Wachtwoord moet minimaal 8 tekens bevatten");
            isValid = false;
        }

        //create logic with Regex to check if password contains a special character.
        //the Pattern statement is to initialize the special characters and put it into "special"
        //the Matcher statement checks if any of the special characters matches with any characters in the String password
        //if a special character is found, set the boolean "hasSpecialChar" to true
        Pattern special = Pattern.compile("[!@#$%&*()_+=|<>?{}\\[\\]~-]");
        Matcher hasSpecial = special.matcher(passwordPasswordfield.getText());
        hasSpecialChar = hasSpecial.find();

        //check if hasSpecialChar is true and if not, provide an error
        if (!hasSpecialChar) {
            setRedBorder(passwordPasswordfield);
            setError(errorMessagePasswordNoSpecial,"Wachtwoord moet bestaan uit \n minstens 1 special character");
            isValid = false;
        }
        //when creating a new user or changing the username of an existing user
        if (user == null || !user.getUsername().equals(usernameTextfield.getText())) {
            //validates if the chosen username is already in use
            if (userDAO.getOneByName(usernameTextfield.getText()) != null) {
                setRedBorder(usernameTextfield);
                setError(errorMessageUsernameExists,"Gebruikersnaam al in gebruik");
                isValid = false;
            }
        }
        return isValid;
    }


    public boolean validateInput() {
        clearAllErrorsAndBorders();
        boolean isValid = true;
        if (!checkIfAllFieldsFilledOut()) {
            isValid = false;
        }
        if (!checkPasswordLength()) {
            isValid = false;
        }
        if (!checkPasswordSpecialChar()) {
            isValid = false;
        }
        if (!checkUsernameAvailability()) {
            isValid = false;
        }
        return isValid;
    }
    
    private void clearAllErrorsAndBorders() {
        clearErrors(errorMessagePasswordShort, errorMessagePasswordNoSpecial, errorMessageUsernameExists);
        clearRedBorder(passwordPasswordfield, usernameTextfield);
    }
    
    private boolean checkIfAllFieldsFilledOut() {
        return !isFieldEmpty(usernameTextfield) && 
               !isFieldEmpty(passwordPasswordfield) && 
               !isFieldEmpty(firstnameTextfield) && 
               !isFieldEmpty(lastnameTextfield) && 
               role != null;
    }
    
    private boolean isFieldEmpty(TextField textField) {
        return textField.getText().trim().isEmpty();
    }
    
    private boolean checkPasswordLength() {
        if (passwordPasswordfield.getText().length() < MINPASSWORDLENGTH) {
            setRedBorder(passwordPasswordfield);
            setError(errorMessagePasswordShort, "Wachtwoord moet minimaal 8 tekens bevatten");
            return false;
        }
        return true;
    }
    
    private boolean checkPasswordSpecialChar() {
        if (!hasSpecialChar(passwordPasswordfield.getText())) {
            setRedBorder(passwordPasswordfield);
            setError(errorMessagePasswordNoSpecial, "Wachtwoord moet bestaan uit minstens 1 special character");
            return false;
        }
        return true;
    }
    
    private boolean hasSpecialChar(String password) {
        Pattern special = Pattern.compile("[!@#$%&*()_+=|<>?{}\\[\\]~-]");
        Matcher hasSpecial = special.matcher(password);
        return hasSpecial.find();
    }
    
    private boolean checkUsernameAvailability() {
        if (isUsernameChanged()) {
            if (isUsernameTaken(usernameTextfield.getText())) {
                setRedBorder(usernameTextfield);
                setError(errorMessageUsernameExists, "Gebruikersnaam al in gebruik");
                return false;
            }
        }
        return true;
    }
    
    private boolean isUsernameChanged() {
        return user == null || !user.getUsername().equals(usernameTextfield.getText());
    }
    
    private boolean isUsernameTaken(String username) {
        return userDAO.getOneByName(username) != null;
    }
    
Rubrics