r/badcode • u/JarrvisForever • Jun 13 '22
js Look at this "beautiful" condition! --- If someone knows how to write such a condition in a variable, I will be grateful for the advice! (The difficulty is that some components of this condition are tied to the cycle)
351
Upvotes
58
u/Dalimyr codes in C#...poorly Jun 13 '22
Why is each check done twice but with both 512 and -512? Surely if viewField.minX is greater than 512+foo then it's also greater than -512+foo? That'd be my first refactor.
15
u/chaosTechnician Jun 13 '22 edited Jun 13 '22
Ouch. I wonder if you wrote out in a comment what the intent is if it'll help you work out easier to read logic to accomplish your goal here.
I refactored a few times to store repeated calculations in variables (guessing at their purpose for variable names), remove redundant checks, remove magic numbers, remove the `not` around the complex check, and tried to make the comparisons easier to read. I came up with this:
It's possible that I accidentally butchered something along the way, though. So do be careful.
EDIT to clean up formatting. Reddit doesn't seem to want to keep the changes I made.
9
u/simpleauthority Jun 13 '22
"Some components of the condition are tied to the cycle" - meaning what exactly?
The function you are calling in the condition is mutating state? So a later call to the same function will return something different, in the same conditional?
Hmm.
3
u/JarrvisForever Jun 15 '22
UPD: Thanks to the help of the people in the comments, this code looks a bit better now.
}
67
u/IDontByte Jun 13 '22
I'd start by extracting these two expressions to variables
28
u/Es452002 Jun 13 '22
Image Transcription: Code
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!
5
u/itemluminouswadison Jun 13 '22
why not extract the getSegmentCords functional call result into a meaningful var?
also why the magic strings "x" and "y"? magic numbers 1024 and 512? please extract meaningful constants
i'd honestly extract each of the 8 conditions into methods or functions and start there to see what materializes. you can probably then wrap the entire block of conditions into its own method with a meaningful name
readability is super important when it comes to complicated statements like this
4
u/nekokattt Jun 13 '22
you call the nested function like 8 times with the same arguments, by the looks (it is hard to see on my phone).
Just put that in a variable and that will halve the size.
4
u/_SAMUEL_GAMING_ Jun 13 '22
you should rename partymin and partxmin to partYmin and partXmin
i read party instead of part y
3
u/Brekkjern Jun 13 '22
Extract that repeated dictionary lookup into this and use that instead of looking up the value for every different segment lookup. Could even do the x and y of
getSegmentCords.And then do the if check.
3
u/parthux1 Jun 13 '22
Wrap and forget
3
u/SueedBeyg Good code makes sense. Bad code just works. Yours does neither. Jun 13 '22
Aw hell nah... throw the whole app out and start over
5
u/carfniex Jun 13 '22
write it as 4 ifs in a separate function returning boolean
2
u/Regular-Tough-593 Jun 14 '22
F**king lit
2
u/kristallnachte Jun 14 '22
You can make your whole condition a function to help make it clearer.
-4
u/AdHealthy3717 Jun 13 '22
Downvoted bkz maintainability is more important than looking cool.
SoftwareEngineering #fail #HardNo
-8
u/Errons1 Jun 13 '22
Picture resolution is to low, cant read it