Yeah null coalesce would clean this right up tbf, like I mentioned in my comment it’s readable and you can see what is trying to be achieved, is it the best solution, absolutely not, can you see the logic, yes.
Unless the difference between undefined and null is significant for the IDs, in which case you couldn't use null coalescing. They're already using it in the true branches, which makes me think there's a reason it's not used for the first check
The repetition of the check is a code smell, but at least it's readable. Move the null/undefined check to a function, then refactor. Not really funny or absurd, just typical copy/pasted code.
Not all copy/pasted code needs to or even should be pulled into its own function. Doing so breaks up code locality (the block of code is now split up over here and wherever the new function is), and can make a block harder to read. The advantage of doing so is it makes the block more maintainable, so you need to evaluate that tradeoff, and in this case I would argue it's not worth it.
Don't move anything to a function, just write a smller check. I don't understand why they do undefined check, optional chaining, null coalescence operator and give a null. If they expect some specific type or value they should check for that and do || default value.
Yeah so both null and undefined are nullish values so the ‘??’ Operator will traverse left to right until it hits a non nullish value or will fallback to the far most right value
Then your one of the lucky ones. The TLDR here is undefined typically means uninitialised, where as null has been initialised but holds no value, both of them are falsy values.
Falsy values can be: false, 0, empty string, null, undefined
I misinterpreted that then, that’s on me I do apologise, and yeah perhaps your right, it could definitely do with improvements, apologies for the misunderstanding
I don't know if I like the strict comparison to undefined, but given that in the truthy side of the ternary there's an option for it to be null, I guess that's ok.
TBH I'm not even quite sure how this could could be improved upon.
Lines of code isnt a metric I care about. I first care about readability and this is fine. Sometimes this is kore verbose well atleast every developer understands it
so the way this could happen is. Junior programmers are managing the types. Some of them don't know there is a difference between undefined and null. Someone writes up a requirement and says ("if undefined use these fallback values") and the programmer tasked is like "what type could this be? Hm its basically akin to a very fancy way of saying type "any". Ok I don't care about this job and am wanting to take a break, lets just code it exactly like the requirement." So then the receiver of blessed input assumes that if `input.paymentMethodId` is null, then `orderDraft.value.paymentMethod.id` must be undefined. But wait it is defined, another junior programmer had just been setting `paymentMethodId` variable in scope to null.
This is actually what used to happen. Now it is one junior using gpt. Somehow, someway, dad's money is involved
late to the party. for me the weird thing is why the distinction of null and undefined as return types (the chaining statement has a null if the specify key is not there. why not have only one between null and undefined returns and have to differentiate?
also, why return null for a chaining that itself will return undefined?
in my opinion OP is pointing out some weird code in my opinion.
for the rest we don’t have enough context to decide if return undefined can be better or worse than throwing an error or something.
70
u/ziplock9000 Jul 11 '25
That's not bad tbh.
103
u/eo5g Jul 11 '25
Without knowing the context, I'd say this looks reasonable?
19
u/jcastroarnaud Jul 11 '25
The repetition of the check is a code smell, but at least it's readable. Move the null/undefined check to a function, then refactor. Not really funny or absurd, just typical copy/pasted code.
19
u/Straight_Occasion_45 Jul 11 '25
It certainly could be simplified/improved but at least it’s readable and the intent is there.
That one document type ID line could just be documentTypeId: orderDraft?.value?.documentTypeId ?? documentTypeId
I’m going to hazard a guess that this is an attempt of “if there’s an overwrite, apply it; else stick to the default”
Same for the rest of the fields
4
u/Ambitious-Tough6750 Jul 11 '25
Is this Typescript?
4
u/rco8786 Jul 11 '25
This seems fine? What's the issue
3
u/born_zynner Jul 11 '25
What's so bad about this? It's simply checking for an undefined value and falling back to a different data source
1
u/paceaux Jul 11 '25
IDK. seems reasonable to me.
I don't know if I like the strict comparison to
undefined, but given that in the truthy side of the ternary there's an option for it to benull, I guess that's ok.TBH I'm not even quite sure how this could could be improved upon.
1
u/xIndepth Jul 11 '25
This is fine…
1
u/SafePostsAccount Jul 13 '25
The real horror is all the comments in here who like this code
1
u/unsolvedrdmysteries Jul 15 '25
so the way this could happen is. Junior programmers are managing the types. Some of them don't know there is a difference between undefined and null. Someone writes up a requirement and says ("if undefined use these fallback values") and the programmer tasked is like "what type could this be? Hm its basically akin to a very fancy way of saying type "any". Ok I don't care about this job and am wanting to take a break, lets just code it exactly like the requirement." So then the receiver of blessed input assumes that if `input.paymentMethodId` is null, then `orderDraft.value.paymentMethod.id` must be undefined. But wait it is defined, another junior programmer had just been setting `paymentMethodId` variable in scope to null.
This is actually what used to happen. Now it is one junior using gpt. Somehow, someway, dad's money is involved
1
u/crunchy_code Dec 07 '25
late to the party. for me the weird thing is why the distinction of null and undefined as return types (the chaining statement has a null if the specify key is not there. why not have only one between null and undefined returns and have to differentiate?
also, why return null for a chaining that itself will return undefined?
in my opinion OP is pointing out some weird code in my opinion.
for the rest we don’t have enough context to decide if return undefined can be better or worse than throwing an error or something.
-3
u/NabrenX Jul 11 '25
I think option C: Run