DEV Community

Cover image for When is it right to make code less efficient?
Best Codes
Best Codes

Posted on

When is it right to make code less efficient?

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
}
Enter fullscreen mode Exit fullscreen mode

v.s.

action
action
action
action
Enter fullscreen mode Exit fullscreen mode

)

Post your opinion in the comments below. Thanks for reading!

Top comments (9)

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

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 :)

Collapse
 
best_codes profile image
Best Codes

Thanks for your opinion! I think I can't disagree with that.🙂

Collapse
 
joolsmcfly profile image
Julien Dephix

Hi.

The example you have given is not less efficient, it's just not refactored.

Take this one:

for user in users
    // do something not related to user
    // rest of loop code
Enter fullscreen mode Exit fullscreen mode

It could be made more efficient with a very simple refactoring:

// do something not related to user
for user in users
    // rest of loop code
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
 
best_codes profile image
Best Codes

Thanks for your opinion! I'll keep that in mind.

Collapse
 
svidlak profile image
Max Svidlo

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.

Collapse
 
best_codes profile image
Best Codes

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.

Collapse
 
efpage profile image
Eckehard

You will get the difference when you need to change your code for some reason.

Collapse
 
best_codes profile image
Best Codes

LOL, true

Collapse
 
best_codes profile image
Info Comment hidden by post author - thread only accessible via permalink
Best Codes

Spam is harmful for dev.to dude.

Some comments have been hidden by the post's author - find out more