r/badcode • • Apr 13 '22

js Found this monstrosity while refactoring some of my old code

Post image
206 Upvotes

85 comments sorted by

View all comments

204

u/Xuntari Apr 13 '22

It is a smart solution to the problem and with better formatting it would probably be better code, but with everything on one line like this it's really hard to see what is nested into what.

I used to write code like this, it looks better because it's shorter, but I learned after a while that readability is so much more important than writing short code.

I work with JS every day, and it took me some time and a lot more brainpower to read the second one.

34

u/ThatChapThere Apr 13 '22

Yeah, you're probably right.

2

u/AdminYak846 Apr 15 '22

I would move the mapping of the game dates (in the set call) to it's own line which would improve its readability.

13

u/Lonewolf953 Apr 13 '22 edited Apr 13 '22

funnily enough it was the opposite for me, in the old code I'm still trying to figure out why certain if statements are there to begin with

meanwhile the new code only took me a few seconds to understand, I guess it comes down to how familiar one is with modern JS code

EDIT: all I did was say I understood the new code faster than the old code and I got downvoted, don't give any opinions on reddit, what a surprise..

15

u/Xuntari Apr 13 '22

A lot of it comes down to personal preference I suppose, but the consensus seems to be that nesting like this on one line is hard to read.

I agree that the solution is better, but i would split it up more, into multiple variables with good names, to avoid the nesting and make it more apparent what each step does.

0

u/[deleted] Apr 13 '22

[deleted] — view deleted comment

1

u/AdminYak846 Apr 15 '22

I work with JS and map and filter are fine, its reduce that trips me up constantly.

2

u/rennitbaby Apr 13 '22

I think for loops also perform faster too. But yes readability is key, OP should format the one liner into multi lines then it’ll be better!