Less indentation for switch expression in assignment/initializer
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 42/100
Línea de trabajo
Comienza localizando la lógica del formatter y las pruebas existentes para switch expressions; después, compara los ejemplos de asignación actuales y propuestos en este issue con el formato de return-switch. El trabajo estará terminado cuando las switch expressions que aparecen inmediatamente a la derecha en asignaciones y declaraciones de variables usen la indentación propuesta sin cambiar los casos de anidamiento más profundo, y exista cobertura de regresión para los ejemplos.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Java
- Estrellas
- 6.2k
- Forks
- 936
- Merge medio
- 6 min
- PR fusionados (30 d)
- 3
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de google/google-java-format
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
google/google-java-format#1094 · 1 comentario ·
-
Complementary Teamups Abierto
Dificultad 5/5 Más de una semana Aptitud para principiantes 15/100
google/google-java-format#1450 · 1 reacción ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 55/100
google/google-java-format#1439 · 1 comentario ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 62/100
google/google-java-format#1436 · 2 comentarios ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 48/100
google/google-java-format#1428 · 3 comentarios ·
Todos los issues de google/google-java-format
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
elastic/gradle-plugins#157 ·
-
enhancement Tools
Dificultad 1/5 Menos de una hora Aptitud para principiantes 75/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
apache/rocketmq-dashboard#5008 ·
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
DETECT_PARAMETER_NAMES=false silently disables @ConstructorProperties-based Creator detection too Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
FasterXML/jackson-databind#6229 ·