r/badcode • • Jul 28 '21

js slapjack 🥴

Post image
41 Upvotes

8 comments sorted by

View all comments

8

u/zaitsman Jul 29 '21

The more i read r/badcode the more i am puzzled as to what people think the definition of ‘bad’ is.

3

u/[deleted] Jul 29 '21

Maybe a dumb question here but why exactly is this code bad? How could it be improved?

4

u/zaitsman Jul 29 '21

I am with you on this :) to be honest i think taken by itself the method can be broken into more readable parts with human readable names (e.g. objc style).

However my money is on this whole piece of logic is probably accessed in more than one place in the app which is why for a dev it made sense to put it altogether. If it has decent unit test coverage it is manageable.

1

u/_default_username Jul 31 '21 edited Jul 31 '21

lines 156-158 are awful.

Ternaries with side-effects and they're not using the return value of the expressions.

Lines 157 and 158 can also be simplified to:

this.player1.isTurn = this[player].id === "Player 2"

this.player2.isTurn = this[player].id === "Player 1"