I was doing some reading here(1,2) and I was wondering if making sure the parameters provided during initialization are correct, violates the guideline that constructors shouldn't do work.
For example (Python):
class Employee:
def __init__(self, empFirstname, empLastname, empEmail):
self._validate_employee(empFirstname, "First Name")
self._validate_employee(empLastname, "Last name")
self._validate_employee(empEmail, "Email")
self._validate_email(empEmail, "Email")
self.empFirstname = empFirstname
self.empLastname = empLastname
self.empEmail = empEmail
@property
def email(self):
return self.empEmail
def _validate_employee(self, parameter, error_message):
if not parameter:
raise TypeError("{0} {1}" .format(error_message, "is missing"))
def _validate_email(self, email, parameter):
if "@" not in email or "." not in email:
raise TypeError("{0} {1}" .format(parameter, " is invalid"))
In my example, I check to make sure the first and last names aren't blank and that the email is valid. Did I violate the guideline?
Update: I'm not asking if it should throw, I'm asking if I'm violating the guideline that constructors shouldn't do work when it validates my parameters.