Is there any better and optimized way to write this code?
public int ValidateEntries()
{
if (this.CompanyName.Length < 6)
{
MessageBox.Show("Company name must be of at least six characters.", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error);
return 1;
}
if (DateTime.Parse(this.FYStarting) > DateTime.Parse(this.FYEnding))
{
MessageBox.Show("Invalid financial year period.", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error);
return 1;
}
return 0; //Default error code: 0 : No Error, 1 : Error
}
I want to introduce try..catch block just in case any error fires. But it’s going to create an overhead. I also want to get rid of these if s.
Thirdly, which is more optimized: Convert.ToDateTime or DateTime.parse?
Declarative validation is an optimum way, Use the appropriate Validation framework that works for you (Like what you can find in Entlib)
You must get rid off current solution and handle cross cutting concerns in a cleaner, easier and consistent way.