patternjavaMinor
Whose line is it anyway?
Viewed 0 times
anywaywhoseline
Problem
I've just started delving into JavaFX, and the following is essentially my "Hello World". Although it's simple, I question the code formatting and wonder if I'm breaking any conventions, especially if explicitly concerning the library.
I also find myself concerned with what best promotes readability. Unfamiliarity brought many an alteration between styles. e.g. for statements, whether it is preferable to instantiate, modify and add an object all at once or simply pairing similar things together and modifying them wherever necessary and adding them at the end -- my final code here is an amalgamation of both styles.
This last bit may be delving a bit into SO/Programmers territory, but I'm curious. I found myself using anonymous inner classes as it made methods easily transportable; I've hitherto not used it more than once in a program, is this bad form? If so, why?
```
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class DrawingLines extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Legato's Lines");
Group root = new Group();
Scene scene = new Scene(root, 300, 150, Color.GRAY);
Line redLine = new Line(10, 10, 200, 10) {
{
setStroke(Color.RED);
setStrokeWidth(3);
getStrokeDashArray().addAll(10d, 5d, 15d, 5d, 20d);
setStrokeDashOffset(0);
}
};
Line whiteLine = new Line(10, 30, 200, 30) {
{
setStroke(Color.WHITE);
setStrokeLineCap(StrokeLineCap.ROUND);
setS
I also find myself concerned with what best promotes readability. Unfamiliarity brought many an alteration between styles. e.g. for statements, whether it is preferable to instantiate, modify and add an object all at once or simply pairing similar things together and modifying them wherever necessary and adding them at the end -- my final code here is an amalgamation of both styles.
This last bit may be delving a bit into SO/Programmers territory, but I'm curious. I found myself using anonymous inner classes as it made methods easily transportable; I've hitherto not used it more than once in a program, is this bad form? If so, why?
```
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class DrawingLines extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Legato's Lines");
Group root = new Group();
Scene scene = new Scene(root, 300, 150, Color.GRAY);
Line redLine = new Line(10, 10, 200, 10) {
{
setStroke(Color.RED);
setStrokeWidth(3);
getStrokeDashArray().addAll(10d, 5d, 15d, 5d, 20d);
setStrokeDashOffset(0);
}
};
Line whiteLine = new Line(10, 30, 200, 30) {
{
setStroke(Color.WHITE);
setStrokeLineCap(StrokeLineCap.ROUND);
setS
Solution
It's hard to pick on such simple and straightforward code.
The one thing that stands out to me is the distance between the line where you declare the
Let me quote an interesting paragraph from Code Complete:
The code between references to a variable is a “window of vulnerability.” In the window, new code might be added, inadvertently altering the variable, or someone reading the code might forget the value the variable is supposed to contain. It’s always a good idea to localize references to variables by keeping them close together.
Indicators of this window of vulnerability are the measurements of variable "span" and "live time":
-
Variable "span": the number of lines between references to a variable. When a variable is referenced multiple times, the average span is computed by averaging all the individual spans. The smaller the better.
-
Variable "live time": the total number of statements over which a variable is "live". This is a count of lines between the first reference and the last. Again, the smaller the better.
By declaring
you have a large "window of vulnerability",
and high average spans and life times.
You can reduce these negative indicators by pushing these declarations down in your method to where the variables are actually used.
The one thing that stands out to me is the distance between the line where you declare the
root variable, and the lines where you actually use it.Let me quote an interesting paragraph from Code Complete:
The code between references to a variable is a “window of vulnerability.” In the window, new code might be added, inadvertently altering the variable, or someone reading the code might forget the value the variable is supposed to contain. It’s always a good idea to localize references to variables by keeping them close together.
Indicators of this window of vulnerability are the measurements of variable "span" and "live time":
-
Variable "span": the number of lines between references to a variable. When a variable is referenced multiple times, the average span is computed by averaging all the individual spans. The smaller the better.
-
Variable "live time": the total number of statements over which a variable is "live". This is a count of lines between the first reference and the last. Again, the smaller the better.
By declaring
root and scene at the top and using only at the button,you have a large "window of vulnerability",
and high average spans and life times.
You can reduce these negative indicators by pushing these declarations down in your method to where the variables are actually used.
Context
StackExchange Code Review Q#80515, answer score: 6
Revisions (0)
No revisions yet.