Skip to content

Latest commit

 

History

History
28 lines (24 loc) · 675 Bytes

File metadata and controls

28 lines (24 loc) · 675 Bytes

Globals are evil

Some hints by Krzysztof Grzybek:

  1. Global and static objects cause implicit dependencies/coupling, thus they break the idea of encapsulation.
  2. It's hard to reason about them - logical scope for understanding behaviour of these objects is expanded to the whole program.
  3. It's hard to mock/stub them.
  4. Global objects pollute the main scope.

Bad:

class Player {
    walk() {
        this.x = nextDestination.x;
        this.y = nextDestination.y;
    }
}

Good:

class Player {
    walk(destination) {
        this.x = destination.x;
        this.y = destination.y;
    }
}