Help me write a cleaner method?
Hi all,
I'm writing a function to break a list of assets into chunks with a max size.
I have an array of objects with the following signature:
{url: string, size: number}
. I want to end up with the following:
If I have a single chunk, I'd like it to just be named chunk.
The way I'm naming chunks is slightly more complicated but irrelevant to the question.
I'm wondering if there is a cleaner way to do this. This works fine and is fast, but to me feels a bit hacky. I'm not sure if that's because it is, or if I'm just being overly self-conscious.13 Replies
Why does it feel hacky to you
I think just because of the nested if/elses and logic duplication
I think it's fine 🤷♂️ would just rename
acc
to chunkSize
or w/e to make it more readable. Don't waste time on stuff that's not important 😛I appreciate that, I need to stop second guessing my code
If it becomes a problem in the future (don't see why) then you can fix it then, if it works it works
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Yes you're right
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I ended up writing it as a reducer function and I think I prefer it
LOL we had the same idea
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Reducers are like 4x slower than for loops but considering my array is maxed at like 200 it won't be an issue
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I appreciate the help from both of you
The first one was fine, but there was a cleaner way