lundi 9 décembre 2019

Simplify nested If and else

Hello i'm new to java programming and i was wondering if there is another possible way to simplify or rather not use if else statements. I am asking this because i've been using a lot of if else condition on my programming and i think it is kinda annoying. i hope you can help me. here is my sample code.

        if (this.hole.greens[greenId].hasOwnProperty("verticalLine")){
            var xMax = this.hole.greens[greenId].verticalLine.xMax,
                xMin = this.hole.greens[greenId].verticalLine.xMin,
                yMax = this.hole.greens[greenId].verticalLine.yMax,
                yMin = this.hole.greens[greenId].verticalLine.yMin

            if (this.hole.greens[greenId].verticalLine.hasOwnProperty("coordinateMode")){
                var holeHeight = this.hole.greens[greenId].verticalLine.height,
                    holeWidth = this.hole.greens[greenId].verticalLine.width,
                    center = (yMax + yMin)/2;

            // Y Coordinate
                var yCoordinate = ((yMax-this.pinPosition.y)/((yMax - yMin)/holeHeight)),
                    yCoordinateCenter = ((center-this.pinPosition.y)/((center - yMin)/holeHeight)/2);
                if (this.pinPosition.baseLine !== 'center') {
                    if (yCoordinate < holeHeight && yCoordinate > 0){
                        var distanceY =   Math.round(yCoordinate);
                    } 
                }else {
                    if (((yCoordinateCenter)*-1) < (holeHeight/2) && (yCoordinateCenter) < (holeHeight/2)) {
                        var distanceY =   Math.round(yCoordinateCenter);
                    }
                }

            // X Coordinate
                var xCoordinateR = ((100-xMin)-(100-this.pinPosition.x)) /((xMax - xMin)/holeWidth),
                    xCoordinateL = (xMax-this.pinPosition.x)/((xMax - xMin)/holeWidth);

                if (this.pinPosition.xOrigin == 'left'){
                        if (xCoordinateR  < holeWidth && xCoordinateR > 0) {
                            var distanceX =  Math.round(xCoordinateR);
                         }
                    } else if (this.pinPosition.xOrigin == 'right'){
                        if (xCoordinateL < holeWidth && xCoordinateL > 0) {
                            var distanceX =  Math.round(xCoordinateL);
                        }
                    }


            if(point.x > xMax && point.y < yMax && point.y > yMin){
                this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMax, y: point.y, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x < xMin && point.y < yMin){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMin, y: yMin, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x < xMin && point.y < yMax){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMin, y: point.y, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x < xMin && point.y > yMax){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMin, y: yMax, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x > xMax && point.y > yMax){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMax, y: yMax, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x > xMax && point.y < yMax){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMax, y: yMin, distanceY: distanceY, distanceX: distanceX });
            }else if (point.y < yMin && point.x > xMin ){
                this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: point.x, y: yMin, distanceY: distanceY, distanceX: distanceX });
            }else if (point.y > yMax && point.x > xMin){

                this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: point.x, y: yMax, distanceY: distanceY, distanceX: distanceX });
            }else{
                this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: point.x, y: point.y, distanceY: distanceY, distanceX: distanceX });
            }

        } else {
            if(point.x > xMax && point.y < yMax && point.y > yMin){
                this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMax, y: point.y, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x < xMin && point.y < yMin){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMin, y: yMin, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x < xMin && point.y < yMax){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMin, y: point.y, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x < xMin && point.y > yMax){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMin, y: yMax, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x > xMax && point.y > yMax){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMax, y: yMax, distanceY: distanceY, distanceX: distanceX });
            }else if (point.x > xMax && point.y < yMax){
                    this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: xMax, y: yMin, distanceY: distanceY, distanceX: distanceX });
            }else if (point.y < yMin && point.x > xMin ){
                this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: point.x, y: yMin, distanceY: distanceY, distanceX: distanceX });
            }else if (point.y > yMax && point.x > xMin){
                this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: point.x, y: yMax, distanceY: distanceY, distanceX: distanceX });
            }else{
                this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: point.x, y: point.y, distanceY: distanceY, distanceX: distanceX });
            }

        }
            }else{
                this.updatePinPosition({ courseId: this.hole.courseId, holeId: this.hole.id, x: point.x, y: point.y, distanceY: distanceY, distanceX: distanceX });
            }

Aucun commentaire:

Enregistrer un commentaire