Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Delivering the MVP Refactoring

jinhwa yoo
jinhwa yoo
10,042 Points

refactoring and validation... I am frustrated by understanding help me... with specific explanation.....

refactoring and validation... I am frustrated by understanding

java

1 Answer

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey,

So let's not over think this and keep it simple. Validation and refactoring are not that similar.

Refactoring - This is making changes to the code so that it's more easily human readable, and removes code duplication. Think of it like this.

When I start coding, I don't plan as well as I should. When I hit a problem, Instead of thinking about design, or how It should work. I just hack it all together until it works. Then I keep going. Days later I hit another issue that I hack together. What I don't realize at that point in time, is that I could call an existing method to solve my problem. When I finally finish, and I review the code as a whole. It's full of comments, commented out code and it's just a mess lol. It's not very easily human readable, and variable names don't really represent what they are etc. I refactor my code. I refactor my code by looking at it as a whole and saying oh I do the same thing three different ways in three methods. Let's just remove two of the three methods and use just one to perform the action each time I need it.

IN SHORT - It's just fixing up my code and making it easier for someone else to come in and make changes. The more clearly defined variable names, use of methods and reduction in duplication. The easier it is for someone else to understand what is happening.

Validation - This is ensuring that certain things work the way that they should.

This is a high level example. Say I have a form. The form requires a name, and an email address. I know that a name is text. I know that an email address will contain an @ sybmbol and a .com, .net, .aol .gmail .yahoo or whatever. When I right my code, I want to make it hard for the user to make a mistake.

FORM A -- Is good

Name: Ryan Email: ryan@gmail.com

FORM B -- is bad

Name: ryan@gmail.com Email: Ryan

I know Form A is filled out correctly, BUT to assume someone is going to fill it out properly is never a good idea. In an event like FORM B, maybe they were drunk or in a hurry who knows. But I don't want to accept values like that in my form.

Validation - creating rules surrounding how your code is allowed to work, what it's allowed to do and what type of data it can do what to. Let's make some rules to ensure data is always like FORM A and not like FORM B

RULES: Name: Must be a string, no longer than 30 characters and have no spaces (assume first name only)
Email: Must be a string, no longer than 40 characters and must contain an @ symbol. IF RULES ARE BROKEN - Prompt user with a message.

Now if someone submits a form that does not follow these rules. The form will prompt the user. That is validation.

IN SHORT - Validation are rules surrounding how code is allowed to function. It may seem trivial but I assure it's not. When you get into enterprise Java applications. This is absolutely necessary for proper functionality and security. It ensures that code behaves how we want, despite the way in which it was accessed, ran or even the data it is ran with.

Does this help? If not let me know and I can try to approach it differently.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

What a nice answer !!!!!! Thumbs up !