Question How to conduct a qualitative code analysis to eliminate invisible errors

HarryStoune

Junior Member
Nov 22, 2022
1
0
6
I'm writing code for an application, but when I run the code, no elements are displayed. I want to analyze the code to eliminate errors, because manual search takes a lot of time. How do you do code review?

Code:
package com.TSP;

import java.io.IOException;
import java.util.ArrayList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.geometry.Point2D;
import javafx.stage.Stage;

public class Controller {

    @FXML
    public static AnchorPane canvasPane;

    private static ArrayList<Point2D> pointsArray;

    private int newCitiesCount; //в дальнейшем буду передавать его как bound для ProblemSolver

    public static Point2D canvasPanePointCoords;

    public static Point2D prevPointClicked;

    public static int getCity(Point2D point) {
            int i;
            for (i = 0; i < pointsArray.size(); ++i) {
                if (pointsArray.get(i).distance(point) < 24)
                    break;
            }
            return i + 1;
    }

    private int repPointClicksCounter;

    private static Stage stage;

    private static void showDistanceInputWindow() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("dialog.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 400, 250);
        stage.setTitle("Distance input");
        stage.setScene(scene);
        stage.show();
    }

    public static void closeDistanceInputWindow() {
        stage.close();
    }

    @FXML
    void onMouseClicked(MouseEvent event) {
        double pointX = event.getSceneX();
        double pointY = event.getSceneY();
        canvasPanePointCoords = canvasPane.sceneToLocal(pointX, pointY);
        int counter = 0; int counter2 = 0;
        int flag = 0;
        if (pointsArray.size() == 0)
            flag = 1;
        else {
            for (int i = 0; i < pointsArray.size(); ++i) {
                if (pointsArray.get(i).distance(canvasPanePointCoords) > 24)
                    ++counter;
            }
            if (counter == pointsArray.size())
                flag = 1;
        }
        if (flag == 1 && newCitiesCount < CitiesCollection.size) { //если в месте клика нет точки
            pointsArray.add(canvasPanePointCoords);
            Circle point = new Circle(canvasPanePointCoords.getX(), canvasPanePointCoords.getY(), 8, Color.CADETBLUE);
            Label label = new Label(Integer.toString(newCitiesCount));
            label.setLayoutX(canvasPanePointCoords.getX()+5);
            label.setLayoutY(canvasPanePointCoords.getY()+5);

            canvasPane.getChildren().add(point);
            canvasPane.getChildren().add(label);
            System.out.println("Added new city: (" + canvasPanePointCoords.getX() + " ; " + canvasPanePointCoords.getY() + ")");
            newCitiesCount++;
        } else if (flag == 0) { //если в месте клика уже есть точка
            if (pointsArray.size() > 1) { //если пред. клик был не в эту же точку и имеется больше 1 точки
                repPointClicksCounter++;
                if (repPointClicksCounter == 2) {
                    for (int i = 0; i < pointsArray.size(); ++i) {
                        if (pointsArray.get(i).distance(canvasPanePointCoords) < 24 || pointsArray.get(i).distance(prevPointClicked) < 24)
                            ++counter2;
                    }
                    repPointClicksCounter = 0;
                }
                if (counter2 == 2 && prevPointClicked.distance(canvasPanePointCoords) > 24) { //если пред. клик и текущий клик - города
                    try {
                        showDistanceInputWindow();
                    }
                    catch (IOException e) {
                        System.out.println("Wrong input!");
                    }
                }
            }
        }
        prevPointClicked = canvasPanePointCoords;
    }

    @FXML
    void onStartButtonClicked(MouseEvent event) {
        CitiesCollection.output();
        ProblemSolver.findShortestRoute(CitiesCollection.matrix, newCitiesCount).printRoute();
    }

    @FXML
    void initialize() {
        CitiesCollection.createCitiesCollection();
        pointsArray = new ArrayList<>();
        canvasPane = new AnchorPane();
        prevPointClicked = new Point2D(0,0);
        canvasPanePointCoords = new Point2D(0,0);
        newCitiesCount = 1;
        repPointClicksCounter = 0;
        stage = new Stage();
    }
}
 

Red Squirrel

No Lifer
May 24, 2003
63,937
10,360
126
twitter.com
I personally like to add "echo" or message boxes, or other similar display in the code. I start in general areas that I think the problem is happening then narrow it down by adding more debug lines. The "right" way is to use an actual debugger but I honestly find this way is just easier, and I'm not introducing variables by running the program through a debugger.

I don't do much javascript so can't help with that specific code but I think you can also output to the web console which you would open in the browser dev tools. Message boxes work too as they halt the code, so it can be used to step through different parts.

Also it helps to output the value of variables, as sometimes you think they are a certain value but they are not, and then that will cause something down the line to not work. From there you can figure out why they are not getting set to what they should, and then just narrow the problem down from there.
 
  • Like
Reactions: igor_kavinski
Thread starter Similar threads Forum Replies Date
Danish Zubair Programming 8
Bulldog13 Programming 2

ASK THE COMMUNITY