Which code you find easier to read
Yesterday at work, I found myself in a situation where I had to write a part of the code in two different ways
Option 1
```
const items = ['item1', 'item3']
const searchedItem = 'item';
const searchedItemExist = items.some((item) => searchItem === item);
if (!searchedItemExist) {
// do some processing
}
```
Option 2
```
const items = ['item1', 'item3']
const searchedItem = 'item';
const isSearchItemNew = !items.some((item) => searchItem === item);
if (searchedItemExist) {
// do some processing
}
```
and since I'm writing code for my colleagues to read && review, I decided to ask them which one they prefer,
and everyone chooses the first option,
for me when the If statement is not for early returns, I generally prefer the condition to be a positive condition (a condition without the not `!` operator), that's why my preference in this case was option 2, but the problem with this option is the `!` before doing the search of the item (!items.some((item) => searchItem === item)) which can be easier to miss.
I want to share this with you and I would love to know what your opinions are on this
5
3 comments
Djaballah Djedid
4
Which code you find easier to read
Senior Dev Academy
skool.com/devmastery-academy-8041
The #1 Free Community for JavaScript Developers to fast-track from Junior to Senior level with quality resources, feedback, and accountability 🥳
Leaderboard (30-day)
powered by