Improve Your Software Design with The CheckAndDo Pattern ?

Why another design pattern? ?

It is a widely common task in Software to perform tests on an object and obtain the result of this process.

In Other Words ⚙️

Let’s imagine a pipeline of checks that an entity has to pass before bein…


This content originally appeared on DEV Community and was authored by Abdulcelil Cercenazi

Why another design pattern? ?

It is a widely common task in Software to perform tests on an object and obtain the result of this process.

In Other Words ⚙️

Let’s imagine a pipeline of checks that an entity has to pass before being persisted to a database.
On every step, we want to return the reason for the check failure, if it does. Otherwise, we want to proceed to the next check.

Alt Text

A Real Life Example

Let’s write a function to verify that a given password ? meets certain criteria.
Length should be between 8 and 14 characters.
Should have at least one upper case character and one lower case character.
Should have at least 2 digits.
Should have at least one special character.

Typical Solution ?

Let’s do it using Java code

public String checkPasswordAndSubmit(String password) {
        // have some logic here perhaps
        int length = password.length();

        if (length < 8) return "Password is under 8 characters";

        if (length > 14) return "Password is above 14 characters";

        if (password.equals(password.toLowerCase())) return "Password must have at least one uppercase letter";

        if (password.equals(password.toUpperCase())) return "Password must have at least one lowercase letter";

        if(password.replaceAll("\\D", "").length() < 2) return "Password must have at least two digits";

        Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(password);
        if (!m.find()) return "Password must have at least one special character";

        else return "Submitted";
    }

What was wrong with it? ?

The implementation above has many flaws, however, the most important two are:
Breaks the single responsibility principle, as this function does more than one check (six ones to be exact).
The function checkPasswordAndSubmit is about 20 lines long, which can be shorter if implemented correctly.

The checkAndDo pattern way ?

We add a chain of check functions, each one tests a single aspect.
Note the naming convention of the functions check_*And_ where:

  • * is the current check we are performing.
  • ** is the final action we want to be done. For example checkPassWordLengthAndSubmit checkPassWordCaseSensitivityAndSubmit checkThatTwoDigitsAreGivenAndSubmit checkThatSpecialCharactersAreGivenAndSubmit
    public String checkPassword(String password) {
        // have some logic here perhaps
        return checkPasswordLengthAndSubmit(password);
    }

    private String checkPasswordLengthAndSubmit(String password) {
        int length = password.length();

        if (length < 8) return "Password is under 8 characters";

        if (length > 14) return "Password is above 14 characters";

        return checkPassWordCaseSensitivityAndSubmit(password);
    }

    private String checkPassWordCaseSensitivityAndSubmit(String password) {
        if (password.equals(password.toLowerCase())) return "Password must have at least one uppercase letter";

        if (password.equals(password.toUpperCase())) return "Password must have at least one lowercase letter";

        return checkThatTwoDigitsAreGivenAndSubmit(password);
    }

    private String checkThatTwoDigitsAreGivenAndSubmit(String password) {
        if(password.replaceAll("\\D", "").length() < 2) return "Password must have at least two digits";

        return checkThatSpecialCharactersAreGivenAndSubmit(password);
    }

    private String checkThatSpecialCharactersAreGivenAndSubmit(String password) {
        Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(password);
        if (!m.find()) return "Password must have at least one special character";

        return submit(password);
    }

    private String submit(String password) {
        return "Submitted";
    }

So, should you use this pattern? ❔❓

if you have a lot of tests to run over an object, and if you want to run a specific action for each test’s result, then this pattern will provide you with a clear, plug-like design to ensure that you won’t get lost while trying to understand what goes where.

Code on GitHub?


This content originally appeared on DEV Community and was authored by Abdulcelil Cercenazi


Print Share Comment Cite Upload Translate Updates
APA

Abdulcelil Cercenazi | Sciencx (2021-05-11T19:31:06+00:00) Improve Your Software Design with The CheckAndDo Pattern ?. Retrieved from https://www.scien.cx/2021/05/11/improve-your-software-design-with-the-checkanddo-pattern-%f0%9f%93%8b/

MLA
" » Improve Your Software Design with The CheckAndDo Pattern ?." Abdulcelil Cercenazi | Sciencx - Tuesday May 11, 2021, https://www.scien.cx/2021/05/11/improve-your-software-design-with-the-checkanddo-pattern-%f0%9f%93%8b/
HARVARD
Abdulcelil Cercenazi | Sciencx Tuesday May 11, 2021 » Improve Your Software Design with The CheckAndDo Pattern ?., viewed ,<https://www.scien.cx/2021/05/11/improve-your-software-design-with-the-checkanddo-pattern-%f0%9f%93%8b/>
VANCOUVER
Abdulcelil Cercenazi | Sciencx - » Improve Your Software Design with The CheckAndDo Pattern ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/11/improve-your-software-design-with-the-checkanddo-pattern-%f0%9f%93%8b/
CHICAGO
" » Improve Your Software Design with The CheckAndDo Pattern ?." Abdulcelil Cercenazi | Sciencx - Accessed . https://www.scien.cx/2021/05/11/improve-your-software-design-with-the-checkanddo-pattern-%f0%9f%93%8b/
IEEE
" » Improve Your Software Design with The CheckAndDo Pattern ?." Abdulcelil Cercenazi | Sciencx [Online]. Available: https://www.scien.cx/2021/05/11/improve-your-software-design-with-the-checkanddo-pattern-%f0%9f%93%8b/. [Accessed: ]
rf:citation
» Improve Your Software Design with The CheckAndDo Pattern ? | Abdulcelil Cercenazi | Sciencx | https://www.scien.cx/2021/05/11/improve-your-software-design-with-the-checkanddo-pattern-%f0%9f%93%8b/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.