dimanche 20 mai 2018

Clean way to avoid conditions

I've got a code like this:

 Map<String, String> args = new HashMap<>();

    args.put("-T", "Tom Sawyer");
//        args.put("-I", "1112223334");

    if (args.containsKey("-T")) {
        Book book = libraryService.findBookByTitle(args.get("-T"));
    } else {
        Book book = libraryService.findBookByIsbn(args.get("-I"));                       
    }

Is there a clean way to avoid ifs? I want my code to decide whether it has to use argument -I or -T. I handled situations when args don't have any of it, I just simplified code for StackOverflow.

Aucun commentaire:

Enregistrer un commentaire