Code Smell 180 – BitWise Optimizations

Bitwise operators are faster. Avoid these micro-optimizations

TL;DR: Don’t use bitwise operators unless your business model is bitwise logic.

Problems

Readability
Clevereness
Premature Optimization
Maintainability
Bijection Violation


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri

Bitwise operators are faster. Avoid these micro-optimizations

TL;DR: Don't use bitwise operators unless your business model is bitwise logic.

Problems

  • Readability

  • Clevereness

  • Premature Optimization

  • Maintainability

  • Bijection Violation

Solutions

  1. Improve readability

Context

Some clever programmers solve problems we don't have.

We should optimize code based on evidence and use the scientific method.

We should benchmark only if necessary and improve code only if really necessary and bear the cost of changeability and maintainability.

Sample Code

Wrong

const nowInSeconds = ~~(Date.now() / 1000)

Right

const nowInSeconds = Math.floor(Date.now() / 1000)

Detection

[X] Semi-Automatic

We can tell our linters to warn us and manually check if it is worth the change.

Exceptions

  • Real-time and mission-critical software.

Tags

  • Premature Optimization

Conclusion

If we find this code in a pull request or code review, we need to understand the reasons. If they are not justified, we should do a rollback and change it to a normal logic.

Relations

More Info

Tilde Operator ~~

Javascript BitWise Operators

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Frédéric Barriol on Unsplash

Original Article Here.

Watch the little things; a small leak will sink a great ship.

Benjamin Franklin

This article is part of the CodeSmell Series.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maxi Contieri | Sciencx (2022-11-19T18:31:51+00:00) Code Smell 180 – BitWise Optimizations. Retrieved from https://www.scien.cx/2022/11/19/code-smell-180-bitwise-optimizations/

MLA
" » Code Smell 180 – BitWise Optimizations." Maxi Contieri | Sciencx - Saturday November 19, 2022, https://www.scien.cx/2022/11/19/code-smell-180-bitwise-optimizations/
HARVARD
Maxi Contieri | Sciencx Saturday November 19, 2022 » Code Smell 180 – BitWise Optimizations., viewed ,<https://www.scien.cx/2022/11/19/code-smell-180-bitwise-optimizations/>
VANCOUVER
Maxi Contieri | Sciencx - » Code Smell 180 – BitWise Optimizations. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/19/code-smell-180-bitwise-optimizations/
CHICAGO
" » Code Smell 180 – BitWise Optimizations." Maxi Contieri | Sciencx - Accessed . https://www.scien.cx/2022/11/19/code-smell-180-bitwise-optimizations/
IEEE
" » Code Smell 180 – BitWise Optimizations." Maxi Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/11/19/code-smell-180-bitwise-optimizations/. [Accessed: ]
rf:citation
» Code Smell 180 – BitWise Optimizations | Maxi Contieri | Sciencx | https://www.scien.cx/2022/11/19/code-smell-180-bitwise-optimizations/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.