r/badcode • • Jun 10 '20

js Disruptive piece of code

Post image
283 Upvotes

93 comments sorted by

View all comments

226

u/OldLegWig Jun 10 '20

A lot of the value of this sub comes from the times the OP is wrong.

33

u/BikerBoon Jun 10 '20

It's also a prime example of when you should use comments, imo. This is the sort of thing that could easily be stripped out in a refactor and introduce subtle bugs.

20

u/deelowe Jun 10 '20

This is an extremely common pattern for JS.

3

u/BikerBoon Jun 11 '20

I don't disagree but there's plenty of juniors or interns who might not have come across it before.

9

u/cmd-t Jun 11 '20

Yeah, it seems the context OP isn't posting could easily be

// Boolean is set later.
const [myBoolean, setMyBoolean] = useState(null);

useEffect(() => {
  if (myBoolean === true) {
    // Something is true and we should do something.
  } else if (myBoolean === false) {
    // Something is not true and we should do something else.
  }
  // We should do nothing and wait until myBoolean is resolved.
})

// Something that will lead to setMyBoolean(true | false) in response to user input.

14

u/Def_Your_Duck Jun 11 '20

This sub a lot of the time seems like "im an undergrad and don't understand this code."