# Code Smell 180 - BitWise Optimizations

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

# Problems

- Readability

- Clevereness

- Premature Optimization

- Maintainability

- [Bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle)  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

[Gist Url]: # (https://gist.github.com/mcsee/46a0a22d915e949c42cfb8260a5689bc)
```javascript
const nowInSeconds = ~~(Date.now() / 1000)
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/31a2d5a8699579d5472688b3ac9d9f61)
```javascript
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

%[https://maximilianocontieri.com/code-smell-20-premature-optimization]

%[https://maximilianocontieri.com/code-smell-165-empty-exception-blocks]

%[https://maximilianocontieri.com/code-smell-06-too-clever-programmer]

%[https://maximilianocontieri.com/code-smell-129-structural-optimizations]

# More Info

[Tilde Operator ~~](https://stackoverflow.com/questions/5971645/what-is-the-double-tilde-operator-in-javascript)

[Javascript BitWise Operators](http://rocha.la/JavaScript-bitwise-operators-in-practice)

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Frédéric Barriol](https://unsplash.com/@webmaster13870) on [Unsplash](https://unsplash.com/s/photos/clock)  

Original Article [Here](https://dev.to/dvddpl/clever-coding-tricks-that-we-dont-need--228l).
  
* * *

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

_Benjamin Franklin_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
