5 Fuse similar code together
This chapter covers:
In the last chapter, I mentioned that we are not finished with updateTile
. It violates several rules, most notably Never use if with else [R4.1.1]. We also worked to preserve the ||
s in the code because they expressed structure. In this chapter, we explore how to expose more such structures in the code.
This is updateTile
at the moment.
Listing 5.1. Initial
function updateTile(x: number, y: number) { if ((map[y][x].isStone() || map[y][x].isFallingStone()) && map[y + 1][x].isAir()) { map[y + 1][x] = new FallingStone(); map[y][x] = new Air(); } else if ((map[y][x].isBox() || map[y][x].isFallingBox()) && map[y + 1][x].isAir()) { map[y + 1][x] = new FallingBox(); map[y][x] = new Air(); } else if (map[y][x].isFallingStone()) { map[y][x] = new Stone(); } else if (map[y][x].isFallingBox()) { map[y][x] = new Box(); } }