Less indentation for switch expression in assignment/initializer
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 42/100
Piste de recherche
Commencez par localiser la logique du formatter et les tests existants pour les switch expressions, puis comparez les exemples d’affectation actuels et proposés dans cette issue avec le formatage de return-switch. Le travail est terminé lorsque les switch expressions situées immédiatement à droite dans les affectations et les déclarations de variables utilisent l’indentation proposée sans modifier les cas d’imbrication plus profonde, avec une couverture de régression pour les exemples.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- Java
- Étoiles
- 6.2k
- Forks
- 940
- Merge moyen
- 5 min
- PR mergées (30 j)
- 8
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de google/google-java-format
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
google/google-java-format#1094 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 4/5 3-5 jours Accessibilité débutants 35/100
google/google-java-format#1470 · 5 réactions ·
Les mainteneurs répondent en général sous 1 jour
-
IndexOutOfBoundsException - Wrong formatted content when dealing with latex StringPeut-être à nouveau libre @Amlan2000 l’a pris il y a 57 jours, et aucune pull request n’est ouverte. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 55/100
google/google-java-format#1439 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
unused import removal leaves extra blank line between package declaration and class Javadoc / declarationPeut-être pris @arimu1 l’a pris il y a 66 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 62/100
google/google-java-format#1436 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Eclipse
Difficulté 3/5 1-2 jours Accessibilité débutants 48/100
google/google-java-format#1428 · 4 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de google/google-java-format
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 64/100
utopia-rise/godot-jvm#1004 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
spring-projects/spring-grpc#442 ·
-
Expose numberOfPermits in RateLimiterEvent.toString() and the ratelimiterevents actuator DTOPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
resilience4j/resilience4j#2547 ·
Les mainteneurs répondent en général sous 9 jours
-
Clock.MakeDate continues execution and returns a rolled-over instant after dispatching error on invalid datePeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 82/100
mit-cml/appinventor-sources#4155 ·
Les mainteneurs répondent en général sous 1 jour