3
\$\begingroup\$

I have three methods in API running similar code snippets. For this reuse the code inserted a switch-case according to the API method.
It's good practice that? I mean, this isn't a high coupling and low cohesion? There is another better way?

API

@ApiOperation(value = "Gerar permalinks de todos os arquivos com validação", response = Permalink.class)
    @RequestMapping(value = "{validacaoId}/permalink/all", method = RequestMethod.POST)
    public Permalink gerarPermalinkComValidacao(@PathVariable("validacaoId") String validacaoId) {
        return permalinkService.createPermalink(validacaoId, "Permalink com validação");
    }

    @ApiOperation(value = "Gerar permalinks de todos os arquivos NOKS com validação", response = Permalink.class)
    @RequestMapping(value = "{validacaoId}/permalink/noks", method = RequestMethod.POST)
    public Permalink gerarPermalinkNoks(@PathVariable("validacaoId") String validacaoId) {
        return permalinkService.createPermalink(validacaoId, "Permalink NOKS com validação");
    }

    @ApiOperation(value = "Gerar permalinks de todos os arquivos sem validação", response = Permalink.class)
    @RequestMapping(value = "{validacaoId}/permalink", method = RequestMethod.POST)
    public Permalink gerarPermalinkSemValidacao(@PathVariable("validacaoId") String validacaoId) {
        return permalinkService.createPermalink(validacaoId, "Permalink sem validação");
    }

Service

public Permalink createPermalink(String validacaoId, String tipo) {
        Validacao validacao = validacaoDAO.findById(validacaoId);
        Permalink permalink = new Permalink().setId(Util.getMd5Time(validacaoId));
        if (validacao == null) {
            throw new NotFoundException("Erro ao gerar permalink: ID da validação inválido.");
        }
        permalinkDAO.createPermalinkDirectory(validacao.getId());
        BasicDBList pecas;
        switch (tipo) {
            case "Permalink com validação":
                pecas = getAllFilesWithValidation(validacao.getId(), permalink.getId());
                break;
            case "Permalink NOKS com validação":
                pecas = noksWithValidation(validacao.getId());
                break;
            case "Permalink sem validação":
                pecas = getAllFiles(validacao.getId(), permalink.getId());
                break;
            default:
                throw new IllegalArgumentException("Tipo de permalink inválido.");
        }
        if (!pecas.isEmpty()) {
            permalink.setEmail(validacao.getEmail())
                    .setLogo(permalinkDAO.getLogo(validacao.getInstancia()).getString("logo", ""))
                    .setDataCriacao(DateTime.nowISODate())
                    .setTipo(tipo)
                    .setValidacaoId(validacao.getId())
                    .setInstancia(validacao.getInstancia())
                    .setPecas(pecas);
            permalinkDAO.create(permalink);
            return permalink;
        }
        throw new InternalServerErrorException("Erro ao obter peças para permalink.");
    }
\$\endgroup\$
1
  • \$\begingroup\$ I recommend you to use Strategy in this case. What if you need to add one more validation? What if you need to remove some validation? en.wikipedia.org/wiki/Strategy_pattern \$\endgroup\$ Commented May 13, 2015 at 21:36

1 Answer 1

5
\$\begingroup\$
  1. Use English for your variable names, request routes, and Exception messages. Imagine a remote developer joins your company (very common these days), they wouldn't be able to understand the stack traces.

  2. validate your parameters before creating unnecessary objects

    Validacao validacao = validacaoDAO.findById(validacaoId);
    if (validacao == null) {
        throw new NotFoundException("Validacao cannot be null");
    }
    Permalink permalink = new Permalink().setId(Util.getMd5Time(validacaoId));
    
  3. The switch statement however is fine.

\$\endgroup\$
3
  • \$\begingroup\$ The NotFoundException is fine when the passed-in ID isn't found; the parameter from outside isn't null. \$\endgroup\$ Commented May 13, 2015 at 19:45
  • \$\begingroup\$ @ferada I removed the second point, I think you are right \$\endgroup\$ Commented May 13, 2015 at 19:51
  • \$\begingroup\$ I think much better place in English but it will be available only to Brazilian people and is a convention on the team use Portuguese \$\endgroup\$ Commented May 14, 2015 at 11:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.