Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Less indentation for switch expression in assignment/initializer

オープン
#1,255 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
42/100
issue の種類
機能追加
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
java
領域
tooling

調査の方向性

まず formatter ロジックと switch expressions の既存テストを見つけ、次に、この issue にある現在の代入例と提案された代入例を return-switch のフォーマットと比較します。作業完了の条件は、代入および変数宣言で右辺に直接置かれた switch expressions が提案されたインデントを使用し、より深いネストのケースを変更せず、例に対するリグレッションカバレッジが追加されることです。

索引モデルが issue の本文から書いたものです。

説明

I would like to suggest a different formatting of switch expressions that are used as right-hand side of an assignment: move the switch on the same line as the assignment and reduce indentation, in the same way as is done for switch expressions after a return. I am aware that this deviates slightly from the basic rules of Google Java Format, but I think this case is worth an exception.

Example code with current formatting:

  int returnSwitchExpression(int i) {
    return switch (i) {
      case 0 -> 1;
      case 1 -> 2;
      default -> 3;
    };
  }

  int assignSwitchExpression(int i) {
    int result =
        switch (i) {
          case 0 -> 1;
          case 1 -> 2;
          default -> 3;
        };
    return result;
  }

  int switchStatement(int i) {
    switch (i) {
      case 0 -> {
        return 1;
      }
      case 1 -> {
        return 2;
      }
      default -> {
        return 3;
      }
    }
  }

Proposed formatting:

  int assignSwitchExpression(int i) {
    int result = switch (i) {
      case 0 -> 1;
      case 1 -> 2;
      default -> 3;
    };
    return result;
  }

Note how the indentation of the switch cases in the assignment is currently different from both the return switch and the switch statement cases. Conceptually and syntactically the switch assignment is quite similar to the return switch, though, and it would make sense to let it look similarly. If the current formatting of the return switch case is considered fine, then certainly the proposed formatting should be similarly ok for readability and clarity of the code.

A concrete advantage of changing the formatting is that refactorings introduce less diff noise: both the a refactoring from a switch statement to a switch expression in an assigment as well as the refactoring of a switch expression out of a return into an assignment would keep the same indentation. Right now such refactorings necessarily change the indentation and typically lead to the complete switch being shown as changed in a diff. Note that for example Google Error Prone by default warns about such potential refactorings of switch statements into switch expressions, and Eclipse has a similar refactoring, so these are not that rare.

To be clear: I am not arguing for changing any formatting related to switch expressions in different places or those that are nested more deeply in an expression, my proposal is only about the case where the switch expression is the immediate right-hand-side child node of an assignment or variable declaration.

I also do not think that this would lead to problematic inconsistencies between switch expressions in assignments and initializers and those used elsewhere, because I would assume that switch expressions are almost exclusively used in the places discussed here and almost never nested more deeply. For example, the automated refactorings by Google Error Prone only introduce such basic cases and not deeper nestings.

Switch expressions are also syntactically special anyway compared to other expressions (e.g. because they always enforce some line breaks in the expression), so some inconsistency with regards to other expressions could be accepted.

So in summary I would think introducing this special case has a concrete benefit, no readability disadvantage, and does not introduce relevant inconsistencies.

主要言語
Java
スター
6.2k
フォーク
936
平均マージ
6分
マージ済み PR(30日)
3

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

google/google-java-format のほかの issue

google/google-java-format の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。