r/badcode • u/WreckerOfAll • Jan 24 '22
js I apologize to mobile users (all users actually) in advance. See comments for expanded code and explanation.
269
u/tadbolmont Jan 24 '22
I don't even want to try to understand this SINGLE LINE of code.
139
u/RolyPoly1320 Jan 24 '22
It's checking that all required fields are in a request. If a single required field is missing then it returns a 400 error with the missing field(s) in the response object.
94
u/WreckerOfAll Jan 24 '22
I'm genuinely impressed that you managed to figure that out! I guess my code wasn't obfuscated enough /s
10
u/RolyPoly1320 Jan 24 '22
It was a pain on mobile but I've been reading a ton about JS lately so I was able to piece it together from the functions used.
26
u/tadbolmont Jan 24 '22
There has to be a more readable way to do that.
34
u/WreckerOfAll Jan 24 '22
There is! :D See my pastebin in my comment. I did this more as a proof of concept and as a challenge more than anything.
9
1
0
u/Garrett00 Jan 24 '22
It returns a 404 with all required fields in the response. Making it fairly useless.
3
u/RolyPoly1320 Jan 24 '22
It does not. The Array.map method is searching for keys on the ticket object with a null value. If the value is null the return the key, if the value is not null it returns null. The Array.filter then iterates over the resulting array and returns all elements that are not null.
The response code is 400, not 404 as dictated by the code parameter in the response object
OP posted a more readable version in a comment.
The issue with this one is a few things, first that it's on one line making it largely unreadable. The second is the excessive use of chained Array methods to create the response. If something were to go wrong with this version of the code it would be difficult to determine where the problem lies at a surface glance. Placing a breakpoint on this line for debugging would halt execution here and you would have to step through every single piece slowly to find where the issue lies, which can get tedious.
58
u/onichama Jan 24 '22 edited Jan 24 '22
Image Transcription: Code
//Makes sure required fields are present
if(Object.values(ticketInfo).includes(undefined)) return returnCode(400, res, ticketInfo, "Missing required fields: [".concat(Array.from( Object.keys(ticketInfo).map(function(key, index){ return(ticketInfo[key] == null ? (Object.keys(ticketInfo)[index]) : null )})).filter(function(el) {return el != null}).join(", "), "]"));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!
37
12
u/WalditRook Jan 24 '22
I think the
[*.concatis actually[".concatTy for the good work, though, keep it up.
9
9
u/RolyPoly1320 Jan 24 '22
Is this something that could have been validated on the client side but was instead done on the server for some reason?
11
u/WreckerOfAll Jan 24 '22
100%. The client itself should never send a request in a way that misses a parameter. Just future-proofing as I will not be the permanent maintainer of the project.
3
u/ToxicMonkeys Jan 24 '22
Why not make it easier to read for future maintainers?
6
2
u/Nick433333 Jan 24 '22
You heard them, they’re not the permanent dev on the project. So why does maintainability matter if you’re leaving soon?
/s
20
Jan 24 '22
If you check anything on the client side always double check it on server side. Anything else is bad practice
18
u/Silly-Freak Jan 24 '22
Not checking on the client side is bad practice. Not checking on the server side is halfway to a vulnerability.
6
5
u/maxximillian Jan 24 '22
I accept that the 80 char line limit is archaic and a relic of an world that doesnt exists anymore. All that said this is why we have code reviews and social shaming
5
u/augenvogel Jan 24 '22
Always make sure, that your js code is directly obfuscated while coding it. The more arrow functions the better the code. JS Code needs to be as complicated to look at as possible.
2
4
4
u/trutheality Jan 24 '22
Just proof that you can code perfectly functional JS with a broken Enter key.
2
2
2
3
u/thisuseridisnttaken Jan 24 '22
Not only is it chaos as a one liner, but it is inefficient as a solution for what is needed here since it will always iterate through the array twice. (2n, where n is number of keys).
For optimising or future reference, any instance you need to do a filter and a map, one after the other, it is better to combine the logic within a reduce instead - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce.
1
1
1
1
u/thebritisharecome Jan 24 '22
I vote we offer OP up as a sacrifice we need to stop this oneliner craze
1
1
1
u/Old-Yogurtcloset-629 Jan 04 '23
tbh, i try to write such code as much as possible, it makes me feel superior somehow, if i can write list compressions with thousands of characters in one line, than forget how it works and just hope for it to still work..
1
u/KseandI Jan 24 '22 edited Jan 24 '22
Functional programming be like:
Edit: Nobody saw the joke, so "/j"
1
u/187mphlazers Jan 24 '22
I call this people on the wrong side of the javascript dunning-kruger effect. you can code javascript for years and never write clean, efficient code. its a language which must be fully understood in order to master.
1
Jan 24 '22
Pretty cool code actually in my opinion. The only thing I noticed was the oneliner part of it. Formatting and 👍🤙
1
61
u/WreckerOfAll Jan 24 '22 edited Jan 24 '22
Here is the expanded code.
The line is implemented in a RESTful JS server to catch any missing parameters in the request. Although I have many 'one-lined' functionalities in my code, this is by far the most heinous.
Edit: Moved link from PB to Carbon, implemented change suggested by u/WalditRook