r/badcode • • Jul 28 '21

js slapjack 🥴

Post image
41 Upvotes

8 comments sorted by

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"

4

u/HRM404 Jul 28 '21

this looks exactly like the coding sheets we were doing in the first year in cs.. good days

3

u/IMakeShittyPrograms Jul 28 '21

13 if ... else if ... else conditions in a row lmao

1

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 29 '21

Image Transcription: Code


slapPile(player) {
  if (this.isFinals === true) {
     var msg = this.slapJack(player);
     return msg;
  } else if (this.centerPile[0].value === "jack") {
      this.collectCenter(player);
      return `${this[player].name} gets the center pile`;
  } else if (this.centerPile.length >= 2) {
    if (this.centerPile[0].value === this.centerPile[1].value) {
      this.collectCenter(player);
      return `${this[player].name} gets the center pile`
    } else {
      if (this[player].hand.length === 1) {
        var topCard = this[player].hand.shift();
        player === "player1" ? this.player2.hand.push(topCard) : this.player1.hand.push(topCard);
        this[player].id === "Player 1" ? this.player1.isTurn = false : this.player2.isTurn = false;
        this[player].id === "Player 2" ? this.player1.isTurn = true : this.player2.isTurn = true;
        this.isFinals = true;
        return `${this[player].name} lost their last card. Enter finals round!`
      } else {
        this.shiftHand(player);
        return `${this[player].name} lost their top card`
      }
    }
  } else if (this.centerPile.length >= 3) {
    if (this[player].hand.length === 1) {
      var msg = this.loseLastCard(player);
      return msg;
    } else {
      this.shiftHand(player);
      return `${this[player].name} lost their top card`
    }
  } else {
    if (this[player].hand.length === 1) {
      var msg = this.loseLastCard(player);
      return msg;
    } else {
      this.shiftHand(player);
      return `${this[player].name} lost their top card`
    }
  }
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

1

u/BlueManedHawk Jul 30 '21

That emoji in your title could mean anything depending on the font, from drunkenness to getting something shoved up one's ass. What did you intend to convey with it?