开发者

There has got to be a better way to write this

开发者 https://www.devze.com 2023-03-09 03:09 出处:网络
public static boolean isValidCoordinate(Coordinate point) { Point map = point.mapCoordinates, tileSet = point.tileSetCoordinates, tile = point.tileCoordinates, pixel = point.pixelCoordinates;
public static boolean isValidCoordinate(Coordinate point) {
    Point map = point.mapCoordinates, tileSet = point.tileSetCoordinates, tile = point.tileCoordinates, pixel = point.pixelCoordinates;
    if (map != null && map.x >= 0 && map.y >= 0) {
        if (tileSet != null) {
            if (tileS开发者_运维百科et.x >= 0 && tileSet.y >= 0) {
                if (tile != null) {
                    if (tile.x >= 0 && tile.y >= 0) {
                        if (pixel != null) {
                            if (pixel.x >= 0 && pixel.y >= 0) {
                                return true;
                            }
                            else {
                                return false;
                            }
                        }
                        else {
                            return true;
                        }
                    }
                    else {
                        return false;
                    }
                }
                else {
                    return true;
                }
            }
            else {
                return false;
            }
        }
        else {
            return true;
        }
    }
    else {
        return false;
    }
}

The coordinate class holds 4 Point objects, representing a unique set of coordinates. Each set of coordinates needs to be positive. mapCoordinates cannot be null, but the other Points can, as long as everything after them is null, and everything before them is positive in this list: mapCoordinates, tileSetCoordinates, tileCoordiantes, pixelCoordinates. Thanks


Of course there is. Use methods to keep DRY:

boolean isValid(Point p) {
    return p == null || (p.x >= 0 && p.y >= 0);
}

boolean isValid(Coordinate c) {
    return isValid(c.mapCoordinates)
        && isValid(c.tileSetCoordinates) 
        && isValid(c.tileCoordinates) 
        && isValid(c.pixelCoordinates);
}

And yes, calling the variables of type Point coordinate and those of type Coordinate point is likely to confuse the reader.

0

精彩评论

暂无评论...
验证码 换一张
取 消