What1slove / RefactoringLab

Вторая лабораторная по рефакторингу
0 stars 0 forks source link

Дублирование кода + одержимость элементарными типами (Crawler) #8

Open What1slove opened 4 years ago

What1slove commented 4 years ago

Метод getCoords Даже не знаю как объяснить происходящее без мата Неуместное использование массивов int когда данные имеют смысл Почти полное дублирование кусков кода Почему плохо: 1 Очень тяжело читается и понимается 2 Можем забыть какая ячейка массива каким данным соответствует 3 При изменении бизнес-логики придётся всё менять и переписывать Код: `int colnum = getColumn(); int pose = (int)pos % C_POSES / 2; // each pose here is doubled for more manageable movement int[][] coords=new int[9][3];

    Column column = lev.getColumns().get(colnum);
    int[] pt1 = column.getFrontPoint1();
    int[] pt2 = column.getFrontPoint2();
    switch (pose)
    {
        case 0:{
            coords[0][0] = pt1[0] +(pt2[0] - pt1[0])/3;
            coords[0][1] = pt1[1] +(pt2[1] - pt1[1])/3;
            coords[0][2] = CHEIGHT_H;
            coords[2][0] = pt1[0] +(pt2[0] - pt1[0])/4;
            coords[2][1] = pt1[1] +(pt2[1] - pt1[1])/4;
            coords[2][2] = -CHEIGHT;
            coords[4][0] = pt2[0] -(pt2[0] - pt1[0])/4;
            coords[4][1] = pt2[1] -(pt2[1] - pt1[1])/4;
            coords[4][2] = CHEIGHT_HP;
            coords[6][0] = pt1[0] +(pt2[0] - pt1[0])/4;
            coords[6][1] = pt1[1] +(pt2[1] - pt1[1])/4;
            coords[6][2] = -CHEIGHT_H;
            break;
        }
        case 1: {
            coords[0][0] = pt1[0] +(pt2[0] - pt1[0])/3;
            coords[0][1] = pt1[1] +(pt2[1] - pt1[1])/3;
            coords[0][2] = CHEIGHT_H;
            coords[2][0] = pt1[0] +(pt2[0] - pt1[0])/2;
            coords[2][1] = pt1[1] +(pt2[1] - pt1[1])/2;
            coords[2][2] = -CHEIGHT;
            coords[4][0] = pt2[0] -(pt2[0] - pt1[0])/3;
            coords[4][1] = pt2[1] -(pt2[1] - pt1[1])/3;
            coords[4][2] = CHEIGHT_H;
            coords[6][0] = pt1[0] +(pt2[0] - pt1[0])/2;
            coords[6][1] = pt1[1] +(pt2[1] - pt1[1])/2;
            coords[6][2] = -CHEIGHT_H;
            break;
        }
        case 2: {
            coords[0][0] = pt1[0] +(pt2[0] - pt1[0])/4;
            coords[0][1] = pt1[1] +(pt2[1] - pt1[1])/4;
            coords[0][2] = CHEIGHT_HP;
            coords[2][0] = pt1[0] +(pt2[0] - pt1[0])*3/4;
            coords[2][1] = pt1[1] +(pt2[1] - pt1[1])*3/4;
            coords[2][2] = -CHEIGHT;
            coords[4][0] = pt2[0] -(pt2[0] - pt1[0])/3;
            coords[4][1] = pt2[1] -(pt2[1] - pt1[1])/3;
            coords[4][2] = CHEIGHT_H;
            coords[6][0] = pt1[0] +(pt2[0] - pt1[0])*3/4;
            coords[6][1] = pt1[1] +(pt2[1] - pt1[1])*2/3;
            coords[6][2] = -CHEIGHT_H;
            break;
        }
    }
    coords[1][0] = pt1[0];
    coords[1][1] = pt1[1];
    coords[1][2]=0;
    coords[3][0] = pt2[0];
    coords[3][1] = pt2[1];
    coords[3][2] = 0;
    coords[5][0] = pt2[0] -(pt2[0] - pt1[0])/6;
    coords[5][1] = pt2[1] -(pt2[1] - pt1[1])/6;
    coords[5][2] = 0;
    coords[7][0] = pt1[0] +(pt2[0] - pt1[0])/6;
    coords[7][1] = pt1[1] +(pt2[1] - pt1[1])/6;
    coords[7][2] = 0;
    coords[8] = coords[0];
    return Arrays.asList(coords);`