r/badcode • u/ImplosiveTech • Feb 21 '22
js there is a reason i'm rewriting this, hell i'm having trouble figuring out how it works
65
Upvotes
r/badcode • u/ImplosiveTech • Feb 21 '22
5
u/hellra1zer666 Feb 22 '22 edited Feb 22 '22
Refactoring stuff like that is hell. I had to pick apart legacy code that had functions that were in the high hundreds of lines of code. I started with extracting code that was calculating a variable value or was processing information and transforming it. You want to firstly encapsulate all the stuff that has no or very few side effects so that you can work out the overarching logic without all the "unimportant" clutter obscuring the code. Then you start and take the first, I don't know 50 lines of code, and try to atomize that block into functions. At this point all you want is smaller functions and once you've done this for the entire unit you can start to look for similar logic and try to unify it and figure out what the actual necessary logical steps are and write proper functions for them.
Even if you wrote that code in the first place, you will be hard-pressed to make sense out of it if it stays like that.
When I had to do something like that, I really learned to appreciate my senior devs for rejecting long functions in early PRs. After that refactoring, I have not written a function that is longer than 20 lines, and that is already too long imo.
7
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 21 '22
Image Transcription: Code
[Any further code is cut off in the image.]
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
3
u/PostmatesMalone Feb 21 '22
Hey, it’s not JUST bad code. It ALSO has an XSS attack feature maybe
2
u/[deleted] Feb 21 '22
I checked out the code, I'm not sure that the station cleaning does anything at all?
tempSchrArris already typeDate | undefined.if ((tempSchArr != undefined) && (tempSchArr != null)) {tempSchArr = new Date(tempSchArr)}Also, my recommendation, if this
cleanStationDataAPIfunction is needed at all, is to use.mapinstead of.forEach.1
u/[deleted] Feb 28 '22
Theme name?
1
u/[deleted] Feb 21 '22
Even your {} look depressed
1
u/Franks2000inchTV Feb 22 '22
But think how much time you saved typing "obj" instead of "object."
0
u/[deleted] Feb 21 '22
[deleted] — view deleted comment