Receber por email
← Voltar para o catálogo

java-best-practices

Java coding best practices. Use when writing or reviewing Java code (17+). Covers modern features, error handling, and patterns.

87estrelas
Atualizado há 7 meses

Ver no GitHub ↗ Licença: MIT

Como adicionar

/plugin marketplace add Taoidle/plan-cascade

O comando exato pode variar conforme o repositório. Confira o README no GitHub.

Para o autor da skill

Cole no README do seu repo

Mostra que sua skill está catalogada na Skillteca, gera backlink e tráfego rastreável.

[![Skillteca](https://www.skillteca.com.br/api/badge/java-best-practices/svg)](https://www.skillteca.com.br/skills/java-best-practices)
#ai

Alerta por categoria

Receba novas skills de Escrita e Conteúdo toda segunda

1 email curto, só com as skills novas de Escrita e Conteúdo. 4 minutos de leitura, sem spam, cancela com 1 clique.

Você confirma o email no primeiro envio. Sem spam. Cancela com 1 clique.

Ler descrição completa

Java Best Practices (17+)

Code Style

| Rule | Guideline | |------|-----------| | Formatter | google-java-format | | Analysis | SpotBugs, Error Prone | | Naming | Clear, no abbreviations |

Modern Features

| Feature | Usage | |---------|-------| | Records | Immutable data carriers | | Sealed classes | Restricted hierarchies | | Pattern matching | instanceof with binding | | var | Local type inference |

public record User(String id, String name) {}

if (obj instanceof String s && !s.isEmpty()) { process(s); }

public sealed interface Result<T> permits Success, Failure {}
record Success<T>(T value) implements Result<T> {}
record Failure<T>(Exception error) implements Result<T> {}

Error Handling

| Rule | Guideline | |------|-----------| | Specific exceptions | Not bare Exception | | Try-with-resources | For AutoCloseable |

try (var reader = new BufferedReader(new FileReader(path))) {
    return reader.lines().toList();
} catch (IOException e) {
    throw new ConfigException("Failed: " + path, e);
}

Project Structure

src/main/java/com/example/{domain,service}/
src/test/java/
pom.xml or build.gradle

Common Patterns

| Pattern | Usage | |---------|-------| | Optional | Null-safe returns | | Stream API | Functional collections | | Builder | Complex construction |

public Optional<User> findById(String id) {
    return Optional.ofNullable(users.get(id));
}

Anti-Patterns

| Avoid | Use Instead | |-------|-------------| | Returning null | Optional<T> | | Mutable data | Records | | instanceof chains | Pattern matching |

Testing (JUnit 5 + AssertJ)

@Test void shouldProcess() {
    var service = new Service(mockRepo);
    var result = service.process(input);
    assertThat(result.status()).isEqualTo(SUCCESS);
}
Compartilhar X LinkedIn