Java validating data

Our interface implementation should be able to handle registration data but as you see we have different types of data.So what we need here is to encapsulate registration data in a single object like this :public class Email Validatation Rule implements Registration Rule{ private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@" + "+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$"; private final Pattern pattern = Pattern.compile(EMAIL_PATTERN); @Override public void validate(Registration Data reg Data) { if ( !"); } The cyclomatic complexity of this method is really high and it might get worse if there are more fields to validate or if we add the actual business logic.Of course we can split the code in two private methods ( validate, do Register ) but the problem with several if blocks will be moved to the private methods.There are cases that we want to validate input data before we send them to business logic layer for processing, computations etc.This validation, in most cases, is done in isolation or it might include some cross-checking with external data or other inputs.pattern.matcher(reg Data.email).matches()) { throw new Illegal Argument Exception("Email is not a valid email!"); } }It’s clear that we have isolated in the above class the email validation.

name.trim().equals("")){ throw new Illegal Argument Exception("Name should not be empty!

Besides this method is doing more than one thing and is hard to test.

When I ask junior developers to refactor this code and make it more readable, testable and maintainable they look at me like an alien :”How am I supposed to make it simpler. ” Well here’s a solution that works fine, honors the Single Responsibility Pattern and makes the code easier to read. In Java 8 terms, it’s called a functional interface, like the following.

To better understand the solution, think each of these if blocks as a validation rule. Now it’s time to transform each validation check to a registration rule.

But before we do that we need to address a small issue.

java validating data-44java validating data-74java validating data-8

We can do the same for all rules of our initial implementation.

Join our conversation (9 Comments).
Click Here To Leave Your Comment Java validating data.

Comments:

  1. 1
    Steve

    The development of radiocarbon dating has had a profound impact on archaeology.

  2. 1
    Steve

    Dating is a diverse site that offers expert articles and content pieces, including in-depth profiles and features of industry-leading people and companies, as well as expert reviews of the best dating sites, exclusive studies, and more. Soulmate intensives are a short series of soul therapy sessions that help you prepare for Big love, find the One, and create a healthy, lasting spiritual partnership.

  3. 1
    Steve

    Differences in various ebony cam sites Having a cyber sex option is thrilling as well as exciting.

  4. 1
    Steve

    CLICK HERE FOR ENTER 40 Plus Pussy TOPLIST Welcome to Mature-Live.com, Internet’s leading source for experienced and seductive mature ladies and couples giving live sex shows in front of their cameras.

Leave a Reply

Your email address will not be published. Required fields are marked *