Skip to content

Having too many getters and setters make the private access modifier useless. #3

@darrensapalo

Description

@darrensapalo

This issue may produce potential bugs.

If you have the following code:
(As seen in GameState, Entity, Enemy, etc.)

public void setCurrentLevel(int currentLevel) {
        this.currentLevel = currentLevel;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public List<Enemy> getEnemies() {
        return enemies;
    }

    public void setEnemies(List<Enemy> enemies) {
        this.enemies = enemies;
    }

    public List<Tower> getAvailableTowers() {
        return availableTowers;
    }

    public List<Tower> getDeployedTowers() {
        return deployedTowers;
    }

    public List<Point> getCurrentWaypoints() {
        return currentWaypoints;
    }

    public void setCurrentWaypoints(List<Point> currentWaypoints) {
        this.currentWaypoints = currentWaypoints;
    }

    public int[][] getGrid() {
        return grid;
    }

    public int getMoney() {
        return money;
    }

    public int getLife() {
        return life;
    }

    public void setLife(int life) {
        this.life = life;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public float getWaveSpawnTime() {
        return waveSpawnTime;
    }

You could have used public attribute instead. It would have been easier.

The reason why you make your object attributes private is because you want to provide your own interfaces for modifying / manipulating the said attributes.

    public void setLife(int life) {
        this.life = life;
    }

Could have been

    public void receiveDamage(Enemy enemy) {
        this.life -= enemy.getDamage();
    }

Which is better than allowing the developer to set the value of Life immediately. It provides less areas for bugs.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions