Using C#/.Net, I have a page where the user can enter data and save these data by clicking a button.

To save data, the user needs to enter a valid date, this means a date which has a certain format.

The page contains amongst others two modules.

The ValidateDate-Event has the responsibility two check the format of the date-field. So it checks the format and displays a message if the date is not well formed.

The Save-Event has to save user entries. To successfully complete the task, a well formed date is needed. So I designed the module to check the date format. This has the advantage that the module can never crash and is not dependent of the Validation-module. But I realized that in fact I do the same thing twice, in two modules.

I am just curious, how do you handle this matter? What do you think is the best way to do it?

1) ValidateDate

Reponsibility: Check format of date field (, inform user)
Sub-Task: Check format of date field

2) Save

Reponsibility: Save data
Sub-Task: Check format of date field

1 Answer 1


Doing the same thing twice is nearly always a bad idea. (see DRY) You have to change it in two places if your validation changes increasing your chance of bugs.

My advice would be have both Save and Validate call another method that validates the input. Its still a good idea to check on save as well as validate as you should never trust user input. They could for example be malicious or there could be some bizzare browser bug causing/allowing them to submit bad data.

  • 2
    +1 In short, refactor date format check to another method.
    – Lionel
    Oct 18, 2011 at 10:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.