Avoid using nested ternary operators as they significantly reduce code readability and make debugging difficult. Complex conditional logic should be refactored into clear, readable structures using if-else statements, early returns, or extracted helper functions.
Avoid using nested ternary operators as they significantly reduce code readability and make debugging difficult. Complex conditional logic should be refactored into clear, readable structures using if-else statements, early returns, or extracted helper functions.
Why this matters:
How to refactor:
Instead of nested ternaries like this:
slotStartTime = slotStartTime.minute() % interval !== 0
? showOptimizedSlots
? rangeEnd.diff(slotStartTime, "minutes") % interval > interval - slotStartTime.minute()
? slotStartTime.add(interval - slotStartTime.minute(), "minute")
: slotStartTime.add(rangeEnd.diff(slotStartTime, "minutes") % interval, "minute")
: slotStartTime.startOf("hour").add(Math.ceil(slotStartTime.minute() / interval) * interval, "minute")
: slotStartTime;
Refactor to clear conditional blocks:
if (slotStartTime.minute() % interval === 0) {
// No adjustment needed
return slotStartTime;
}
if (showOptimizedSlots) {
const timeDiff = rangeEnd.diff(slotStartTime, "minutes") % interval;
const minutesToAdd = timeDiff > interval - slotStartTime.minute()
? interval - slotStartTime.minute()
: timeDiff;
return slotStartTime.add(minutesToAdd, "minute");
}
return slotStartTime
.startOf("hour")
.add(Math.ceil(slotStartTime.minute() / interval) * interval, "minute");
Additional guidelines:
Enter the URL of a public GitHub repository