Elegant validation?
-
Given the following code:
private String name;
private List<String> years;
private List<String> studyChoices;private void doCheck() { if (!name.isEmpty() || name == null) { } for (String year : years) { if (!year.isEmpty() || year == null) { } } for (String studyChoice : studyChoices) { if (!studyChoice.isEmpty() || studyChoice == null){ } } }
How do I condense this code into a more elegant and OO solution? Maybe put it all in one list and check for each item in that list? I would like something like: 'If any of these parameters were not passed in or if they are empty, output an error message, else give the desired output'. Basically I have to write a Servlet that gets form parameters from the request and checks if all parameters were provided. If they were, it is then supposed to write an output page. Any ideas? Thanks
-
Given the following code:
private String name;
private List<String> years;
private List<String> studyChoices;private void doCheck() { if (!name.isEmpty() || name == null) { } for (String year : years) { if (!year.isEmpty() || year == null) { } } for (String studyChoice : studyChoices) { if (!studyChoice.isEmpty() || studyChoice == null){ } } }
How do I condense this code into a more elegant and OO solution? Maybe put it all in one list and check for each item in that list? I would like something like: 'If any of these parameters were not passed in or if they are empty, output an error message, else give the desired output'. Basically I have to write a Servlet that gets form parameters from the request and checks if all parameters were provided. If they were, it is then supposed to write an output page. Any ideas? Thanks
First thing is that all your tests are the wrong way round (and logically wrong I suspect), you should check the variable is not
null
before using it as a reference to a method thus:if (name == null || name.isEmpty()) {
// or maybe you really mean
if (name != null && !name.isEmpty()) {I am not sure how you would condense the two loops any more as they appear to be independent of each other.
-
Given the following code:
private String name;
private List<String> years;
private List<String> studyChoices;private void doCheck() { if (!name.isEmpty() || name == null) { } for (String year : years) { if (!year.isEmpty() || year == null) { } } for (String studyChoice : studyChoices) { if (!studyChoice.isEmpty() || studyChoice == null){ } } }
How do I condense this code into a more elegant and OO solution? Maybe put it all in one list and check for each item in that list? I would like something like: 'If any of these parameters were not passed in or if they are empty, output an error message, else give the desired output'. Basically I have to write a Servlet that gets form parameters from the request and checks if all parameters were provided. If they were, it is then supposed to write an output page. Any ideas? Thanks
Is this entered from a GUI or is this object coming from some API? For a GUI you can also use a GUI sided validation. You can e.g. use a KeyListener and validate the entered value right away. For an API side validation I would suggest a Validation Layer which has to be passed before the data is used. Your code - as Richard already stated - is a bit mixed up. Here is a valid suggestion:
private void doCheck() {
if (name != null && name != "") { // always check first for null
for (String year : years) { // years only need to be checked when name was ok
if (year != null && year != "" ) {
try{
int i = Integer.parseInt(year);
// you can check if the number has a valid value here
// (e.g. between 1900 and 2012 (now-18 for adult check)
}
catch(Exception oException){
/* not a valid number */
}
}
}
for (String studyChoice : studyChoices) {
if (studyChoice!=null && studyChoice != ""){
/*Check for valid study course here - I suggest an Enum for the fixed range of valid values*/
}
}
}
else{ /*not even a valid name is given*/ }
}regards Torsten When I'm not working