P
P
postya2019-02-08 15:30:52
Java
postya, 2019-02-08 15:30:52

How to refactor code in which only numbers change?

Wrote an application in JavaFX, but my conditions look very large. There is a repetition of the code. The same properties are used, but with different numbers. For example, the setScaleX() method is often repeated, only the value changes in parentheses.
How can you shorten the code and make it all clear?

//get Screen Resolution
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    screenSize.getHeight();
    screenSize.getWidth();

    int screenHeight = screenSize.height;
    int screenWidth = screenSize.width;

    //assign video size to screen size
    media.setFitWidth(screenWidth);
    media.setFitHeight(screenHeight);

    if (screenWidth <= 1700) {
      media.setTranslateY(50);
      media.setScaleX(1.1);
      media.setScaleY(1.1);

    }  if (screenWidth <= 1600 && screenHeight <= 900) {
      media.setScaleX(1.0);
      media.setScaleY(1.0);
      media.setTranslateY(-3);

    } if (screenWidth == 1600 && screenHeight == 1024) {
      media.setScaleX(1.1);
      media.setScaleY(1.1);
      media.setTranslateY(60);

    } if (screenWidth <= 1370) {
      media.setTranslateY(0);
      btnPalette.setPrefWidth(40);
      btnFont.setPrefWidth(40);
      btnQuestCards.setPrefWidth(40);
      btnNonQuestCards.setPrefWidth(40);

    }  if (screenWidth <= 1300) {
      media.setScaleX(1.4);
      media.setScaleY(1.4);
      media.setTranslateY(150);
    }  if (screenWidth == 1280 && screenHeight == 800 || screenWidth == 1280 && screenHeight == 768) {
      media.setScaleX(1.1);
      media.setScaleY(1.1);
      media.setTranslateY(50);
    } if (screenWidth == 1280 && screenHeight == 720 || screenWidth == 1176 && screenHeight == 664) {
      media.setScaleX(1.0);
      media.setScaleY(1.0);
      media.setTranslateY(0);
    }  if (screenWidth == 1152 && screenHeight == 864) {
      media.setScaleX(1.4);
      media.setScaleY(1.4);
    }  if (screenWidth == 1024 && screenHeight == 768) {
      media.setTranslateY(90.0);
    } if (screenWidth == 800 && screenHeight == 600) {
      media.setScaleX(1.3);
      media.setScaleY(1.3);
      media.setTranslateY(80.0);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Ketov, 2019-02-08
@postya

Not sure, but still.
Instead of:

if (screenWidth <= 1700) {
  media.setTranslateY(50);
  media.setScaleX(1.1);
  media.setScaleY(1.1);
}
if (screenWidth <= 1600 && screenHeight <= 900) {
    media.setScaleX(1.0);
    media.setScaleY(1.0);
    media.setTranslateY(-3);
}

I would do like this:
reDraw(screenWidth <= 1700, media, 1.1, 1.1, 50.0);
reDraw(screenWidth <= 1600 && screenHeight <= 900, media, 1.0, 1.0, -3.0);
        // и метод под всё это(его можно поперегружать и сделать ещё более удобным)
    private static void reDraw(Boolean condition, ImageView view, Double scaleX, Double scaleY, Double translateY) {
        if (condition) {
            if (scaleX != null)
                view.setScaleX(scaleX);
            if (scaleY != null)
                view.setScaleY(scaleY);
            if (translateY != null)
                view.setTranslateY(translateY);
        }
    }

If these conditions will be used for something else, you can make it even prettier:
boolean isLargeSize = screenWidth <= 1700;
boolean isMediumSize = screenWidth <= 1600 && screenHeight <= 900;

reDraw(isLargeSize, media, 1.1, 1.1, 50.0);
reDraw(isMediumSize, media, 1.0, 1.0, -3.0);

A
Alex Wells, 2019-02-08
@Alex_Wells

You can make EnumScreenSize, in which to write some logic that gives the desired instance based on Dimension. The first 5 lines will already be reduced to one. >= <= && ||This will also allow you to do instead of tens size == EnumScreenSize.FULL_HD. If the scale values ​​apply to all screens in general, and they can be taken out in enms - this is also worth doing. Instead of if's, you can use switch, and although it is cumbersome, it will look simpler. If setScale() and other methods return this, then you can chain them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question