This is ugly as sin but we can't see the perfect storm that led to this possibly being a good solution to the problem of whatever fucked up data structure they're dealing with.
Don't crucify me, but this isn't that bad for string addressing. Yes the cases would make way more sense replaced with a loop, but case 1 and case 2 (at least if not using a JavaScript Switch that's secretly an if else chain) could be a good optimization if this is a hot path. Obviously larger cases should be replaced with a loop.
But I know what you're thinking, why not just indexing operations directly on the object? Because the object or the key are dynamically generated. It's for variably structured information that exists at runtime - which could be evident of a greater design issue, but without more context we can't really know for sure.
23
u/[deleted] Dec 17 '21 edited Dec 17 '21
const keys = key.split('.')
if(keys.length == 0) return null;
let result = data[keys[0]]
for(let i = 1; i < k.length; i++) {
result = result[keys[i]] || null
}
return result
Something like that ?
Edit: I'm not lmiting to 4, so not good
3
u/ragingroku Dec 17 '21
Cuz I’m one step closer to the edge/ and I’m about to….
Unrelated, OP how do you get make a screenshot formatted like that?
2
u/geon Dec 18 '21
We have tons of trash code like that at work. Always fun when you have to go in and modify something. 90 % of my job is just refactoring.
33
u/Icy-Low-9264 Dec 17 '21
I want to know about case 5
13
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 17 '21
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!
7
u/tcpukl Dec 17 '21
If it's too deep surely returning the deepest parameter makes more sense than null.
4
u/pau1rw Dec 17 '21
"too deep" had me.
2
u/GeneralBookkeeper252 Dec 17 '21
Jail
2
u/ososalsosal Dec 18 '21
This is ugly as sin but we can't see the perfect storm that led to this possibly being a good solution to the problem of whatever fucked up data structure they're dealing with.
3
u/[deleted] Dec 17 '21
Don't crucify me, but this isn't that bad for string addressing. Yes the cases would make way more sense replaced with a loop, but case 1 and case 2 (at least if not using a JavaScript Switch that's secretly an if else chain) could be a good optimization if this is a hot path. Obviously larger cases should be replaced with a loop.
But I know what you're thinking, why not just indexing operations directly on the object? Because the object or the key are dynamically generated. It's for variably structured information that exists at runtime - which could be evident of a greater design issue, but without more context we can't really know for sure.
1
u/Legasov_ Dec 18 '21
How do you put your code in that 'macintosh' window..? it looks cool like a mockup
1
u/Furry_69 Dec 17 '21
wtf