patternjavaMinor
Different methods of API that use similar Services
Viewed 0 times
methodsdifferentsimilarservicesthatuseapi
Problem
I have three methods in API running similar code snippets. For this reuse the code inserted a
It's good practice that? I mean, this isn't a high coupling and low cohesion? There is another better way?
API
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 = getAllFilesWithV
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 = getAllFilesWithV
Solution
-
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.
-
validate your parameters before creating unnecessary objects
-
The switch statement however is fine.
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.
-
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));-
The switch statement however is fine.
Code Snippets
Validacao validacao = validacaoDAO.findById(validacaoId);
if (validacao == null) {
throw new NotFoundException("Validacao cannot be null");
}
Permalink permalink = new Permalink().setId(Util.getMd5Time(validacaoId));Context
StackExchange Code Review Q#90674, answer score: 5
Revisions (0)
No revisions yet.