Chuyển đến nội dung
Skillpedia
Lập trình chung280k · #191

java-coding-standards

Skill này làm gì

Công cụ này giúp đảm bảo code Java của bạn tuân theo thực hành tốt nhất và các quy ước, đặc biệt khi dùng framework Spring Boot hoặc Quarkus. Nó kiểm tra những việc như tên biến, cách lưu trữ dữ liệu và cách xử lý lỗi.

Khi nào nên dùng

  • ✓Khi viết code Java cho dự án Spring Boot hoặc Quarkus.
  • ✓Khi bạn muốn đảm bảo code dễ đọc và dễ bảo trì.
  • ✓Khi xem lại code của người khác để kiểm tra xem có tuân theo quy tắc không.

Liên kết skill

https://github.com/affaan-m/ECC/blob/main/pi/core/skills/java-coding-standards/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/affaan-m/ECC/blob/main/pi/core/skills/java-coding-standards/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Kiểm tra xem đoạn code Java Spring Boot này có tuân theo quy ước đặt tên và tính bất biến không.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Công cụ phân tích code của bạn và so sánh với một bộ quy tắc được định sẵn cho framework Spring Boot và Quarkus. Nếu có gì chưa đúng, nó gợi ý cách làm đúng.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Java Coding Standards

Standards for readable, maintainable Java (17+) code in Spring Boot and Quarkus services.

When to Use

  • Writing or reviewing Java code in Spring Boot or Quarkus projects
  • Enforcing naming, immutability, or exception handling conventions
  • Working with records, sealed classes, or pattern matching (Java 17+)
  • Reviewing use of Optional, streams, or generics
  • Structuring packages and project layout
  • [QUARKUS]: Working with CDI scopes, Panache entities, or reactive pipelines

How It Works

Framework Detection

Before applying standards, determine the framework from the build file:

  • Build file contains quarkus → apply [QUARKUS] conventions
  • Build file contains spring-boot → apply [SPRING] conventions
  • Neither detected → apply shared conventions only

Core Principles

  • Prefer clarity over cleverness
  • Immutable by default; minimize shared mutable state
  • Fail fast with meaningful exceptions
  • Consistent naming and package structure
  • [QUARKUS]: Favor build-time over runtime processing; avoid runtime reflection where possible

Examples

The sections below show concrete Spring Boot, Quarkus, and shared Java examples for naming, immutability, dependency injection, reactive code, exceptions, project layout, logging, configuration, and tests.

Naming

// 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: [QUARKUS] JAX-RS resources named as *Resource, not *Controller
public class MarketResource {}

// PASS: [SPRING] REST controllers named as *Controller
public class MarketController {}

Immutability

// 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
}

// PASS: [QUARKUS] Panache active-record entities use public fields (Quarkus convention)
@Entity
public class Market extends PanacheEntity {
  public String name;
  public MarketStatus status;
  // Panache generates accessors at build time; public fields are idiomatic here
}

// PASS: [QUARKUS] Panache MongoDB entities
@MongoEntity(collection = "markets")
public class Market extends PanacheMongoEntity {
  public String name;
  public MarketStatus status;
}

Optional Usage

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

// [QUARKUS] Panache
Optional<Market> market = Market.find("slug", slug).firstResultOptional();

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

Streams Best Practices

// 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

Dependency Injection

// PASS: [SPRING] Constructor injection (preferred over @Autowired on fields)
@Service
public class MarketService {
  private final MarketRepository marketRepository;

  public MarketService(MarketRepository marketRepository) {
    this.marketRepository = marketRepository;
  }
}

// PASS: [QUARKUS] Constructor injection
@ApplicationScoped
public class MarketService {
  private final MarketRepository marketRepository;

  @Inject
  public MarketService(MarketRepository marketRepository) {
    this.marketRepository = marketRepository;
  }
}

// PASS: [QUARKUS] Package-private field injection (acceptable in Quarkus — avoids proxy issues)
@ApplicationScoped
public class MarketService {
  @Inject
  MarketRepository marketRepository;
}

// FAIL: [SPRING] Field injection with @Autowired
@Autowired
private MarketRepository marketRepository; // use constructor injection

// FAIL: [QUARKUS] @Singleton when interception or lazy init is needed
@Singleton // non-proxyable — use @ApplicationScoped instead
public class MarketService {}

Reactive Patterns [QUARKUS]

// PASS: Return Uni/Multi from reactive endpoints
@GET
@Path("/{slug}")

…
Câu hỏi thường gặp

'Quy ước đặt tên' là gì?

Đây là các quy tắc về cách đặt tên biến, hàm và lớp để code dễ hiểu hơn. Ví dụ, trong Java, tên lớp thường bắt đầu bằng chữ in hoa.

'Tính bất biến' là gì?

Nghĩa là khi một dữ liệu đã được tạo thì không thể thay đổi. Dùng dữ liệu bất biến giúp code an toàn và dễ đoán hơn.

Tại sao công cụ phân biệt Spring Boot và Quarkus?

Vì mỗi framework có quy tắc và thực hành tốt nhất riêng, cần được tuân theo để đạt hiệu năng tối ưu và dễ bảo trì.

Chi tiết

Mô tả gốc của tác giả

Java coding standards for Spring Boot and Quarkus services: naming, immutability, Optional usage, streams, exceptions, generics, CDI, reactive patterns, and project layout. Automatically applies framework-specific conventions. Use when writing or reviewing Java in a Spring Boot or Quarkus service.

repo: affaan-m/ECC

Độ phổ biến

Sao GitHub
280k
Vị trí xếp hạng
#191
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự