If I had written it I would've at least wrapped the four outermost conditions in descriptive variables, such as `filterAdded`, `filterChanged`, etc. Fortunately the component is being rewritten as a functional component and changes are going to be handled more elegantly. No idea why this ever passed code review—it was all written before I was at this company.
4
u/Anund Nov 16 '23
This is when you wrap some of these conditions in functions to make the code readable.
if (filterChanged(currentFilter, prevState.currentFilter) || sortChanged(currentSort, prevState.currentSort)) {...}2
u/[deleted] Nov 16 '23
I had to check my code base just now to make sure because I have some code that looks just like this lmao
1
u/ElectricalPrice3189 11d ago
Still ugly:
(currentFilter && (!prevState.currentFilter || currentFilter.name !== prevState.currentFilter.name || currentFilter.value !== prevState.currentFilter.value)) ||
(currentSort && (!prevState.currentSort || currentSort.name !== prevState.currentSort.name || currentSort.value !== prevState.currentSort.value))