7
\$\begingroup\$

This is a customer information data entry form. First name and Last name are required. In addition phone number, e-mail and birth date are used to check the database for existing customers with the same information.

Either of these fields (phone, e-mail or birth date) can be empty or filled in. In order to validate against database I need to check if the user has filled each field in or not.

Is there a way to refactor the code and still set a different property for each scenario?

The code looks like this:

var sFirstName = Inputs.GetProperty("sFirstName");
var sLastName = Inputs.GetProperty("sLastName");
var sEmail = Inputs.GetProperty("sEmail");
var sPhone = Inputs.GetProperty("sPhone");
var sBirthDate = Inputs.GetProperty("sBirthDate");
var sCoAddress = Inputs.GetProperty("sCoAddress");
var sAddress = Inputs.GetProperty("sAddress");
var sZip = Inputs.GetProperty("sZip");
var sCity = Inputs.GetProperty("sCity");

if(!sFirstName || !sLastName){
    TheApplication().RaiseErrorText("First name and last name are required");           
}

if(!sEmail && (!sCoAddress || !sAddress || !sZip || !sCity)){
    TheApplication().RaiseErrorText("Please enter either e-mail or postal address");
}
if(sEmail && sBirthDate && sPhone){
    Outputs.SetProperty("QueryAll","Y");
}
else if(sEmail && sBirthDate && !sPhone){
    Outputs.SetProperty("QueryEmailBday","Y");
}
else if(sEmail && !sBirthDate && sPhone){
    Outputs.SetProperty("QueryEmailPhone","Y");
}
else if(!sEmail && sBirthDate && sPhone){
    Outputs.SetProperty("QueryBdayPhone","Y");
}
else if(sEmail && !sBirthDate && !sPhone){
    Outputs.SetProperty("QueryEmail","Y");
}
else if(!sEmail && sBirthDate && !sPhone){
    Outputs.SetProperty("QueryBirthDate","Y");
}
else if(!sEmail && !sBirthDate && sPhone){
    Outputs.SetProperty("QueryPhone","Y");
}
else if(!sEmail && !sBirthDate && !sPhone){
    Outputs.SetProperty("QueryNameOnly","Y");
}
\$\endgroup\$
6
  • 3
    \$\begingroup\$ Welcome to Code Review! You've come to the right place, what you've written there is a maintainability-disaster waiting to happen. \$\endgroup\$
    – Mast
    Commented Feb 16, 2016 at 11:09
  • \$\begingroup\$ You might want to provide some information about Input and GetProperty. They don't look like native JS (they look like from a Windows platform given the capital-case syntax). Giving a bit of context about your system may allow for better solutions. \$\endgroup\$
    – Joseph
    Commented Feb 16, 2016 at 13:20
  • \$\begingroup\$ Can't you just query all the information all the time if you have their the required fields? \$\endgroup\$
    – BenVlodgi
    Commented Feb 16, 2016 at 14:30
  • \$\begingroup\$ This would be a lot easier if your Queries would have strict names. So QueryBday instead of QueryBirthDate - in that case you could just build the query name. \$\endgroup\$
    – Falco
    Commented Feb 16, 2016 at 15:00
  • \$\begingroup\$ What happens next? What do you do with QueryAll, QueryEmailBday, etc.? \$\endgroup\$ Commented Feb 16, 2016 at 21:17

2 Answers 2

1
\$\begingroup\$

You could solve this by constructing your Queryname dynamically:

var queryName = "Query";
if ( sEmail ) queryName += "Email";
if ( sBDay  ) queryName += "BDay";
if ( sPhone ) queryName += "Phone";

if ( queryName == "Query" ) queryName = "QueryNameOnly";

Outputs.SetProperty( queryName,"Y" );

But for this you would have to be a little more strict with your Parameter-Names not changing between BDay and BithDate and no Specialcase for ALL

\$\endgroup\$
0
0
\$\begingroup\$

If you are unable to (as @Falco commented, and answered,) change your "Property"^ names - you could also use a look-up table concept (implemented as a dictionary object):

var params = {
    "sEmail"        :   "Email",
    "sPhone"        :   "Phone",
    "sBirthDate"    :   "BDay"
};

^ Note: As @Falco implied, these are more correctly 'parameters' or arguments, than properties.

This makes it far more maintainable than a page of if-else blocks.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.