Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object-level validation vs validating user input #9

Closed
iskandarzulkarnaien opened this issue Aug 22, 2019 · 4 comments
Closed

Object-level validation vs validating user input #9

iskandarzulkarnaien opened this issue Aug 22, 2019 · 4 comments

Comments

@iskandarzulkarnaien
Copy link

iskandarzulkarnaien commented Aug 22, 2019

From what I know, there are two main ways to approach error handling. Object-Level validations (raise exception in constructor) and validating user input (in whatever class handles user input).

Seems like the general consensus from this Stack Overflow question is that object level validation is the "right" way to approach things from an OO perspective (e.g. one response reads "The constructor should always return an instance which is in a valid state. There is no excuse for doing otherwise.") as you are more likely to accidentally create a god class that handles everything if you choose to validate user input.

It seems that most of us chose to validate user input. Anyone have any opinions regarding this? Is validating both an excessive measure?

@iskandarzulkarnaien iskandarzulkarnaien changed the title Object-level validation vs validations in main Object-level validation vs validating user input Aug 22, 2019
@damithc
Copy link
Collaborator

damithc commented Aug 22, 2019

Perhaps you can give a concrete example, preferably from the iP?

@iskandarzulkarnaien
Copy link
Author

iskandarzulkarnaien commented Aug 22, 2019

Let's look at the Todo object for example. We could do validation in two ways:

  1. Validate input before passing it to the constructor (perhaps by having a method in Duke.java raise exception if description is blank).

  2. Validate user input in Todo constructor (perhaps by raising exception if this.description is null)

I'm thinking of whether option 2 would be more appropriate, since it results in the Todo class being responsible for not allowing "invalid" Todo objects to exist, as opposed to having the main class handle it or creating a new class specifically to validate inputs.

@uggi121
Copy link

uggi121 commented Aug 22, 2019

If I could opine in on this issue, I'd say both approaches have their pros and cons, which you've highlighted in the above comments. While Object-Level validations align with the essence of OOP, it could be a bit tedious to deal with similar cases in multiple objects. This will ultimately lead to a lot of redundant and repeated code.

A partial solution could be to handle common cases like white-space/empty inputs at the input-class level, leaving the object constructors to deal with more specific invalid inputs relevant to their associated objects.

To add on, we could abstract the input class at a higher level to a machine that accepts input. The machine has no idea what the valid input for other machines (objects) are. However, there are certain inputs that are invalid inputs for ALL objects. These can be handled at the input class level.

@iskandarzulkarnaien
Copy link
Author

Thanks for the response Sudharshan, really appreciate your contribution to the discussion.

About the input class, that makes a lot of sense. It just acts as a filter to remove inputs that are obviously gibberish or incomplete, while the respective objects deal with whether the input is "correct" (e.g. startTime < endTime).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants