JA

java-coding-standards

Provides coding standards for maintainable Java 17+ Spring Boot development.

Install

mkdir -p .claude/skills/java-coding-standards && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/709" && unzip -o skill.zip -d .claude/skills/java-coding-standards && rm skill.zip

Installs to .claude/skills/java-coding-standards

Activation

This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.

Spring Boot服务的Java编码标准:命名、不可变性、Optional用法、流、异常、泛型和项目布局。
55 charsno explicit “when” trigger
Intermediate

Key capabilities

  • Enforce naming conventions for classes and methods
  • Promote immutability using Java records and final fields
  • Standardize Optional usage in service layers
  • Implement consistent exception handling with domain-specific exceptions
  • Define standard project package layout

How it works

The skill provides a set of coding standards and patterns for Spring Boot services, focusing on modern Java features like records and pattern matching to ensure clarity and maintainability.

Inputs & outputs

You give it
Java source code
You get back
Refactored code snippets or architectural guidance

When to use java-coding-standards

  • Refactoring service code to use Optional instead of null
  • Standardizing project package and directory structure
  • Implementing consistent exception handling in service layers
  • Updating legacy classes to Java 17+ records

About this skill

Java 编码规范

适用于 Spring Boot 服务中可读、可维护的 Java (17+) 代码的规范。

何时激活

  • 在 Spring Boot 项目中编写或审查 Java 代码时
  • 强制执行命名、不可变性或异常处理约定时
  • 使用记录类、密封类或模式匹配(Java 17+)时
  • 审查 Optional、流或泛型的使用时
  • 构建包和项目布局时

核心原则

  • 清晰优于巧妙
  • 默认不可变;最小化共享可变状态
  • 快速失败并提供有意义的异常
  • 一致的命名和包结构

命名

// PASS: Classes/Records: PascalCase
public class MarketService {}
public record Money(BigDecimal amount, Currency currency) {}

// PASS: Methods/fields: camelCase
private final MarketRepository marketRepository;
public Market findBySlug(String slug) {}

// PASS: Constants: UPPER_SNAKE_CASE
private static final int MAX_PAGE_SIZE = 100;

不可变性

// PASS: Favor records and final fields
public record MarketDto(Long id, String name, MarketStatus status) {}

public class Market {
  private final Long id;
  private final String name;
  // getters only, no setters
}

Optional 使用

// PASS: Return Optional from find* methods
Optional<Market> market = marketRepository.findBySlug(slug);

// PASS: Map/flatMap instead of get()
return market
    .map(MarketResponse::from)
    .orElseThrow(() -> new EntityNotFoundException("Market not found"));

Streams 最佳实践

// PASS: Use streams for transformations, keep pipelines short
List<String> names = markets.stream()
    .map(Market::name)
    .filter(Objects::nonNull)
    .toList();

// FAIL: Avoid complex nested streams; prefer loops for clarity

异常

  • 领域错误使用非受检异常;包装技术异常时提供上下文
  • 创建特定领域的异常(例如,MarketNotFoundException
  • 避免宽泛的 catch (Exception ex),除非在中心位置重新抛出/记录
throw new MarketNotFoundException(slug);

泛型和类型安全

  • 避免原始类型;声明泛型参数
  • 对于可复用的工具类,优先使用有界泛型
public <T extends Identifiable> Map<Long, T> indexById(Collection<T> items) { ... }

项目结构 (Maven/Gradle)

src/main/java/com/example/app/
  config/
  controller/
  service/
  repository/
  domain/
  dto/
  util/
src/main/resources/
  application.yml
src/test/java/... (mirrors main)

格式化和风格

  • 一致地使用 2 或 4 个空格(项目标准)
  • 每个文件一个公共顶级类型
  • 保持方法简短且专注;提取辅助方法
  • 成员顺序:常量、字段、构造函数、公共方法、受保护方法、私有方法

需要避免的代码坏味道

  • 长参数列表 → 使用 DTO/构建器
  • 深度嵌套 → 提前返回
  • 魔法数字 → 命名常量
  • 静态可变状态 → 优先使用依赖注入
  • 静默捕获块 → 记录日志并处理或重新抛出

日志记录

private static final Logger log = LoggerFactory.getLogger(MarketService.class);
log.info("fetch_market slug={}", slug);
log.error("failed_fetch_market slug={}", slug, ex);

Null 处理

  • 仅在不可避免时接受 @Nullable;否则使用 @NonNull
  • 在输入上使用 Bean 验证(@NotNull, @NotBlank

测试期望

  • 使用 JUnit 5 + AssertJ 进行流畅的断言
  • 使用 Mockito 进行模拟;尽可能避免部分模拟
  • 倾向于确定性测试;没有隐藏的休眠

记住:保持代码意图明确、类型安全且可观察。除非证明有必要,否则优先考虑可维护性而非微优化。

When not to use it

  • Projects not using Spring Boot
  • Legacy Java versions below 17

Prerequisites

Java 17+Spring Boot

Limitations

  • Focuses primarily on Spring Boot services
  • Requires manual application of standards

How it compares

It provides a prescriptive set of rules for modern Java development that prioritizes maintainability and type safety over generic coding practices.

Compared to similar skills

java-coding-standards side by side with the closest alternatives in the catalog.

SkillInstallsUpdatedSafetyDifficulty
java-coding-standards (this skill)164moNo flagsIntermediate
java-springboot04moNo flagsBeginner
java-pro344moNo flagsAdvanced
springboot-tdd55moNo flagsIntermediate

Try saying

Example prompts that trigger this skill in your AI assistant.

You might also like

java-springboot

carlosroco

Genera la estructura completa de un microservicio Java Spring Boot 3.x con Maven. Úsalo cuando necesites crear el proyecto base (pom.xml, clase principal), la capa de servicio (interfaz + implementación) o el controlador REST.

00

java-pro

sickn33

Master Java 21+ with modern features like virtual threads, pattern matching, and Spring Boot 3.x. Expert in the latest Java ecosystem including GraalVM, Project Loom, and cloud-native patterns. Use PROACTIVELY for Java development, microservices architecture, or performance optimization.

3492

springboot-tdd

affaan-m

Test-driven development for Spring Boot using JUnit 5, Mockito, MockMvc, Testcontainers, and JaCoCo. Use when adding features, fixing bugs, or refactoring.

531

jpa-patterns

affaan-m

JPA/Hibernate patterns for entity design, relationships, query optimization, transactions, auditing, indexing, pagination, and pooling in Spring Boot.

322

backend-microservice-development

TencentBlueKing

后端微服务开发规范,涵盖目录结构、分层架构(API/Service/DAO)、依赖注入、配置管理、Spring Boot 最佳实践。当用户进行后端开发、创建新微服务、编写 Kotlin/Java 代码或设计服务架构时使用。

213

microservice-infrastructure

TencentBlueKing

微服务基础设施指南,涵盖条件配置、事件驱动架构、服务间通信、国际化与日志等微服务架构的核心基础设施。当用户实现服务间调用、配置多环境、实现异步通信、处理国际化或规范日志输出时使用。

411

Search skills

Search the agent skills registry