There's no shame in doing one or two if/else blocks. Ternaries can be great for simple assignments, but something like this just looks like nonsense at a glance.
I think I might just build up the object with individual if statements.
let returnObject = {
// any defaults go here
};
if (columnActionType === null)
returnObject.offset_from = tradeId;
// shortcut now since remaining conditionals require fullscreen
if (!fullscreen)
return returnObject;
if (filters?.protocol?.length > 0)
returnObject.protocol = filters.protocol;
if (filters?.service?.length > 0)
returnObject.service = filters.service;
return returnObject;
Unfortunately this is just a common scenario, conditional assignment of values. Your best bet is to normalize how objects like this are initialized and isolate the construction/assignment code as best possible.
You all look like CS granny's complaing about ternaries this code would be convoluted no matter the syntax, everytime you use excessive branching you turn your code into a puzzle
64
u/potato_drinks May 03 '22
somebody shoot me
41
u/Darknety May 03 '22 edited May 04 '22
I mean.. chaining ternaries like this isn't terrible in itself, but making the same checks a bunch of times to save one if-context is.
Edit: I can't read code at 5am apparently.
4
u/ronodipbasak May 03 '22
Why fullscreen so important?
62
u/[deleted] May 03 '22
Why not check for full screen and filters ahead of time, and make 2 separate meaningful branches, rather than repeating that check?
60
u/Tao1_ May 03 '22
Someone just learned about ternary
32
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 03 '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/alystair May 03 '22
It could definitely be cleaned up by destructuring and extracting the filter results. Pretty readable.
1
u/Rataratz May 28 '22
the fuck were u thinking
6
u/m2thek May 03 '22
There's no shame in doing one or two if/else blocks. Ternaries can be great for simple assignments, but something like this just looks like nonsense at a glance.
2
u/Interloper949 May 03 '22
This is triggering. The code base I’m currently trying to clean up is like this
1
u/Impossible_Average_1 May 03 '22
filters?
1
u/Wu_Fan May 03 '22
I don’t know js well
Is that an “AND” as the first half of a ternary? And a bit of breaching DRY?
1
u/the_other_Scaevitas May 04 '22
Looks like someone just learnt about ternary statements
1
u/kingdango May 06 '22 edited May 06 '22
I think I might just build up the object with individual if statements.
Unfortunately this is just a common scenario, conditional assignment of values. Your best bet is to normalize how objects like this are initialized and isolate the construction/assignment code as best possible.
1
u/CaitaXD May 03 '22
You all look like CS granny's complaing about ternaries this code would be convoluted no matter the syntax, everytime you use excessive branching you turn your code into a puzzle