1

When I instantiate a new object, what should I do with the parameters that get passed in in terms of validation?

I'm guessing it's not safe to just assume the parameters coming in are fine, however I thought validation should be separated into another layer?

For example, if a class takes a start time and and end time the things the class should require:

  • start_time and end_time are both valid datetime objects
  • start_time should be before end_time

Any direction with what is okay to validate in class constructors, if anything at all as well as any general information about where validation should take place would be helpful.

It might be worth noting I'm trying to work with an MVC structure.

tereško
  • 56,151
  • 24
  • 92
  • 147
Matt
  • 5,207
  • 23
  • 73
  • 111

2 Answers2

0

You tagged the question as both and . Both are loosely type languages that won't enforce your variables and orguments to be of a specific type. Depending on what you're going to do with those values, that's just fine. Some other times, you have to check, or your code will produce an error. And both languages provide ways to do that.

To answer your question: yes, it is okay to validate your data on a constructor. But it's also boring and messy, so don't do that everywhere. When exactly you should do that or not depends on how your code is structured, and how you handle errors and exceptions on a higher level.

bfavaretto
  • 69,385
  • 15
  • 102
  • 145
  • 1. Can you define both types of validation? You've defined that I need to account for errors in invalid data passed in... but how does the other differ and what's an example of when I might use it? – Matt Aug 30 '12 at 03:45
  • 2. I'm actually using node. In which case after `start_time` and `end_time` are passed in I should check if they're valid datetimes? (since js doesn't seem to allow you to require types like that) – Matt Aug 30 '12 at 03:46
0

Validation of data is responsibility of domain objects, which are part of the model layer.

And no, you should not put any computation in the constructors. Constructor should only assign values to local variables. You should either validate information in setters, or by using a separate public method.

Also, you should not confuse validation with data integrity checks (like "users must have unique emails"). Those should be performed by the storage abstractions structures (like data mappers) and/or enforced by your storage medium. When you are saving the information from domain object, your storage abstraction structures might encounter an error. In that case they assign an error state to the domain object.

Depending on the structure of your application, the error state (both ones caused by validation problems and data integrity conflicts) can be handles immodestly or stored somewhere temporarily, for example: in session.

tereško
  • 56,151
  • 24
  • 92
  • 147
  • Sorry I'm not familiar with some of the terms you've used here and where they fit within MVC. Can you clarify where data mappers come into play? Is the domain object just a version of the object used to save to the database? – Matt Sep 01 '12 at 18:19
  • @Matt, data mappers are structures, which know how to store and retrieve data from storage (that might or might not be an SQL database). Maybe [this](http://stackoverflow.com/a/11943107/727208) helps a bit. – tereško Sep 01 '12 at 18:59