5

I have a list of strings and I have 10 unique methods doing different validations based on what string it is. For example this is what I have now:

if(name1 != null){
  validateName1();
}
else{
  throw Error();
}

 if(name2 != null){
  validateName2();
}
else{
  throw Error();
}
...

So instead of doing the same if-else check 10 times for all the different methods I was hoping I could store the names in a String list and have a for loop going through it in the following manner:

List<String> names = new ArrayList<String>();
for(name : names){
   if(name != null){
     validate[name]();
   }
   else{
     throw Error();
   }

Please let me know if this is possible, or if this is a rudimentary approach and there is a more elegant solution to my problem. To reiterate my problem is basically I have 10 different & unique validate methods and instead of writing if-else for each of them I want to be able to go through a for loop to call these methods with the new name.

Thanks in advance for the help!

3
  • 4
    define interface, create array/hashmap with objects which implement this interface and then you will be able to call array[i].method(value); Commented Aug 8, 2014 at 22:59
  • 1
    I think some sort of validation or testing framework would be a good idea here too. Commented Aug 8, 2014 at 23:09
  • I think this question is more related to CodeReview than StackOverflow: codereview.stackexchange.com Commented Aug 8, 2014 at 23:22

1 Answer 1

2

First, you have a different validator for every type of name. You say you have ten names:

public interface Name {
}

public class ShortName implements Name {
}

public class LongName implements Name {
}

public class MaidenName implements Name {
}

and so on. Then you have to validate a name.

public interface Name {
  public void validate();
}

which will make your other names throw compiler errors because they don't have the setName(...) method. So add it.

public class ShortName implements Name {
   private String name;

   public void validate() {
     // the first pass at validation
     if (name == null) {
        throw IllegalStateException("Name cannot be null");
     }
     // the second pass at validation
     if ("".equals(name)) {
        throw IllegalStateException("Name has a zero length");
     }
     // ok, the name is valid
   }
}

Once you have one of these for every name, you can now do this.

List<Name> names = getNames();

for (Name name : names) {
   names.validate();
}

and each type of name will run it's unique validator.

Sign up to request clarification or add additional context in comments.

3 Comments

This looks like it will work. Thanks for the help! I will let you know if I face any issues implementing this.
I thought I could simplify my code by writing less. This looks like it will be even more work to implement an interface and same amount of writing for all the methods. This is definitely an elegant solution, but my time is better spent on not trying to optimize/beautify my code using this methodology. Thanks for the answer, though!
@ItsANabhility I understand, just keep in mind that maintaining code is more expensive than writing it. While we all make trade-offs in developing code, when you look at solutions you should consider not the lines of code, but how that code will need to be modified to do expected future changes. After all, we complain about managers being incompetent because they use LOC as a metric, so we shouldn't then decide that LOC is a valid metric internally.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.