Java Coding Standards

affaan-m/ECC/docs/ja-JP/skills/java-coding-standards

by affaan-mef648e01899ba3e8dc6371642deaaf64b4477775No license275K starsListed Oct 9, 2026Updated Oct 9, 2026Repository updated 4 days ago

Spring Bootサービス向けのJavaコーディング標準:命名、不変性、Optional使用、ストリーム、例外、ジェネリクス、プロジェクトレイアウト。

Instructions onlySoftware Development
AI-generated overview

Defines Java 17+ coding standards for Spring Boot services, covering naming, immutability, Optional, streams, exceptions and layout.

What it does
This skill supplies a written set of Java coding standards for Spring Boot services, aimed at readable and maintainable Java 17+ code. It covers core principles, naming conventions, immutability with records and final fields, Optional usage, stream best practices, exception handling, generics and type safety, Maven/Gradle project layout, formatting, code smells, logging, null handling and testing expectations. It produces guidance and code examples rather than generated code or files.
When to use it
Use it when writing, reviewing or refactoring Java code in a Spring Boot service and a consistent team standard is needed. It also fits onboarding or style discussions where naming, immutability, exception and logging conventions must be settled.
Requirements
No tools, packages or credentials are required; it is instructions only and ships no scripts. The conventions target Java 17+ and Spring Boot projects, and mention Maven or Gradle layouts, JUnit 5, AssertJ and Mockito as context.

Javaコーディング標準

Spring Bootサービスにおける読みやすく保守可能なJava(17+)コードの標準。

核となる原則

  • 巧妙さよりも明確さを優先
  • デフォルトで不変; 共有可変状態を最小化
  • 意味のある例外で早期失敗
  • 一貫した命名とパッケージ構造

命名

java
// PASS: クラス/レコード: PascalCasepublic class MarketService {}public record Money(BigDecimal amount, Currency currency) {}
// PASS: メソッド/フィールド: camelCaseprivate final MarketRepository marketRepository;public Market findBySlug(String slug) {}
// PASS: 定数: UPPER_SNAKE_CASEprivate static final int MAX_PAGE_SIZE = 100;

不変性

java
// PASS: recordとfinalフィールドを優先public record MarketDto(Long id, String name, MarketStatus status) {}
public class Market {  private final Long id;  private final String name;  // getterのみ、setterなし}

Optionalの使用

java
// PASS: find*メソッドからOptionalを返すOptional<Market> market = marketRepository.findBySlug(slug);
// PASS: get()の代わりにmap/flatMapを使用return market    .map(MarketResponse::from)    .orElseThrow(() -> new EntityNotFoundException("Market not found"));

ストリームのベストプラクティス

java
// PASS: 変換にストリームを使用し、パイプラインを短く保つList<String> names = markets.stream()    .map(Market::name)    .filter(Objects::nonNull)    .toList();
// FAIL: 複雑なネストされたストリームを避ける; 明確性のためにループを優先

例外

  • ドメインエラーには非チェック例外を使用; 技術的例外はコンテキストとともにラップ
  • ドメイン固有の例外を作成(例: MarketNotFoundException)
  • 広範なcatch (Exception ex)を避ける(中央でリスロー/ログ記録する場合を除く)
java
throw new MarketNotFoundException(slug);

ジェネリクスと型安全性

  • 生の型を避ける; ジェネリックパラメータを宣言
  • 再利用可能なユーティリティには境界付きジェネリクスを優先
java
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.ymlsrc/test/java/... (mainをミラー)

フォーマットとスタイル

  • 一貫して2または4スペースを使用(プロジェクト標準)
  • ファイルごとに1つのpublicトップレベル型
  • メソッドを短く集中的に保つ; ヘルパーを抽出
  • メンバーの順序: 定数、フィールド、コンストラクタ、publicメソッド、protected、private

避けるべきコードの臭い

  • 長いパラメータリスト → DTO/ビルダーを使用
  • 深いネスト → 早期リターン
  • マジックナンバー → 名前付き定数
  • 静的可変状態 → 依存性注入を優先
  • サイレントなcatchブロック → ログを記録して行動、または再スロー

ログ記録

java
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 Validation(@NotNull、@NotBlank)を使用

テストの期待

  • JUnit 5 + AssertJで流暢なアサーション
  • モック用のMockito; 可能な限り部分モックを避ける
  • 決定論的テストを優先; 隠れたsleepなし

覚えておく: コードを意図的、型付き、観察可能に保つ。必要性が証明されない限り、マイクロ最適化よりも保守性を最適化します。

Source and attribution

Source:affaan-m/ECCindocs/ja-JP/skills/java-coding-standardsat commitef648e0

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal