Reputation: 473
I'm writing a user interface in JavaFX and would prefer not use FXML. Is it conventionally and efficiently sound to organize children using braces as follows? The braces serve no purpose other than to make the code look better. Will the braces be entirely optimized away when compiled?
HBox top = new HBox();
{
Label white = new Label("White");
white.getStyleClass().addAll("white-check");
white.setMinWidth(160);
white.setMinHeight(60);
Label lightGray = new Label("Light Gray");
lightGray.getStyleClass().addAll("lightgray-check");
lightGray.setMinWidth(160);
lightGray.setMinHeight(60);
top.getChildren().addAll(white, lightGray);
}
Let me know if I should migrate this to programmers SE.
Thanks.
Upvotes: 1
Views: 90
Reputation: 159331
Answer
I think your proposal is OK from a technical viewpoint.
Opinion
From a stylistic view point the additional braces when none are really required is just a little strange to me because I have never seen anybody write code like that. However, it does read OK, has no real technical overhead and, when you consider some of the alternatives below, it doesn't require a radically different approach like a DSL or additional syntax and type constructs like creating additional methods or classes.
The more standard way I've seen stuff done is either via factory methods or inheritance. For example:
Alternatives
Builders
If builders were still around, the natural thing to do would be to use them - however builders are now deprecated, so they aren't a viable alternative. When the JavaFX team dropped builders, there were numerous replacement proposals (which you can find by following all of the posts on the builder dropping thread), but I don't think a true standard drop-in replacement was ever really found, just different options.
Factory Method
HBox top = new HBox(
createLabel("White", "white-check"),
createLabel("Light Gray", "lightgray-check")
);
. . .
public Label createLabel(String text, String styleClass) {
Label label = new Label(text);
label.getStyleClass().add(styleClass);
label.setMinWidth(160);
label.setMinHeight(60);
return label
}
Inheritance
HBox top = new HBox(
new CustomLabel("White", "white-check"),
new CustomLabel("Light Gray", "lightgray-check")
);
. . .
public class CustomLabel extends Label {
public CustomLabel(String text, String styleClass) {
super(text);
getStyleClass().add(styleClass);
setMinWidth(160);
setMinHeight(60);
}
}
Alternate Domain Specific Language
e.g. ScalaFX code:
var top = new HBox {
content = Seq(
new Label {
text = "White"
styleClass = Seq("white-check")
minWidth = 160
minHeight = 60
},
new Label {
text = "Light Gray"
styleClass = Seq("lightgray-check")
minWidth = 160
minHeight = 60
}
)
}
Upvotes: 3
Reputation: 9961
Is it conventionally and efficiently sound to organize children using braces as follows?
I think this is appropriate in your case (you will reuse variable names, separate components, etc.).
Will the braces be entirely optimized away when compiled?
Yes, this is just a language syntax for define scopes.
Upvotes: 1