Hello fellow programmers!
Today, I made my first post that is solely a discussion. In fact, you are reading it right now!
So, I pose this question to you: When is it right to make code less efficient?
(e.g.
repeat 4 times {
action
}
v.s.
action
action
action
action
)
Post your opinion in the comments below. Thanks for reading!
Top comments (9)
Legibility and maintainability most often. It's difficult to comprehend the repetitions in your example when the number rises, and it's hard to maintain if the number should change in future. That said, if this was a hard and fast, only 4 makes sense, then perhaps it should be left long hand.
Unrolling loops can be a very powerful technique to optimise the tightest inner loops, it's rarely worth the trade off with readability outside of these areas.
Don't prematurely optimise I guess :)
Thanks for your opinion! I think I can't disagree with that.🙂
Hi.
The example you have given is not less efficient, it's just not refactored.
Take this one:
It could be made more efficient with a very simple refactoring:
Easy as pie and it's something I'd mention during code review.
I guess you could leave a less efficient code if it's to be used once. Even then I try to give hints during code review as it's good practice.
Thanks for your opinion! I'll keep that in mind.
Well, as all things in life - it depends.
Is the code never gonna be changed? are you working with junior developers? today its 4 actions, but tomorrow there might be 16 actions.
Maybe the number of actions executed depends on the state of your application?
Today's easy choices might cost a week of code refactoring in the next month, who knows.
IMO, im always trying to keep things dynamic as possible, because when the codebase grows so is the hidden complexity.
Thanks for your opinion! What I do is minify the code if it is very unlikely to ever be updated, which is maybe 1% of the time LOL.
You will get the difference when you need to change your code for some reason.
LOL, true
Spam is harmful for dev.to dude.
Some comments have been hidden by the post's author - find out more